是否有一个包将网络()或igraph()网络转换为R中兼容的Rgraphviz?

时间:2012-03-12 08:04:15

标签: r graphviz igraph

花了很多时间在SNA和igraph包中用于分析的格式格式化网络。这些和Rgraphviz所需的数据类型之间是否存在桥梁?我的意思是,保留:源到目的地,标签,边缘权重,其他属性,如颜色等。

2 个答案:

答案 0 :(得分:4)

是的,很容易,而且明确地在the igraph documentation中:

write.graph(mygraph, file="filename", format="dot")

答案 1 :(得分:0)

我没有找到要在igraph中转换Rgraphviz的任何软件包,但是,这是不保存文件的一种方法。函数graph::graphAM接收到一个 邻接矩阵 ,该矩阵可以使用函数igraphigraph::get.adjacency获取。

使用这种方法,如果您具有链接到节点或边的任何功能,则需要单独添加它们。

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()

enter image description here