Shiv Shiv - 9 days ago 6
R Question

How to save one graph in two destination files?

It saves "b.png", which is obvious. How to save both pdf and png?

pdf("a.pdf")
png("b.png")

plot(iris$Sepal.Length)

dev.off()
dev.off()

Answer

Try this example, we are assigning ggplot output into a variable then "plot" into files:

library(ggplot2)

myPlot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()

pdf("temp.pdf")
myPlot
dev.off()

png("temp.png")
myPlot
dev.off()