Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save an igraph plot

Tags:

r

igraph

Great package igraph, but I'm struggling to save the plots. I have trawled the net without success. This is some sample code, how can I save the resulting plot?

Test <- data.frame(
  FirstName=c("Bob","Charlie","Beth","Sam"),
  Age=c(23,56,41,33))

Friends <- c(1,2,1,4,2,4,3,4)

g <- graph.empty (4, directed = FALSE)
V(g)$color <- "lightblue"  #Nodes$NodeColour
V(g)$label <- as.character(Test$FirstName)
g <- add.edges(g, Friends)
plot(g,
     vertex.label.color="black",
     vertex.shape="sphere",
     vertex.label.cex = 0.5,
     vertex.size=24,
     layout=layout.circle)
title("Friend Network",cex.main=1,col.main="blue")

#how do you save plot as a png?
like image 447
Zeus Avatar asked Aug 14 '26 21:08

Zeus


1 Answers

Rather than saving graphs as static images, I prefer to save them as html files and have an interactive graph:

library(visNetwork)
library(htmlwidgets)
saveWidget(visIgraph(g), file = "test.html")

See https://datastorm-open.github.io/visNetwork/igraph.html for more details.

like image 174
c0bra Avatar answered Aug 16 '26 09:08

c0bra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!