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

Can't plot route with ggmap. route() does not retrieve correct coordinates?

我正在尝试在 ggmap 中绘制路线,但它们没有出现在地图上。在我看来,路线得到了错误的坐标。我尝试了 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 个问题。

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

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