简体   繁体   English

如何使用 R/leaflet 在 map 中 plot 单个数据点?

[英]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)

在此处输入图像描述

Data数据

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.

相关问题 使用R Leaflet和Shiny(多个条件)过滤地图上的数据点 - Filter data points on a map using R Leaflet and Shiny (multiple criteria) 使用传单 lib 在地图上绘制 34 个点 - using leaflet lib to plot 34 points on map 如何使用 ggbarplot 将单个数据点添加到条形图中? (添加抖动不起作用,点图也不起作用) - How can I add individual data points to my bar plot using ggbarplot? (add jitter does not work, neither does dotplot) 如何将R中的传单地图输出为可以使用Leaflet命令进行编辑的格式? - How do I output leaflet map from R into format where I can edit using Leaflet commands? 如何将点绘制为 R 中的颜色? - How can I plot points as colors in R? 如何在R中的小册子地图上绘制多个等时线 - how to plot multiple isochrone on leaflet map in R 如何在R中的地图中的特定位置绘制数据点 - How to plot data points at particular location in a map in R R直方图。 如何标记一些而非全部数据点 - R histogram. How can I label some but not all individual data points 如何使用Leaflet在R中创建GTFS数据的交互式图? - How to create an interactive plot of GTFS data in R using Leaflet? 如何根据 R Shiny 中 Leaflet 地图的输入过滤数据表? - How can I filter a data table based on input from a Leaflet map within R Shiny?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM