R:使用 ggmap 映射多个路由

R: Mapping multiple Routes using ggmap

本周一段时间以来,我一直在尝试改变这个 lovely flowing data visualization,但在最终实施中一直遇到障碍。

这是我正在使用的data set。我已将其融合到一个框架中,其中包含我要显示的三个关键信息位:startinglatlong、endinglatlong 和旅行次数。

我最接近使用这个想法 posted here,但是有两个在两个项目上遇到了障碍:

1) 根据出行次数改变线路的大小 2) 获取 google api 允许我调用这么多行(我的数据集中有 55,704)。

counts 是我的完整 df 的名称,看起来像这样:

head(counts)
  X from_station_id.x to_station_id.x From_Station_Lat From_Station_Long    End_Station_Lat End_Station_Long   n                   eichel     
1 1                 5               5         41.87396         -87.62774        41.87396        -87.62774 275 41.87395806 -87.62773949  
2 2                 5              13         41.87396         -87.62774        41.93250        -87.65268   1 41.93250008 -87.65268082    
3 3                 5              14         41.87396         -87.62774        41.85809        -87.65107  12     41.858086 -87.651073
4 4                 5              15         41.87396         -87.62774        41.85645        -87.65647  19     41.856453 -87.656471
5 5                 5              16         41.87396         -87.62774        41.91033        -87.67252   7     41.910329 -87.672516
6 6                 5              17         41.87396         -87.62774        41.90332        -87.67273   5       41.90332 -87.67273
                thomas
1 41.87395806 -87.62773949
2 41.87395806 -87.62773949
3 41.87395806 -87.62773949
4 41.87395806 -87.62773949
5 41.87395806 -87.62773949
6 41.87395806 -87.62773949

然后我着手为想法post中的函数制作一个更简单的df,a la:

start<-c(counts[1:10,9])
dest<-c(counts[1:10,10])

我想我可能会在函数中添加数字,所以我在 n 上做了标记(也许不是最好的命名约定,但请坚持我的做法)。

n <- c(counts[1:10, 8])

然后是路径搜索功能:

leg <-function(start, dest){
     r<- route(from=start,to=dest,mode = c("bicycling"),structure = c("legs"))  
     c<- geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat),
                  alpha = 2/4, size = 2, data = r, colour = 'blue') 

     return (c)
 }

底图:

a<-qmap('Chicago', zoom = 12, maptype="roadmap", color="bw") 

现在的魔法:

for (n in 1:10){
     #l<-leg(start[n], dest[n])  
     l<-leg(as.character(df[n,1]), as.character(df[n,2]))  

    a<-a+l
 }
a

这成功了。

不幸的是,当我尝试在更大的子集上 运行 它时,它会 运行 一点点然后去:

Information from URL : http://maps.googleapis.com/maps/api/directions/json?   origin=41.88871604+-87.64444785&destination=41.87395806+-87.62773949&mode=bicycling&units=metric&alternatives=false&sensor=false
Error: (list) object cannot be coerced to type 'integer'

我从这里和其他地方搜索了解到,这可能是由于 Google 门控 api 调用,因此尝试添加 Sys.sleep(1),但这会中断,所以去了 Sys.sleep(1.5) 和 f运行kly 似乎仍然如此。考虑到对于 +55k 行,您正在查看 +23 小时的通话,即使这是一个非常昂贵的通话。我的代码是:

for (n in 1:30){
 #l<-leg(start[n], dest[n])  

 l<-leg(as.character(df[n,1]), as.character(df[n,2]))  
 Sys.sleep(1.5)     
a <- a + l
a}

这似乎 运行 但当我输入 "a" 我得到:

Error in eval(expr, envir, enclos) : object 'startLon' not found

最后如前所述,我想为更多使用的路线可视化更粗的线条。通常我会通过 aes 并做类似的事情:

geom_path(
aes(x = lon, y = lat),  colour = 'red', size = n/100,
data = df, lineend = 'round'
  )

所以它会根据路线数量读取 n 列和 g运行t 的大小。为了让它在这里工作,我需要那个号码绑定到方向路线,所以我写了第二个函数,如下所示:

leg <-function(start, dest, n){

 r<- route(from=start,to=dest,mode = c("bicycling"),structure = c("route"))  
 c<- geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat),
              alpha = 2/4, size = n/10, data = r, colour = 'blue') 

 return (c)
}

for (n in 1:55704){
#l<-leg(start[n], dest[n])  

l<-leg(as.character(df[n,1]), as.character(df[n,2]), as.numeric(df[n,3]))  
Sys.sleep(1)       
a <- a+l       
  }

这个运行一分钟然后死于错误:

Error: (list) object cannot be coerced to type 'integer'

但较短的版本非常接近:

for (n in 2:6){
#l<-leg(start[n], dest[n])  

l<-leg(as.character(df[n,1]), as.character(df[n,2]), as.numeric(df[n,3]))  
Sys.sleep(1)       
a <- a+l       
}

据我所知,它有效,但最多只有 30 个。遗憾的是,较长的版本有点 运行 出来了。基本上我认为,如果我能克服错误消息,我就快成功了,我只是不想花几天时间 运行 查询。欢迎所有帮助和输入。谢谢你的时间。

好的,经过大量的修改和修改,我终于确定了可行的循环解决方案:

leg <-function(start, dest, n){

    r<- route(from=start,to=dest,mode = c("walking"),structure = c("route"))  
    c<- geom_path(aes(x = lon, y = lat),
             alpha = 2/4, size = as.numeric(n)/500, data = r, colour = 'blue') 
    Sys.sleep(runif(1, 3.0, 7.5))
    return (c)
}

a <- qmap('Chicago', zoom = 12, maptype = 'road', color="bw")

for (n in 101:200){
    l<-leg(as.character(df[n,1]), as.character(df[n,2]),as.character(df[n,3])) 

    a<-a+l
}

a

这很有效。唯一的障碍是 google api 会拒绝呼叫。在我添加随机变量 sys.sleep 后,它工作顺利。也就是说,我仍然从来没有一次尝试超过 150 个(为了便于视觉和功能,将我的映射限制为前 10% 的路线样本)。最后,经过一段愉快的插画时间后,我得到了一张漂亮的地图。感谢社区的兴趣和提供循环的想法。