Is there a package to convert network() or igraph() networks to Rgraphviz compatible in R?
Question
Spent a lot of time formatting networks in the formats used for analysis in SNA and igraph packages. Is there a bridge between these and Rgraphviz's desired data-type? By this, I mean, preserves: Source-to-destination, Label, Edge weight, other attributes like color, etc.
Solution
Yes, very easily, and clearly in the igraph documentation:
write.graph(mygraph, file="filename", format="dot")
OTHER TIPS
I didn't find any package to do convert igraph
in Rgraphviz
, but, this is one way without saving the file. The function graph::graphAM
receive an adjacency Matrix which can be got from igraph
using the function igraph::get.adjacency
.
With that approach, if you have any feature linked to nodes or edges you will need to add them individually.
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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow