Save ggplot Graphics at Intended End-use Dimensions and Resolutions for Best Results

[This article was first published on R – William Doane, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Users of ggplot2 often run into issues with their saved graphics not having the expected resolution or font sizes.

By default, when using RStudio, ggsave() saves graphics at the last dimensions of the plot preview pane and at screen resolution. As a demo, try

library(ggplot2)

ggplot(mtcars, aes(hp, mpg)) + geom_point()

ggsave("graphic.png")

Then resize the plot preview pane and ggsave() again. And again. You’ll get three very different graphics as a result. Your font sizes are likely to be different from what you expect, if you drag those saved graphics into an MS Word or PowerPoint document, since the graphic will have to resize to fit the container (page, slide, or content area).

The solution is to be explicit about what your intended end-use is. I wrote a utility function to save a given graphic with proper dimensions and resolution for a number of use cases: for use in MS Word (U.S. Letter) documents at half- and full-page sizes, both portrait and landscape; and in both standard and widescreen slide formats. The code uses a default sent of dimensions and resolutions, but you can provide your own data.frame, if needed. U.S. users will recognize 8.5-2 as letter-width less 2 inches for standard 1 inch margins and 11-2 as letter-height less the same 2 inches for margins. Half page height is then (11-2)/2.

Now, your “10pt” axis labels will actually be 10pt when you place your graphics. Always use your graphics at 100% scale, otherwise all bets are off.

To leave a comment for the author, please follow the link and comment on their blog: R – William Doane.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)