简体   繁体   English

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

[英]create a fake map with markers using leaflet in R

I would like to create a fake map with leafleat. 我想用leafleat创建一个假地图。 The idea is to have a circle with center in 0,0 and radius 2 and show some markers inside. 这个想法是有一个圆心,中心在0,0和半径2,并在里面显示一些标记。 This is how I generate the circle (comments on how to improve the code are very welcome!) 这就是我生成圆圈的方法(非常欢迎有关如何改进代码的评论!)

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)

I am able to draw the circle with the following code: 我可以使用以下代码绘制圆圈:

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

I would like to use the following data to add markers to my map: 我想使用以下数据将标记添加到我的地图中:

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))

I would like the color of the marker to be Color and the shape to be type . 我希望标记的颜色是Color ,形状是type I would also like to have a tooltip that shows id and other when I hover the mouse over the marker. 当我将鼠标悬停在标记上时,我还想要一个显示idother的工具提示。

I tried this: 我试过这个:

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

but I get an error: 但是我收到一个错误:

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

Thanks for the help! 谢谢您的帮助!

The columns in your data frame are expressed as formulas, and formulas start with a twiddle ~ : 数据框中的列表示为公式,公式以旋转开头~

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM