花了很多时间将网络格式化为SNA和igraph包中用于分析的格式。是否存在与Rgraphviz所需数据类型之间的桥梁?我的意思是,保留源到目的地、标签、边权重以及其他属性,如颜色等。
是的,非常容易,在igraph文档中有清晰的说明:
write.graph(mygraph, file="filename", format="dot")
igraph
转换为Rgraphviz
的包,但是有一种方法可以避免保存文件。函数graph::graphAM
接收一个邻接矩阵,该矩阵可以使用函数igraph::get.adjacency
从igraph
中获取。library(tidyverse)
# Create the Data
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
"Esmeralda"),
age=c(48,33,45,34,21),
gender=c("F","M","F","M","F"))
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))
# Create the igraph object
g <- igraph::graph.data.frame(relations, directed=TRUE, vertices=actors)
# transform igraph object in graph which will be used in Rgraphviz
graph::graphAM(igraph::get.adjacency(g) %>% as.matrix(),
edgemode = 'directed') %>%
Rgraphviz::layoutGraph() %>%
Rgraphviz::renderGraph()