I have a graph created with igraph
where each node has a spatial location with lat
and long
. I would like to use ggraph
to plot this network using these spatial coordinates to define the position/layout of nodes. Any ideas on how to do this?
I know it is possible to do this in igraph
(see reproducible example below) but I'd prefer doing this with ggraph
, particularly because I would like to add a ggmap
layer to the plot.
reproducible example
library(igraph)
library(ggraph)
# create network
# nodes
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David","Esmeralda"),
age=c(48,33,45,34,21),
gender=c("F","M","F","M","F"),
long=c(-43.17536, -43.17411, -43.36605, -43.19155, -43.33636),
lat=c(-22.95414, -22.9302, -23.00133, -22.90353, -22.87253))
# edges
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David","David", "Esmeralda"),
to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
same.dept=c(FALSE,FALSE,TRUE,FALSE,FALSE,TRUE),
friendship=c(4,5,5,2,1,1), advice=c(4,5,5,4,2,3))
# Graph
g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)
Plot network with spatial coordinates using igraph
# get lat long coordinates for the layout
lo <- layout.norm(as.matrix(actors[, c("long","lat")]))
#plot
plot.igraph(g, layout=lo, rescale=T, vertex.label= NA)