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

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.