r - 无法使用 ggmap 绘制路线。 route() 没有检索到正确的坐标?

标签 r dictionary routes coordinates ggmap

我试图在 ggmap 中绘制路线,但它们没有出现在 map 上。在我看来,路线得到了错误的坐标。我尝试了 geom_leg 和 geom_path。这是示例:

mainroute2 <- route(from = c("39.951", "-75.173"),  # I tried with point and
               to = c("39.954","-75.195"),            # comma separator
               alternatives = FALSE, structure = "route")

 map2 <- get_map(
    location = c(lon=-75.16662, lat=39.95258), # painfully picked by hand
       source = "google", zoom = 13, maptype = "roadmap")


ggmap(map2) + geom_path(
       aes(x = lon, y = lat ),
       alpha = 3/4, size = 1, color = "black", data = mainroute2
      )

我已经尝试了很多 ggmap、qmap、geom_path、geom_leg 的替代方案和组合。一切都失败了。上周我做到了,但现在我做不到了!

此外,当您使用 ggplot 绘制 mainroute2 时(或者如果您目视检查),您会看到经度坐标为 105,这没有意义,因为路线应该从“39,951”、“-75,173”到“39,954”, “-75,195”。

请帮忙!

编辑:问题已回答。坐标必须是单个折叠的 char 元素,而不是矢量 c(lat, lon)。 (我还编辑了我的坐标以点分隔符而不是逗号分隔符,这一点也立即被指出)

谢谢

最佳答案

我在您的代码中发现了 2 个问题。

问题 #1: route 函数的 From/to 参数应该是名称或逗号分隔的纬度和经度。

问题 #2: 使用 , 作为小数点分隔符。可能与您系统的区域设置不匹配。

修改后的代码:

library(ggmap)
mainroute2 <- route(from = "39.951,-75.173",  # lat,lon
                    to = "39.954,-75.195",      
                    alternatives = FALSE, structure = "route")

map2 <- get_map(
  location = c(lon=-75.16662, lat=39.95258), # painfully picked by hand
  source = "google", zoom = 13, maptype = "roadmap")

# color is changed to Red to make it visible clearly 
ggmap(map2) + geom_path(
  aes(x = lon, y = lat ),
  alpha = 3/4, size = 1.5, color = "red", data = mainroute2
) 

enter image description here

关于r - 无法使用 ggmap 绘制路线。 route() 没有检索到正确的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50550257/

相关文章:

r - 映射博客之间的链接连接的最佳方式是什么?

javascript - 字符串化 map 中的数据?

symfony - 在 Symfony 4 中为路由配置默认主机

r - R 中泊松随机变量生成的改进逆变换方法

R Shiny Conditional Input 标签基于另一个输入

c++ - 标记化字符串时出现段错误

使用 Azure CDN 性能下降?

azure - 基于http header 自定义逻辑的动态路由(Azure、AWS、GCP)

r - 检查重复项,求和并在求和后删除一行

java - 更改 Hibernate 实体中的字段类型?