在ggmap中添加一个圆形

11

假设我使用ggmap包生成了一个伦敦的地图:

library(ggmap)
library(mapproj)

map <- get_map(location = "London", zoom = 11, maptype = "satellite")

p <- ggmap(map)+ 
     theme(legend.position = "none") 

print(p)

现在我想在这张图上添加一个圆,它有一些中心坐标(比如:lon=-0.1,lat=52.23),半径可以用公里表示。

我试着使用类似问题的解决方案(用ggplot2绘制圆形),你只需要向函数添加这样一个语句:

p <- p + annotate("path",
                  x = xc+r*cos(seq(0,2*pi,length.out=100)),
                  y = yc+r*sin(seq(0,2*pi,length.out=100)))

它可以工作,但由于不同的比例尺,圆形并不真正是圆形。有可能正确地绘制它吗? 非常感谢任何帮助!

编辑: 我找到了解决方案(https://gis.stackexchange.com/questions/119736/ggmap-create-circle-symbol-where-radius-represents-distance-miles-or-km),使用不同的包,输出是正确的。不过,如果有人知道如何使用ggmap做到这一点,请分享。


你尝试在地图绘制的末尾添加 + coord_equal() 吗? - Phil
是的,但我认为这样就不可靠了,从距离方面来看。 - Michał
此外,输出结果也不同,例如与此网站(http://www.freemaptools.com/radius-around-point.htm)获得的输出结果不同。 - Michał
我看到你创建圆形的代码中最大的问题是,你的中心点是以纬度、经度对而不是给定英里或公里对的距离对表示的... 如果你使用这篇文章链接,你可以生成一组纬度和经度对,无论缩放级别如何,都应该生成一个给定大小的圆。 - Travis Gaddie
2个回答

1

这里提供了一个使用sf包和ggplot::geom_sf的解决方案。首先,通过坐标创建一个点并使用EPSG 32630将其转换为伦敦的UTM区域(30u),以便确定距离:

# dev version of ggplot2 required
library(sf)
library(ggplot2)

sf_pt <- st_point(c(-0.1, 52.23)) %>% 
  st_sfc(crs = 4326) %>%
  st_transform(32630)

然后添加一个缓冲区。
sf_pt %<>% st_buffer(100)

现在将其转换回EPSG:4326(纬度/经度WGS84),并使用ggmap绘制。
p <- ggmap(map) +
  geom_sf(data = sf_pt %>% st_transform(4326)) +
  theme(legend.position = "none") 

print(p)

0
您可以从地图对象中获取经度和纬度跨度:
> m = get_map(location="london", zoom=11, maptype="satellite")
> corners = attributes(m)$bb
> delta.x = corners["ur.lon"] - corners["ll.lon"]
> delta.y = corners["ur.lat"] - corners["ll.lat"]

然后根据需要调整您的路径。请注意,ggmap包中有一个名为LonLat2XY的函数(参见reference)。


2
能否演示一下在这种情况下"accordingly"是什么意思? - Roman Luštrik
“按照相应的方式”是否只涉及将圆形大小参数链接到正在被删除的变量?例如,如果您的位置lat和lon分别为37.8和122.4,则delta.y的角落可能为37.9 [ur] - 37.7 [ll] - 但您必须在引用中构建计算以使圆圈的大小符合您提供的每个坐标。 - leerssej

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接