Generate width and height of a device based upon a given number of plots per page

Generate width and height in pixels of a device based upon a given number of plots per page

get_device_size(
  nplots = 1,
  layout = "portrait",
  units = c("in", "cm", "mm", "px"),
  dpi = 300
)

get_device_size_pixel(nplots = 1, layout = "portrait", dpi = 300)

Arguments

nplots

Number of plots per page.

layout

Either 'portrait' or 'landscape'.

units

One of ('in', 'cm', 'mm', 'px'). If not supplied, defaults to 'in'.

dpi

Plot resolution in dot per inch.

Value

A named numeric vector of width and height

A named numeric vector of width and height

Examples

library(ggplot2)

dims <- get_device_size(nplots = 4,
                        units = 'in',
                        dpi = 300)

plot <- ggplot(mpg, aes(class, hwy)) +
  geom_point()

ggsave(filename = 'plot.png',
       plot = plot,
       path = tempdir(),
       width = dims['width'],
       height = dims['height'],
       units = 'in',
       dpi = 300)