[英]How can I plot individual data points in a map using R/ leaflet?
I am trying to show the individual points in a given place, like a map equivalent of dot plot.我试图显示给定位置的各个点,例如 map 相当于点 plot。 I tried with
leaflet
library in R, but I am only able to map the size of the marker to the continuous variable.我尝试在 R 中使用
leaflet
库,但我只能将 map 的标记大小设置为连续变量。 Is it possible to map the individual data points as clusters instead of mapping the size of the marker to the continuous variable?是否可以将单个数据点作为集群进行 map,而不是将标记的大小映射到连续变量?
My data looks like this我的数据看起来像这样
Lat,Lon,Place,People
19.877263,75.3390241,Aurangabad,1
20.2602939,85.8394548,Bhubaneshwar,2
30.7194022,76.7646552,Chandigarh,23
13.0801721,80.2838331,Chennai,25
11.0018115,76.9628425,Coimbatore,2
27.4844597,94.9019447,Dibrugarh,1
16.2915189,80.4541588,Guntur,1
17.3887859,78.4610647,Hyderabad,4
22.5677459,88.3476023,Kolkata,7
15.8309251,78.0425373,Kurnool,1
9.9256493,78.1228866,Madurai,1
You can use the following code to have dot plot您可以使用以下代码获得点 plot
leaflet(df) %>% addTiles() %>%
addCircleMarkers(lng = ~Lon, lat = ~Lat,
popup = ~Place)
df = structure(list(Lat = c(19.877263, 20.2602939, 30.7194022, 13.0801721,
11.0018115, 27.4844597, 16.2915189, 17.3887859, 22.5677459, 15.8309251,
9.9256493), Lon = c(75.3390241, 85.8394548, 76.7646552, 80.2838331,
76.9628425, 94.9019447, 80.4541588, 78.4610647, 88.3476023, 78.0425373,
78.1228866), Place = structure(1:11, .Label = c("Aurangabad",
"Bhubaneshwar", "Chandigarh", "Chennai", "Coimbatore", "Dibrugarh",
"Guntur", "Hyderabad", "Kolkata", "Kurnool", "Madurai"), class = "factor"),
People = c(1L, 2L, 23L, 25L, 2L, 1L, 1L, 4L, 7L, 1L, 1L)), class = "data.frame", row.names = c(NA,
-11L))
leaflet
works great with sf
package. leaflet
适用于sf
package。 Taking a sample of your data points抽取数据点样本
lat <- c(19.877263, 20.2602939)
lon <- c(75.3390241, 85.8394548)
place <- c("Aurangabad", "Bhubaneshwar")
You can convert them in spatial object using sf
package.您可以使用
sf
package 在空间 object 中转换它们。 For leaflet
to give you tiles, you need to have WSG84 coordinates.为了
leaflet
给你瓷砖,你需要有 WSG84 坐标。 I assumed your data were in this coordinate system.我假设你的数据在这个坐标系中。
library(sf)
df <- data.frame(lon, lat, place, stringsAsFactors = FALSE)
points <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326)
Then it's easy to plot with leaflet
.然后很容易 plot 与
leaflet
。 Assuming you want markers that popup the name of the place when you click假设您想要在单击时弹出地点名称的标记
library(leaflet)
leaflet(df) %>% addTiles() %>% addMarkers(popup = ~ place)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.