使用网络可视化地理地图上的数据(R) [英] Visualizing data on geographic map with networks (R)

查看:117
本文介绍了使用网络可视化地理地图上的数据(R)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用文化距离矩阵中包含的数据在地理地图上描绘一个网络.例如:

I am trying to depict a network on the geographic map using data contained in a cultural distance matrix. E.g.:

          AT      BE     CH     CZ
AT    0       0.00276 0.148  0.109
BE    0.00276 0       0.145  0.112
CH    0.148   0.145   0      0.257
CZ    0.109   0.112   0.257  0    

网络线路的起点和终点应位于不同的国家/地区(例如AT,BE,CH和CZ).

Starting and ending points of the network’s lines should be located in different countries (i.e. here AT, BE, CH and CZ).

当矩阵的相应条目低于某个阈值(例如,所有矩阵条目的平均值)时,应在国家/地区之间画线. (我想 dplyr 包可用于过滤数据,如示例

Lines should be depicted between countries when a corresponding entry of the matrix is below a certain threshold (e.g., the mean of all matrix’ entries). (I suppose dplyr package can be used to filter the data, as in the example http://www.gis-blog.com/flight-connection-map-with-r/)

地图包括欧亚大陆国家.我使用Trimble Data Marketplace获取Shapefile并在R中绘制了一个地理地图,如下所示:

The map includes countries of Eurasia. I used Trimble Data Marketplace to get Shapefile and draw a geographic map in R as below:

此地图是通过以下代码获得的:

This map is obtained with the code:

> shapefile <- readOGR("directory_with_file", "name_of_file")
> shapefile_df <- fortify(shapefile)
> map <- ggplot() + geom_path(data = shapefile_df, aes(x = long, y = lat,
group = group),color = ‘black', size = .2)
> print(map)

现在如何使用矩阵的数据在此地理地图上绘制网络?

How can I draw network on this geographic map now using the matrix’ data?

(网络将代表国家的文化接近度及其随着时间的演变)

(Networks will represent cultural proximity of countries and its evolution over time)

推荐答案

您需要为所需的内容建立网络;我从ggmap :: geocode包获得的坐标; 一旦有了网络,就可以设置"Edge"参数来表示您拥有的文化距离值,因为它从0.002变为0.2,所以必须将其放大,否则您会得到非常小的线条,此代码将帮助您获得在赛道上,您只需要添加剩余的文化距离

You need to build a network for what you're looking for; the coordinates I obtained from ggmap::geocode package; Once you have the network, you set the "Edge" parameter to represent the cultural distance value you have, since this moves from 0.002 to 0.2 you have to enlarge it, otherwise you'll get very tiny lines, this code will help you get on the track, you just need to add the remaining cultural distances

library(maps)
library(igraph)

df<-data.frame(from = c("at", "be", "ch", "cz"), to= c("be", "ch", "cz", "at"), 
               weight=c(0.02,0.145,0.257,.109))
meta <- data.frame("name"=c("at", "be", "ch", "cz"), 
                   "lon"=c(14.55,4.46,8.227,14.4738),  
                   "lat"=c(47.51,50.5,46.818,50.0755))

g <- graph.data.frame(df, directed=F, vertices=meta)
E(g)$color <- "brown"
E(g)$width <- E(g)$weight*10
lo <- as.matrix(meta[,2:3])
map("world",  xlim = c(-8, 30),
    ylim = c(35, 55), asp=1)
plot(g, layout=lo, add = TRUE, rescale = FALSE)

其他建议:在ggplot2中绘制地图时,请使用+ coord_equal()

additional advice: use + coord_equal() when plotting maps in ggplot2

这篇关于使用网络可视化地理地图上的数据(R)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆