0

How can I save an R plot that is only displayed in the "plots-pane" and not saved in the workspace?

Here simplified example on how I create my plot:

library(corrplot)
data(cars)
res.plot <- cor(cars)
par(mfrow=c(3,2))
for (i in 1:6) {
 corrplot(res.plot)
}

To save the resulting plot I can't use ggsave for example because I dont have the plot= input as the plot is not saved in the workspace.

This code doesn't work either:

data(cars)
res.plot <- cor(cars)
par(mfrow=c(3,2))
svg(filename=paste0("Rplot_",Sys.time(),".svg"), width = 8.27,height = 11.69)
for (i in 1:6) {
  corrplot(res.plot)
}
dev.off() 

The only solution I found so far is to use the "Export" dialog of R studio but as I need to export a lot of plots it would be a way faster to save them using code.

Any ideas?

zx8754
  • 52,746
  • 12
  • 114
  • 209
brauer-t
  • 45
  • 7
  • I'd suggest trying it first just for one plot, and then after you understood the syntax implement the loop... – dario Oct 22 '21 at 09:26
  • It works perfectly fine saving one plot... – brauer-t Oct 22 '21 at 09:27
  • I've never used `svg` but I'd suggest to move the `svg` to `dev.off` lines inside the loop, change the filename code so it uses the iterator and not `Sys.time` (which only goes down to seconds - it would overwrite the plots) – dario Oct 22 '21 at 09:35
  • Does this answer your question? [Save multiple ggplots using a for loop](https://stackoverflow.com/questions/26034177/save-multiple-ggplots-using-a-for-loop) – manro Oct 22 '21 at 09:35
  • @manro OP is specifically talking about "base" plots... – dario Oct 22 '21 at 09:36
  • 1
    And a general comment: 1. When using non "base" functions, please specify the `library` call or use namespaces (where does the `corrpolot` come from?) 2. Information like *"This code doesn't work either:"* is not very useful. At least specify in what way it did not work or better show the exact error message. – dario Oct 22 '21 at 09:40
  • @dario this: https://stackoverflow.com/questions/32048305/plot-graphs-in-r-by-loop-and-save-it-like-jpeg ? – manro Oct 22 '21 at 09:40
  • @manro Much better duplicate ;) Any of the answers there work, and so does OPs code (after some fixing). The crucial part is moving the saving part **inside** the for loop – dario Oct 22 '21 at 09:44
  • By the way, if we are using Windows, we can't have filenames with colons `":"` - `"Rplot_2021-10-22 10:56:57.svg"` – zx8754 Oct 22 '21 at 09:57
  • 1
    I said as general comment. But yes, the issue was order of par, it should come after graphic device svg is activated. – zx8754 Oct 22 '21 at 09:59

2 Answers2

1

You are trying to save a loop. Try this:

library(corrplot)
data(cars)
res.plot <- cor(cars)
par(mfrow=c(3,2))

for (i in 1:6) {
  svg(filename=paste0("Rplot_",Sys.time(),".svg"), width = 8.27,height =   11.69)
  corrplot(res.plot)
  dev.off() 
}

Or all plots in the same panel:

library(corrplot)
res.plot <- cor(cars)
svg(filename=paste0("Rplot_",Sys.time(),".svg"), width = 8.27,height = 11.69)
par(mfrow=c(3,2))
for (i in 1:6) {
  corrplot(res.plot)
}
dev.off() 
Lacococha
  • 128
  • 8
0

I found a differnt approach that also does what I want:

library(corrplot)
data(cars)
res.plot <- cor(cars)
par(mfrow=c(3,2))
for (i in 1:6) {
 corrplot(res.plot)
}
dev.copy(svg,'myplot.svg',width = 8.27,height = 11.69)
dev.off()
brauer-t
  • 45
  • 7