使用 R 中的传单创建带有标记的假地图

create a fake map with markers using leaflet in R

我想用 leafleat 创建一个假地图。这个想法是有一个中心在 0,0 和半径 2 的圆,并在里面显示一些标记。我的圈子是这样生成的(代码如何改进,欢迎大家评论!)

library(sp)
library(leaflet)
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  Sr1 = Polygon(cbind(xx, yy))
  Srs1 = Polygons(list(Sr1), "s1")
  SpP = SpatialPolygons(list(Srs1), 1:1)
  return(SpP)
}
Circle.Town <- circleFun(c(0,0),5,npoints = 100)

我可以用下面的代码画圆:

leaflet(height = "400px") %>% addPolygons(data = Circle.Town)

我想使用以下数据向我的地图添加标记:

df1 <- data.frame(long=c(0.6,1,1.4), lat=c(-2, -.8, -0.2), other=c('a', 'b', 'c'), Color=c(10,8,6), 
                  type=c('Public', 'Public', 'Private'), id=c(1:3))

我希望标记的颜色为 Color,形状为 type。当我将鼠标悬停在标记上时,我还想要一个显示 idother 的工具提示。

我试过这个:

leaflet(height = "400px") %>% addPolygons(data = Circle.Town) %>% addMarkers(data = df1, lat = lat, lng =long )

但是我得到一个错误:

Error in inherits(f, "formula") : object 'long' not found

感谢您的帮助!

数据框中的列以公式表示,公式以旋转符号开头 ~:

  leaflet(height = "400px") %>% 
       addPolygons(data = Circle.Town) %>%
       addMarkers(data = df1, lat = ~lat, lng =~long )