Using geo-coordinates as vertex coordinates in igraph r-package

In the igraph package for R, I am struggling to build a social network using latitude / longitude coordinates as a graph layout.

Imagine this simple example: a 4-node network with which you know the geographical location and connections:

df<-data.frame("from" = c("Bob", "Klaus", "Edith", "Liu"), "to"= c("Edith", "Edith", "Bob", "Klaus")) 

Here you have metadata for the nodes, so Bob lives in New York, Klaus in Berlin, Edith in Paris and Liu in Bejing:

 meta <- data.frame("name"=c("Bob", "Klaus", "Edith", "Liu"), "lon"=c(-74.00714, 13.37699, 2.34120, 116.40708), "lat"=c(40.71455, 52.51607, 48.85693, 39.90469)) 

We create an igraph object ...

 g <- graph.data.frame(df, directed=T, vertices=meta) 

... and we define our layout as longitude / latitude coordinates

 lo <- layout.norm(as.matrix(meta[,2:3])) plot.igraph(g, layout=lo) 

If you run this example with these (real) geo-coordinates, you will see that it is “relatively” accurate in the sense that the locations are correct relative to each other. However, if I draw a lot of such coordinates, the map of the world maps looks “stretched”.

Is there a way in which I can build my nodes on the world map so that the coordinates are 100% right and I see the connections between my nodes? I really want to continue to use the igraph package because it offers many features that I may need later when I want to analyze the connections between nodes.

+5
source share
1 answer

One solution element, no doubt, is the rescale = FALSE parameter for igraph::plot() , as I suggested in the comment. The OP asked why this person has an empty plot? This is due to the fact that the chart area is still limited by the interval [-1; 1] [-1; 1] along the x and y axes. The default value is igraph::plot() . Therefore, we need to give the parameters xlim = c(-180, 180) and ylim = c(-90, 90) . This already gives the correct positioning. However, if our goal is to create a figure with a world map, it might be better to write a graphic graph on an SVG cairo device. Then we can place the map behind the chart in any SVG editor (for example, Inkscape is a great solution), and we can still scale and edit the chart and labels. To do this, you need to set some other igraph.plotting parameters, but this already concerns proportions and aesthetics. Here is the code I used to output SVG:

 #!/usr/bin/Rscript require(igraph) require(Cairo) df <- data.frame("from" = c("Bob", "Klaus", "Edith", "Liu"), "to" = c("Edith", "Edith", "Bob", "Klaus")) meta <- data.frame("name" = c("Bob", "Klaus", "Edith", "Liu"), "lon" = c(-74.00714, 13.37699, 2.34120, 116.40708), "lat" = c(40.71455, 52.51607, 48.85693, 39.90469)) g <- graph.data.frame(df, directed = TRUE, vertices = meta) lo <- layout.norm(as.matrix(meta[,2:3])) dpi = 1.0 Cairo(file = 'map-graph.svg', type = "svg", units = "in", width = 4 / dpi, height = 2 / dpi, dpi = dpi) plot.igraph(g, layout = lo, xlim = c(-180, 180), ylim = c(-90, 90), rescale = FALSE, edge.curved = TRUE, edge.arrow.size = 10 / dpi, edge.arrow.width = 0.5 / dpi, vertex.label.dist = 50 / dpi, vertex.label.degree = 90 / dpi, vertex.size = 200 / dpi, vertex.label.cex = 21 / dpi, vertex.frame.color = NA, vertex.label.color = '#FFFF00', edge.color = '#FFFFFF', vertex.label.family = 'sans-serif', edge.width = 16 / dpi) dev.off() 

When the SVG created by igraph looks great, we can open it in Inkscape. Then import ( Ctrl+i ) the map in case it is a pixmap; or open if it's a vector graphic (e.g. PDF, SVG). Manually scale and place the map to set the same scale as the graph in SVG (i.e., until the points get to the right place) - for proportional scaling, hold Ctrl in Inkscape. Here is the result of this method:

enter image description here

(Map image is available for non-commercial use on Wikimedia Commons).

I think igraph is capable of creating such numbers, but this is not the main goal of this software, so it has its limitations. At some point, you can use some Geographic Information System (GIS) software, which is designed for just that. I have no experience with them, but qgis is probably worth a look at.

+6
source

All Articles