igraph add to geographic map

▼魔方 西西 提交于 2019-12-03 03:59:10

You can do it like this:

library(raster)
library(igraph)
greece <- getData('GADM', country='GRC', level=1)
df<-data.frame("from" = c("Athens", "Iraklio", "Thessaloniki", "Patra"), "to"= c("Thessaloniki", "Thessaloniki", "Athens", "Iraklio"))
meta <- data.frame("name"=c("Athens", "Iraklio", "Thessaloniki", "Patra"), 
               "lon"=c(23.72800,25.13356,22.94090,21.73507),  
               "lat"=c(37.98415,35.33349,40.63229,38.24628))
g <- graph.data.frame(df, directed=T, vertices=meta)
lo <- as.matrix(meta[,2:3])
plot(greece)
plot(g, layout=lo, add = TRUE, rescale = FALSE)

Obviously you don't want to normalize your layout coordinates to a scale from -1 to 1, because your geo plot does not use that scale. So no layout.norm(). However, it seems that the latest igraph automatically normalizes the coordinates by default under the hood. At first, I didn't find the responsible rescale parameter in the documentation and had to use debug(plot.igraph) to trace and see it. (Although it's documented in ?igraph.plotting.) If you set rescale=FALSE and add=TRUE, then it should work as expected.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!