prener includes a number of functions that I use in conjunction with ggplot2. The package includes a tool for implementing points as a unit of measure in ggplot2 as well as standardized design and export options.

Sizing Plots

One issue I address is that ggsave() does not accept measurements for width and height in points, but Keynote lays slides out using points as the unit of measurement. The cp_points() function will convert points to inches, centimeters, or millimeters (the units used by ggsave()).

> library(prener)
> library(ggplot2)

> cp_points(200, units = "in")
[1] 2.77778 

> cp_points(200, units = "cm")
[1] 7.05556

> cp_points(200, units = "mm")
[1] 70.5556

These functions can be embedded directly in ggsave() calls:

p <- ggplot(mpg, aes(hwy)) + geom_histogram()

ggsave("hwy", p, device = "png", 
       width = cp_points(500, units = "mm"), 
       height = cp_points(375, units = "mm"), 
       units = "mm", dpi = 300)

Standardized Theme

I have tweaked the excellent theme_fivethiryeight ggplot2 theme from the ggthemes package to provide a consistent appearance for plots used with my Sequoia Keynote template. I have changed the background color for the plot, the panel, and the legend to match the background of my slides. I've also re-positioned the legend, since I often teach in classrooms where bottom legends are not visible to the entire class. Finally, I've changed the base font-size to one that stands out easily on slides.

p <- ggplot(mpg, aes(hwy)) + 
         geom_histogram() + 
         cp_sequoiaTheme()

Standardized Export Options

The Keynote templates that I use for teaching and presentations have three standard sizes that I create plots for - small (960 x 540 points), medium (960 x 630 points), and large (1024 by 768 points). The cp_plotSave() function will export ggplot2 plots at these dimensions. By default, these are done at 300 dots per inch but that can also be adjusted:

p <- ggplot(mpg, aes(hwy)) + 
         geom_histogram()

cp_plotSave("hwy", p, preset = "sm")
cp_plotSave("hwy", p, preset = "med")
cp_plotSave("hwy", p, preset = "lg", dpi = 500)

If no device is specified, plots will be exported as .png files. Other output options may be specified using device:

cp_plotSave("hwy", p, device = "pdf", preset = "lg", dpi = 500)


chris-prener/prener documentation built on May 17, 2019, 9:12 a.m.