1

I like to write code in Rmarkdown files and preview my plots inline. When it comes to saving my plots as images though, my actual image doesn't match the inline preview that I see in Rstudio.

Here is a reprex:

#```{r, fig.width=7, fig.asp=.5, dpi=600} # This is my chunk header
# Make the plot
mtcars %>% 
  ggplot() +
  geom_bar(aes(cyl)) +
  labs(title = "Title Title Title Title Title Title Title") +
  theme(plot.title = element_text(size = 30))
# Save most recent plot
ggsave("test plot.png", 
       width = 7, height = 7/2, units = "in", 
       device = ragg::agg_png,
       dpi = 600)

What I see inline RStudio:

enter image description here

What my image looks like:

enter image description here

Any idea what to do to make my preview match what I'm going to see on my ggsaved file or vice versa?

Additional context: If I use the {showtext} package then I can set the dpi with showtext::showtext_opts to a higher dpi than the default 96 that is used in the Rmkardown inline preview. I am trying to move away from {showtext} in favor of {ragg} but I am running into this issue. Given my past experience, my guess would be that the font resolution is not increasing with the rest of the plot elements when I ggsave but I'm not sure how to fix that. Also, I don't think setting the dpi in the code chunk increases the dpi of the preview, that just applies when I knit the document (which I am not worried about at the moment).

Phil
  • 7,287
  • 3
  • 36
  • 66
  • I think [this will help](https://www.christophenicault.com/post/understand_size_dimension_ggplot2/). – Kat Aug 13 '22 at 16:46
  • Thanks for linking this, @Kat! I've read through this article a few times and while it has been a huge help for understanding the topic surrounding my issue, unfortunately I don't think is solves my specific issue: I want to be able to preview my plot inline (whether on and Rmarkdown or Quatro document) so that the text in the preview I see matches the dimensions of the text on the file as exported. Put another way--I want the inline preview to be "what you see is what you get". Any thoughts on how I can achieve this? – Philli Phresh Aug 22 '22 at 00:43
  • I tried to run down several ideas, but none of these are particularly simple. This issue may be resolved or reduced by using `rel()` for any absolute sized parts of `ggplot`. (Some content is sized dynamically; text, for the most part, isn't relative.) I think the easiest way to do this is by using a widget-based graphing option (like HIghcharter or Plotly). The sizes are all dynamic (as far as I know). – Kat Aug 22 '22 at 18:59

0 Answers0