问题
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
Starting and ending points of the network’s lines should be located in different countries (i.e. here AT, BE, CH and CZ).
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/)
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)
回答1:
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)
additional advice: use + coord_equal() when plotting maps in ggplot2
来源:https://stackoverflow.com/questions/49454687/visualizing-data-on-geographic-map-with-networks-r