fig_start: Starts a PDF or PNG figure with better defaults

View source: R/fig_start.R

fig_startR Documentation

Starts a PDF or PNG figure with better defaults

Description

Creates a PDF or PNG figure with dimensions that default approximately to a single column on a two-column paper, bolds text, reduces space between labels and the figure, and sets other improved defaults for margins. The goal is to create publication- and presentation-quality figures with few keystrokes. PNG version is by default high resolution (600 ppi) and scales dimensions according to this resolution to get images that match their PDF counterparts as much as possible.

Usage

fig_start(
  name,
  width = 3,
  height = 3,
  bg = "white",
  mar_b = 3,
  mar_l = 3,
  mar_t = 0,
  mar_r = 0,
  mar = NULL,
  mar_pad = 0.2,
  png = FALSE,
  png_res = 600,
  verbose = TRUE
)

Arguments

name

The output path, excluding extension (.pdf or .png is automatically added)

width

Figure width, in inches (default 3). If png = TRUE, this value gets multiplied by png_res to result in the final width in pixels.

height

Figure height, in inches (default 3). If png = TRUE, this value gets multiplied by png_res to result in the final height in pixels.

bg

Figure background color (default "white")

mar_b

Bottom margin size, in margin lines (default 3). mar_pad gets added to this value to produce the final margins.

mar_l

Left margin size, in margin lines (default 3). mar_pad gets added to this value to produce the final margins.

mar_t

Top margin size, in margin lines (default 0). mar_pad gets added to this value to produce the final margins.

mar_r

Right margin size, in margin lines (default 0). mar_pad gets added to this value to produce the final margins.

mar

A vector with four elements (in the format graphics::par() expects, as an alternative specification. mar_pad gets added to this vector to produce the final margins. The individual margins above are used if mar = NULL (default).

mar_pad

Margin padding applied to all margins.

png

If TRUE, output is a PNG file (default FALSE results in a PDF file).

png_res

Resolution of PNG image in ppi. In addition to setting the resolution internally in the file, this scales the widthand height values (default in inches) to the final pixel values of the output. Ignored if png = FALSE.

verbose

If TRUE (default), prints a message indicating the path of the file that was created.

The PDF or PNG is created via grDevices::pdf() or grDevices::png(), respectively. Fonts and lines are bolded by setting all of the following parameters to 2 via graphics::par(): font, font.main, font.sub, font.lab, font.axis, and lwd. Similarly, titles and axis labels are placed closer to the figure by setting mgp = c(2, 0.5, 0), and the approximate number of tickmarks is reduced by setting lab = c(3, 3, 7).

Examples

### simple square figure with nice margins
# fig_start('my_plot')
# plot(1:10)
# fig_end()

### example with a title
### increase top margin only
# fig_start('my_plot', mar_t = 1.5)
# plot(1:10, main = 'My Title!')
# fig_end()


OchoaLab/ochoalabtools documentation built on July 28, 2024, 12:37 p.m.