本文介绍了如何在ggplot地图上添加地理空间连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用此作为一个参考,我试图绘制一个较低的四十八个地图,并添加图层来可视化状态之间的流量。 图书馆(ggplot2)图书馆(地图)图书馆(geosphere)#为地球上给定的一对(拉特,长) #加载地图数据 all_states #plot state map p 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 c(geo [i,] $ dest_lon,geo [i,] $ dest_lat),n = 50,addStartEnd = TRUE)) list_lines [i ] p } p 这是当我尝试打印图时得到的结果 p 错误ev al(expr,envir,enclos):找不到对象'lon' 我试图调试这个,发现这是有效的 p + geom_line(data = list_lines [[1]],aes(x = lon,y = lat ),color ='#FFFFFF') 但是为第二个列表元素添加另一个层会打破它,但是就我对R和ggplot的有限了解而言,这是相当可观的!解决方案 gcIntermediate 返回不同的列名称(因为起源和目的地相同,所以i = 2): for (in 1:3){ inter 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 c(geo [i,] $ dest_lon ,geo [i,] $ dest_lat),n = 50,addStartEnd = TRUE))名称(inter)< -c(lon,lat)p } 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 USall_states <- map_data("state")# plot state mapp <- ggplot() + geom_polygon( data=all_states, aes(x=long, y=lat, group = group), colour="white", fill="grey10" )# sample origin - destination lat,long pairsgeo <- 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 pairlist_lines <- list()# use the geosphere package's gcIntermediate function to generate 50 interpolated # points for each origin-destination pairfor (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')}pHere is what I get when I try to print the plotpError in eval(expr, envir, enclos) : object 'lon' not foundI tried to debug this and found that this worksp + 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 lat1 -119.7 36.172 -119.6 36.13 V1 V21 -119.7 36.172 -119.7 36.17 lon lat1 -119.7 36.172 -119.5 36.24The 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地图上添加地理空间连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 04:20