【问题标题】:How to add geo-spatial connections on a ggplot map?如何在 ggplot 地图上添加地理空间连接?
【发布时间】:2012-08-05 01:35:43
【问题描述】:

使用this 作为参考,我正在尝试绘制下四十八的地图并添加图层以可视化状态之间的流动。

library(ggplot2)
library(maps)
library(geosphere) # to inter-polate a given pair of (lat,long) on the globe

# load map data for the US
all_states <- map_data("state")

# plot state map
p <- ggplot() + geom_polygon( data=all_states, 
                       aes(x=long, y=lat, group = group),
                       colour="white", fill="grey10" )

# sample origin - destination lat,long pairs
geo <- structure(list(orig_lat = c(36.17, 36.17, 36.17), 
orig_lon = c(-119.7462, -119.7462, -119.7462), dest_lat = c(33.7712, 36.17, 39.0646), 
    dest_lon = c(-111.3877, -119.7462, -105.3272)), .Names = c("orig_lat", 
"orig_lon", "dest_lat", "dest_lon"), row.names = c(NA, 3L), class = "data.frame")

#> geo
#  orig_lat  orig_lon dest_lat  dest_lon
#1    36.17 -119.7462  33.7712 -111.3877
#2    36.17 -119.7462  36.1700 -119.7462
#3    36.17 -119.7462  39.0646 -105.3272

# list to hold a dataframe of interpolated points for each origin-destination pair
list_lines <- list()

# use the geosphere package's gcIntermediate function to generate 50 interpolated  
# points for each origin-destination pair
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))
  list_lines[i] <- list(inter)
  p <- p + geom_line( data = list_lines[[i]], aes(x = lon, y = lat), color = '#FFFFFF')
}
p

这是我尝试打印情节时得到的结果

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

我尝试调试它,发现它有效

p + geom_line( data = list_lines[[1]], aes(x = lon, y = lat), color = '#FFFFFF')

但是为第二个列表元素添加另一层会破坏它,但这是我对 R 和 ggplot 的有限了解!

【问题讨论】:

    标签: r ggplot2 geospatial


    【解决方案1】:

    gcIntermediate 返回不同的列名(因为 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
    

    以下几行应该有效:

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

    【讨论】:

      【解决方案2】:

      让我感到奇怪的是,您以两种不同的方式引用经度:long 在脚本的开头,lon 在结尾。如果您希望多个 geom 一起工作,则需要使这些名称保持一致。

      此外,几乎不需要使用 for 循环添加相同的 geom。只需添加一个 geom_line 并使用 color 美学来绘制多条线。

      【讨论】:

        【解决方案3】:

        使用ggplot2 有一个非常简单的解决方案。有一个简单的教程,介绍如何使用ggplot2hereR 中绘制流图。

        p + 
          geom_segment(data = geo, aes(x = orig_lon,    y = orig_lat, 
                                       xend = dest_lon, yend = dest_lat,
                                       color="#FFFFFF")) +  coord_equal()
        

        【讨论】:

          猜你喜欢
          • 2021-03-01
          • 2021-07-16
          • 2021-11-23
          • 1970-01-01
          • 1970-01-01
          • 2017-12-26
          • 1970-01-01
          • 2014-01-04
          • 2018-07-19
          相关资源
          最近更新 更多