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


in igraph package r, struggling plot social network using latitude/longitude coordinates layout of graph.

imagine simple example: network 4 nodes of know geographical location , connections:

df<-data.frame("from" = c("bob", "klaus", "edith", "liu"), "to"= c("edith", "edith", "bob", "klaus")) 

here have meta-data nodes, bob lives in ny, klaus in berlin, edith in paris , 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 make g igraph object...

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

...and define our layout longitude/latitude coordinates

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

if run example these (real) geo-coordinates, you'll see "relatively" accurate in sense locations right relative each other. however, if plot lot of coordinates this, worlds cartesian map looks "stretched out".

is there way can plot nodes on world map coordinates 100% right , see connections between nodes? want keep using igraph package offers lot of functionalities might need later on when want analyze links between nodes.

one element of solution no doubt rescale = false parameter igraph::plot() suggested in comment. op asked why ey gets empty plot this? it's because plotting area still limited [-1; 1] interval along both x , y axes. default of igraph::plot(). need give xlim = c(-180, 180) , ylim = c(-90, 90) parameters. gives correct positioning. however, if our aim produce figure map of world, maybe best write igraph plot onto cairo svg device. able place map behind graph in svg editor (e.g. inkscape great solution), , still free scale , edit graph , labels. doing this, other igraph.plotting parameters necessary set, proportions , aesthetics. here code used produce svg output:

#!/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 svg produced igraph looks fine, can open in inkscape. import (ctrl+i) map in case pixmap; or open if vector graphics (e.g. pdf, svg). manually scale , position map set same scale graph in svg (i.e. until points right place) – proportional scaling, hold ctrl in inkscape. here result of method:

enter image description here

(the map image made available non-commercial public use wikimedia commons).

i think igraph capable produce figures this, not primary aim of software, has limitations. @ point might consider use geographic information system (gis) software designed things this. have no experience those, qgis worths take at.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -