【问题标题】:Add a circle to ggmap向ggmap添加一个圆圈
【发布时间】:2018-02-27 18:42:01
【问题描述】:

假设我使用 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)和半径,例如以公里为单位。 我尝试使用类似问题(Draw a circle with 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() 添加到地图图的末尾?
  • 是的,但我认为那在距离方面不可靠
  • 加上输出不同,例如从本网站获得的输出:freemaptools.com/radius-around-point.htm
  • 我在创建圆圈的代码中看到的最大问题是,您的中心是纬度、长对,而不是给定英里或公里对中的距离对......如果你使用这篇文章@ 987654324@ 你可以生成一组 lat long 对,无论缩放如何,它们都应该生成一个给定大小的圆

标签: r google-maps ggplot2 data-analysis ggmap


【解决方案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 (lat/lon WGS84) 并使用 ggmap 绘图

p <- ggmap(map) +
  geom_sf(data = sf_pt %>% st_transform(4326)) +
  theme(legend.position = "none") 

print(p)

【讨论】:

    【解决方案2】:

    可以从地图对象中获取经纬度跨度:

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

    【讨论】:

    • 在这种情况下想证明“相应地”是什么意思?
    • “相应地”不只是涉及将圆圈大小参数链接到要放入的变量吗?因此,例如,如果您的位置 lat 和 lon 分别为 37.8 和 122.4,则 delta.y 的角可能是 37.9[ur] - 37.7[ll] - 但您必须根据参考建立计算以获得为您提供的每个坐标圈出您想要的大小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 2016-06-22
    • 2011-03-10
    • 1970-01-01
    相关资源
    最近更新 更多