Save plot exactly as previewed in the "Plots" panel

I know that a very similar question already exists here, but the provided answer did not work for me.

When I want to save a plot, this is my usual workflow: I generate a plot and adjust the size of the plot in the "Plots" Panel of RStudio until I am satisfied. I then call dev.size() to get the exact size.
Afterwards, I save the plot with ggsave(...,dpi=300) and specify the previously determined width and height. The problem is, that after saving, the plot looks completely different, especially the text sizes.

If I use the "Export" option from RStudio the plot looks exactly as it does in the preview, but the quality is quite bad and doing this manually is tedious.

Here is the picture, that hopefully illustrates what I mean:

The code I use to save the plot looks like this:

library(tidyverse)
library(patchwork)
theme_custom <- function(){
  theme(plot.title = element_text(face = "bold",
                                  size = 15, hjust = 0.5),
        axis.title = element_text(face = "bold",size = 15),
        axis.title.y = element_text(angle=90,vjust =2),
        axis.title.x = element_text(vjust = -0.2),
        axis.text = element_text(), 
        axis.line = element_line(colour="black"),
        axis.ticks = element_line(),
        panel.grid.major = element_line(colour="#f0f0f0"),
        panel.grid.minor = element_blank()
        )
  }


p1 <- ggplot(mtcars, aes(x = mpg, y = wt)) + 
  geom_point() + 
  facet_grid(vs + am ~ gear, margins = "vs") +
  theme_custom()+
  ggtitle("a random title")

p2 <- ggplot(mtcars, aes(x = mpg, y = wt)) + 
  geom_point() + 
  facet_grid(vs + am ~ gear, margins = "vs") +
  theme_custom()+
  ggtitle("a random title")

p3 <- ggplot(mtcars, aes(x = mpg, y = wt)) + 
  geom_point() + 
  facet_grid(vs + am ~ gear, margins = "vs") +
  theme_custom()+
  ggtitle("a random title")


# NOTE: I use patchwork here to combine the plots, but the problem is the same if I use a single plot..
combined <- wrap_plots(p1, p2, p3, nrow = 1) +
  plot_annotation(tag_levels = "A") & 
  theme(plot.tag = element_text(size = 15, face="bold"))

combined

ggsave("plot.png")

I would love to know, if there is an option to "programmatically" save a plot which exactly recreates the "Plots" preview in high quality.

I tried this on a windows and on a linux machine and the result was the same in both cases.

Thanks a lot in advance!

This is semi-programatic; based on the premise being your interactive manipulation of rstudios plot window. to reproduce at least what you produce by so doing; try :

rstudioapi::savePlotAsImage(file = "myplot.png",width = dev.size(units="px")[[1]],height=dev.size(units="px")[[2]])

Thank you very much for your comment. This is already very close to what I am looking for. But if I understand it correctly, this function mimics the "Export" function above the "plots" panel. Consequently, the resolution of the plot that I save to disk is quite poor. Is it possible to increase this resolution? Similarity to the dpi parameter of the ggsave function?

1 Like

I usually set the dimension and resolution of my ggplot in ggsave; and only then will I do the styling in ggplot. So I don't look at the preview, but directly at the exported file. This way the result is exactly what I want.

Yeah, that is also what I will do now. But was just wondering why I see such a gigantic discrepancy between the previews and the file on disk.

Because of the combination of resolution and dimension.
The ones in the preview can be different than the ones you set in ggsave, so there can be significant discrepancies. Using these settings gets you close to what you see in the preview (in my experience).

ggsave("Rplot.png", width = 5000, height = 3500, units = "px", dpi = 800)
1 Like

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.