ggplots resizing on save and how to 'optimize' elements before saving

Hi there,

I'm working on a "book" project (quarto-based) with loads of plots. My biggest problem is how to achieve consistency in the plot's appearance: after rendering the book, saved plots deviate from the look I had in the preview pane. E.g.,:

This is something expected given the text sizing/resolution problem discussed here: Understanding text size and resolution in ggplot2 | Christophe Nicault.

As I understand it, the chunk's options and pane size are not connected in any way, therefore preview is not representative in its appearance. So, my question is how can I link the size of the output plots (i.e., saved on render) and the size of the plots in the preview pane of Rstudio IDE consistent (in both overall size and aspect ratio)?

Thanks in advance.


I'm not sure if this will solve your issue, but if you save your plot with ggsave() and set the width and height to what Quarto uses in the output you should be able to preview the plots that way.

According to the Quarto documention the default html plot dimensions are 7x5 so after making your plot you could run ggsave("test_plot.png", width = 7, height = 5) to see what the plot will look like.

If that doesn't quite match what's in the book you could set the plot width and height globally in the YAML header (see link above) and match those dimensions when calling ggsave().

Thank you for your suggestion.
This is actually what I'm trying to avoid. Of course, ggsave() would be much faster than rendering the whole document. However, it's still a tedious process, especially when you dealing with lots of plots in very different types and shapes.

Another option is to use ggsave() to save all of your plots and then load the .png (for example) files into the quarto document with ![](). In this scenario, your quarto doc might look like the following:


I get the feeling this won't meet your needs but wanted to share it as an option just in case.

1 Like

FYI Look at FAQ: How to Format R Markdown Source to understand how to write code content with backticks

1 Like

Thanks @cderv! I've updated my comment.

1 Like

This topic was automatically closed 21 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.