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:
What my image looks like:
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).