set_labels: Set the Labels in a List of ggplot Objects

View source: R/set_labels.R

set_labelsR Documentation

Set the Labels in a List of ggplot Objects

Description

Set the Labels in a List of ggplot Objects

Usage

set_labels(
  plots,
  ...,
  indices = seq_along(plots),
  label_data = NULL,
  action = c("replace", "append", "prepend")[1],
  sep = " "
)

Arguments

plots

list of ggplot objects as returned by ggplot

...

name-value pairs as given to labs. Possible names are e.g. "x", "title", "subtitle", "caption". The values are vectors of character that are recycled to the length of indices. They are used as labels given to each plot or to the plots selected by their indices.

indices

indices of the plots to which the label is to be given. By default the label is given to all plots

label_data

data frame containing the different label types in different columns. If given and not NULL arguments in ... are ignored.

action

one of "replace" (replace the existing label), "append" (append to the existing label), "prepend" (prepend to the existing label).

sep

separator to be used when append is one of "append", "prepend".

Examples

p <- example_plot_2()

plots <- list(p, p, p, p)

plots_1 <- set_labels(
  plots, title = c("Title A", "Title B", "Title C", "Title D"),
  subtitle = "same subtitle", x = c("x label one", "x label two")
)

plots_2 <- set_labels(
  plots, title = c("(A)", "(B)", "(C)", "(D)"),
  subtitle = "(always the same)", x = c("(one)", "(two)"),
  action = "append"
)

label_data <- data.frame(title = "Titles set with label_data",
  subtitle = sprintf("Plot %d", seq_along(plots))
)
plots_3 <- set_labels(plots, label_data = label_data)

do.call(gridExtra::grid.arrange, plots_1)
do.call(gridExtra::grid.arrange, plots_2)
do.call(gridExtra::grid.arrange, plots_3)


KWB-R/kwb.plot documentation built on Oct. 2, 2023, 10:16 p.m.