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

时间:2018-05-27 08:14:45

标签: r dictionary routes coordinates ggmap

我正在尝试在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)。 (我还编辑了我的坐标指向分隔符而不是逗号分隔符,这也是立即指出的)

谢谢

1 个答案:

答案 0 :(得分:3)

我可以在你的代码中看到2个问题。

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

问题#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