spec_plot: Helper functions to generate inline sparklines

View source: R/mini_plots.R

spec_plotR Documentation

Helper functions to generate inline sparklines

Description

These functions helps you quickly generate sets of sparkline style plots using base R plotting system. Currently, we support histogram, boxplot, line, scatter and pointrange plots. You can use them together with column_spec to generate inline plot in tables. By default, this function will save images in a folder called "kableExtra" and return the address of the file.

Usage

spec_plot(
  x,
  y = NULL,
  width = 200,
  height = 50,
  res = 300,
  same_lim = TRUE,
  xlim = NULL,
  ylim = NULL,
  xaxt = "n",
  yaxt = "n",
  ann = FALSE,
  col = "lightgray",
  border = NULL,
  frame.plot = FALSE,
  lwd = 2,
  pch = ".",
  cex = 2,
  type = "l",
  polymin = NA,
  minmax = list(pch = ".", cex = cex, col = "red"),
  min = minmax,
  max = minmax,
  dir = if (is_latex()) rmd_files_dir() else tempdir(),
  file = NULL,
  file_type = if (is_latex()) "pdf" else svglite::svglite,
  ...
)

Arguments

x, y

Vector of values or List of vectors of values. y is optional.

width

The width of the plot in pixel

height

The height of the plot in pixel

res

The resolution of the plot. Default is 300.

same_lim

T/F. If x is a list of vectors, should all the plots be plotted in the same range? Default is True.

xlim, ylim

Manually specify plotting range in the form of c(0, 10).

xaxt

On/Off for xaxis text

yaxt

On/Off for yaxis text

ann

On/Off for annotations (titles and axis titles)

col

Color for the fill of the histogram bar/boxplot box.

border

Color for the border.

frame.plot

On/Off for surrounding box (spec_plot only). Default is False.

lwd

Line width for spec_plot; within spec_plot, the minmax argument defaults to use this value for cex for points. Default is 2.

pch, cex

Shape and size for points (if type is other than "l").

type

Passed to plot, often one of "l", "p", or "b", see graphics::plot.default() for more details. Ignored when polymin is not NA.

polymin

Special argument that converts a "line" to a polygon, where the flat portion is this value, and the other side of the polygon is the 'y' value ('x' if no 'y' provided). If NA (the default), then this is ignored; otherwise if this is numeric then a polygon is created (and 'type' is ignored). Note that if polymin is in the middle of the 'y' values, it will generate up/down polygons around this value.

minmax, min, max

Arguments passed to points to highlight minimum and maximum values in spec_plot. If min or max are NULL, they default to the value of minmax. Set to an empty list() to disable.

dir

Directory of where the images will be saved.

file

File name. If not provided, a random name will be used

file_type

Graphic device. Can be character (e.g., "pdf") or a graphics device function (grDevices::pdf). This defaults to "pdf" if the rendering is in LaTeX and "svg" otherwise.

...

extra parameters passing to plot


kableExtra documentation built on June 22, 2024, 10:59 a.m.