Insert images and inline plots into tinytable objects

Description

The plot_tt() function allows for the insertion of images and inline plots into tinytable objects. This function can handle both local and web-based images.

Usage

plot_tt(
  x,
  i = NULL,
  j = NULL,
  fun = NULL,
  data = NULL,
  color = "black",
  xlim = NULL,
  height = 1,
  height_plot = 400,
  width_plot = 1200,
  images = NULL,
  sprintf = "%s",
  assets = "tinytable_assets",
  ...
)

tt_plot(
  x,
  i = NULL,
  j = NULL,
  fun = NULL,
  data = NULL,
  color = "black",
  xlim = NULL,
  height = 1,
  height_plot = 400,
  width_plot = 1200,
  images = NULL,
  sprintf = "%s",
  assets = "tinytable_assets",
  ...
)

Arguments

x A tinytable object.
i Integer vector, the row indices where images are to be inserted. If NULL, images will be inserted in all rows.
j Integer vector, the column indices where images are to be inserted. If NULL, images will be inserted in all columns.
fun

String or function to generate inline plots.

  • Built-in plot types (strings):

    • “histogram”: Creates histograms from numeric vectors. Accepts color argument.

    • “density”: Creates density plots from numeric vectors. Accepts color argument.

    • “bar”: Creates horizontal bar charts from single numeric values. Accepts color and xlim arguments.

    • “barpct”: Creates horizontal percentage bar charts from single numeric values between 0 and 1. Accepts color and background arguments.

    • “line”: Creates line plots from data frames with x and y columns. Accepts color and xlim arguments.

  • Custom functions:

    • Functions that return ggplot2 objects.

    • Functions that return another function which generates a base R plot, ex: function(x) {function() hist(x)}

    • Note: When using custom ggplot2 functions that return plots with text elements, the text size will normally need to be adjusted because the plot is inserted as a very small image in the table. Text sizes of 1 or smaller often work well (e.g., theme(text = element_text(size = 1))).

  • See the tutorial on the tinytable website for more information.

data a list of data frames or vectors to be used by the plotting functions in fun.
color string Name of color to use for inline plots (passed to the col argument base graphics plots in R).
xlim Numeric vector of length 2.
height Numeric, the height of the images in the table in em units.
height_plot Numeric, the height of generated plot images in pixels (default: 400).
width_plot Numeric, the width of generated plot images in pixels (default: 1200).
images Character vector, the paths to the images to be inserted. Paths are relative to the main table file or Quarto (Rmarkdown) document.
sprintf Character string, a sprintf format string to format the generated cell content. Default is "%s" which displays the content as-is. Use this to wrap images or plots in custom markup.
assets Path to the directory where generated assets are stored. This path is relative to the location where a table is saved.
Extra arguments are passed to the function in fun. Important: Custom plotting functions must always have as an argument.

Details

The plot_tt() can insert images and inline plots into tables.

Value

A modified tinytable object with images or plots inserted.

Examples

library("tinytable")

# Built-in plot types
plot_data <- list(mtcars$mpg, mtcars$hp, mtcars$qsec)

dat <- data.frame(
  Variables = c("mpg", "hp", "qsec"),
  Histogram = "",
  Density = "",
  Bar = "",
  BarPct = "",
  Line = ""
)

# Random data for sparklines
lines <- lapply(1:3, \(x) data.frame(x = 1:10, y = rnorm(10)))

# Percentage data (values between 0 and 1)
pct_data <- list(0.65, 0.82, 0.41)

tt(dat) |>
  plot_tt(j = 2, fun = "histogram", data = plot_data) |>
  plot_tt(j = 3, fun = "density", data = plot_data, color = "darkgreen") |>
  plot_tt(j = 4, fun = "bar", data = list(2, 3, 6), color = "orange") |>
  plot_tt(j = 5, fun = "barpct", data = pct_data, color = "steelblue") |>
  plot_tt(j = 6, fun = "line", data = lines, color = "blue") |>
  style_tt(j = 2:6, align = "c")

# Custom function example (must have ... argument)
custom_hist <- function(d, ...) {
  function() hist(d, axes = FALSE, ann = FALSE, col = "lightblue")
}

tt(data.frame(Variables = "mpg", Histogram = "")) |>
  plot_tt(j = 2, fun = custom_hist, data = list(mtcars$mpg))