如何在ggplot地图上添加地理空间连接? [英] How to add geo-spatial connections on a ggplot map?
问题描述
使用此作为一个参考,我试图绘制一个较低的四十八个地图,并添加图层来可视化状态之间的流量。
图书馆(ggplot2)
图书馆(地图)
图书馆(geosphere)#为地球上给定的一对(拉特,长)
#加载地图数据
all_states <-map_data(state)
#plot state map
p <-ggplot()+ geom_polygon(data = all_states,
aes (x = long,y = lat,group = group),
color =white,fill =grey10)
#样本来源 - 目标纬度,长对$ b $ (33.7712,36.17,39.0646),$ b $ lt; - 结构(列表(orig_lat = c(36.17,36.17,36.17),
orig_lon = c(-119.7462,-119.7462,-119.7462),dest_lat = c b $ b 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来保存每个起始 - 目的地对的插入点数据帧
list_lines< - list()
#使用geosphere包的gcIntermediate函数来生成50个插值的$ (i在1:3){b $ b 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
错误ev al(expr,envir,enclos):找不到对象'lon'
我试图调试这个,发现这是有效的
p + geom_line(data = list_lines [[1]],aes(x = lon,y = lat ),color ='#FFFFFF')
但是为第二个列表元素添加另一个层会打破它,但是就我对R和ggplot的有限了解而言,这是相当可观的!
解决方案
for (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))
名称(inter)< -c(lon,lat)
p < p + geom_line(data = inter,aes(x = lon,y = lat),color ='#FFFFFF')
}
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)
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
Here is what I get when I try to print the plot
p
Error in eval(expr, envir, enclos) : object 'lon' not found
I tried to debug this and found that this works
p + geom_line( data = list_lines[[1]], aes(x = lon, y = lat), color = '#FFFFFF')
but adding another layer for the second list element breaks it, but that's as far as I could get with my limited knowledge of both R and ggplot!
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')
}
这篇关于如何在ggplot地图上添加地理空间连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!