我创建了一个图表,igraph
其中每个节点都有一个空间位置lat
和long
。我想ggraph
使用这些空间坐标来绘制这个网络来定义节点的位置/布局。关于如何做到这一点的任何想法?
我知道可以这样做igraph
(请参阅下面的可重现示例),但我更喜欢使用 来执行此操作ggraph
,特别是因为我想ggmap
在绘图中添加一个图层。
可重现的例子
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)
使用空间坐标绘制网络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)