简体   繁体   English

R:使用ggmap映射多条路线

[英]R: Mapping multiple Routes using ggmap

I have been trying to make a different this lovely flowing data visualization for some time this week but keep hitting a snag in the final implementation. 这个星期我一直在尝试改变这种可爱的流动数据可视化效果 ,但是在最终实现中一直遇到障碍。

Here is the data set I'm using. 这是我正在使用的数据集 I have melded it into a frame with the three key bits of information I want to display: startinglatlong, endinglatlong, and number of trips. 我将其与要显示的三个关键信息融合为一个框架:startinglatlong,Endinglatlong和行程数。

I got closest using the idea posted here , but two hit a snag on two items: 我使用这里发布的想法最接近,但是有两个碰到了两个问题:

1) making the size of the line change based on the number of trips 2) getting the google api to allow me to call this many rows (I have 55,704 in my data set). 1)根据行程数来更改行的大小2)获取Google API以允许我调用这么多行(我的数据集中有55,704)。

counts is the name of my full df, with looks like so: 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

Then I set about making an easier df for the function in the idea post, a la: 然后,我开始在想法文章la中为函数创建更简单的df:

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

I thought I might add in numbers into the function so I tagged on n (maybe not the best naming convention, but stick with me here). 我以为可以在函数中加上数字,所以我在n上加了标签(也许不是最好的命名约定,但请在这里坚持使用)。

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

then the route searching function: 然后路由搜索功能:

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

base map: 基本图:

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

now the magic: 现在的魔力:

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

This worked. 这工作了。

unfortunately when I tried to run it on a larger subset it would run for a little bit and then go: 不幸的是,当我尝试在更大的子集上运行它时,它会运行一点然后走:

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'

I understand from searching here and elsewhere that this can be due to Google gating api calls, and so tried adding in Sys.sleep(1), but that would break, so went to Sys.sleep(1.5) and frankly that still seems to. 从这里和其他地方的搜索中我了解到,这可能是由于Google门控api调用造成的,因此尝试添加Sys.sleep(1),但这会中断,因此转到Sys.sleep(1.5),坦率地说,这似乎仍然。 Even that is a pretty expensive call, given that for +55k rows you're looking at +23 hours of calls. 考虑到对于+ 55k行,您正在查看+23个小时的通话,即使那样,这也是一个非常昂贵的通话。 My code was: 我的代码是:

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}

this seemed to run but when I entered "a" I got: 这似乎可以运行,但是当我输入“ a”时,我得到了:

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

Finally as mentioned I'd like to visualize thicker lines for more used routes. 最后,如前所述,我想为更常用的路线显示较粗的线。 typically I'd do this via the aes and doing something like: 通常,我会通过aes并执行类似的操作:

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

so it would read column n and grant a size based on number of routes. 因此它将读取列n并根据路由数量授予大小。 for that to work here I need that number to bind to the directions route, so I wrote a second function like this: 为了在这里工作,我需要将该数字绑定到路线指南,因此我编写了第二个函数,如下所示:

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       
  }

This ran for a minute and then died on the error: 这运行了一分钟,然后因错误而死亡:

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

but a shorter version got tantalizingly close: 但更短的版本非常诱人:

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       
}

it worked, as far as I can tell, but nothing more than like 30. Sadly the longer version just kind of runs out. 据我所知,它的工作原理是30,但遗憾的是更长的版本却用光了。 basically I think that if I can get past the error message I'm almost there, I just don't want to have to spend days running the query. 基本上,我认为,如果我可以克服错误消息,而我几乎就在那儿,那我只是不想花几天时间运行查询。 All help and input welcome. 欢迎所有帮助和投入。 thank you for your time. 感谢您的时间。

ok, so after a lot of noodling and modifying the above I finally settled on the looping solution that works: 好的,因此经过大量修改和修改上述内容,我最终确定了可行的循环解决方案:

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

this worked fairly well. 这工作得很好。 the only bumps were when it the google api would reject the call. 唯一的障碍是当Google API拒绝呼叫时。 after I added the random variable sys.sleep in there it worked without a hitch. 在其中添加了随机变量sys.sleep之后,它运行顺利。 That said, I still never tried more than 150 at a go (limited my mapping to a sample of the top 10% of routes for ease of visual and for function). 就是说,我仍然从未尝试过超过150次(为了简化视觉和功能,我将映射限制在前10%路径的示例中)。 Finally after some happy illustrator time I ended up with a good looking map. 最终,经过一段愉快的插画家时间,我最终得到了一张漂亮的地图。 Thanks to the community for the interest and for providing the looping idea. 感谢社区的关注并提供了循环的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM