7

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?
Zeus
  • 1,496
  • 2
  • 24
  • 53
  • 5
    Before running the `plot` function, do `png("my_plot.png", 600, 600)` (the two numbers are the horizontal and vertical number of pixels). Then, after the `title` function, run `dev.off()`. You can save as a `pdf` in a similar way. Type `?pdf` and `?png` for help on these functions. – eipi10 May 26 '17 at 05:10
  • Thanks that works, I appreciate the help. Can you make your comment an answer so I can accept it? – Zeus May 26 '17 at 05:16
  • 2
    Thanks @Zeus. This question is probably a duplicate (see, for example, [here](https://stackoverflow.com/questions/7144118/how-to-save-a-plot-as-image-on-the-disk)), so there's no need to add an answer. – eipi10 May 26 '17 at 05:20
  • Ok, but I'll leave the question up in case the code helps someone else. – Zeus May 26 '17 at 05:27

1 Answers1

2

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.

c0bra
  • 1,031
  • 5
  • 22