How to add geo-spatial connections on a ggplot map?

后端 未结 3 502
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 14:13

Using this as a reference, I\'m trying to plot a map of the lower forty-eight and add layers to visualize flow between states.

library(ggplot2)
library(maps)         


        
3条回答
  •  悲哀的现实
    2020-12-18 14:15

    gcIntermediate returns different column names (since origin and destination is identical for i=2):

    for (i in 1:3) {
      inter <- as.data.frame(gcIntermediate(c(geo[i,]$orig_lon, geo[i,]$orig_lat), 
                                            c(geo[i,]$dest_lon, geo[i,]$dest_lat), 
                                            n=50, addStartEnd=TRUE))
      print(head(inter, n=2))
    }
         lon   lat
    1 -119.7 36.17
    2 -119.6 36.13
          V1    V2
    1 -119.7 36.17
    2 -119.7 36.17
         lon   lat
    1 -119.7 36.17
    2 -119.5 36.24
    

    The following lines should work:

    for (i in 1:3) {
      inter <- as.data.frame(gcIntermediate(c(geo[i,]$orig_lon, geo[i,]$orig_lat), 
                                            c(geo[i,]$dest_lon, geo[i,]$dest_lat), 
                                            n=50, addStartEnd=TRUE))
      names(inter) <- c("lon", "lat")
      p <- p + geom_line(data=inter, aes(x=lon, y=lat), color='#FFFFFF')
    }
    

提交回复
热议问题