lt_errorbar: Draw an inline error-bar plot in a column

View source: R/tab.R

lt_errorbarR Documentation

Draw an inline error-bar plot in a column

Description

Render a numeric column's cells as a small inline SVG: a point at the estimate and a horizontal bar (with end caps) from the lower to the upper bound, drawn on a scale shared across the column so rows are comparable at a glance (handy for a forest plot of effect sizes with confidence intervals). It is lightweight: only the numbers travel to the client, and the SVG is drawn in the browser — so for an interactive table (lt_interactive()) only the rows on the current page are drawn.

Usage

lt_errorbar(
  x,
  ...,
  limits = NULL,
  ref = NULL,
  into = NULL,
  width = 160,
  height = NULL,
  hide = TRUE,
  axis = FALSE,
  color = FALSE,
  labels = NULL
)

Arguments

x

An lt() object.

...

One or more error-bar series, each naming the point estimate, the lower bound, and the upper bound, in that order: either a two-sided formula value ~ lower + upper (estimate on the left, bounds on the right) or a length-3 character vector / integer positions c(value, lower, upper). Give several to stack several series per cell. The plot is drawn in the first series' value column's cells, or into into when given.

limits

Numeric c(min, max) for the shared horizontal scale. Defaults to the range of all the columns' finite values. Values outside the scale are clamped to the edges.

ref

Optional value at which to draw a vertical reference line (e.g. 0 for a risk difference, 1 for an odds ratio).

into

Optional name of a dedicated column to draw the plot into, instead of the first series' value column. The series columns (...) are still read for their values and stay visible unless hide drops them, so you can show every value column as text and a plot in its own column. A column of this name is created when it does not already exist (added as a new, extra column – use lt_move() to position it). When NULL (default), the plot is drawn into the first series' value column.

width, height

Pixel size of each cell's SVG. width may instead be a relative CSS width such as "100%" or "80%", in which case the plot auto-fits its column: it takes that fraction of the column's width and is redrawn when the width changes (e.g. the window or a column is resized), never shrinking below the default pixel width. Use list(min =, width =) to set that minimum explicitly, e.g. list(min = 200, width = "100%"). height defaults to a size that fits the number of series (taller for more).

hide

If TRUE (default), the columns the plot reads are hidden, since their values are drawn into it: all but the one holding the plot, or – when into names a separate target column – all of them. Set to FALSE to keep them visible.

axis

Draw a shared horizontal axis (a baseline with tick marks and labels at "nice" round values) in the table footer under the plot column, plus faint vertical gridlines inside each cell at the same tick positions to help read the bars. TRUE draws the axis; a character string draws the axis with that string as a caption below it (e.g. "Risk difference"); FALSE (default) draws no axis.

color

Series colors. FALSE (default) draws every series in one default color; TRUE assigns colors from the current palette (grDevices::palette()); a character vector of CSS colors sets them explicitly (recycled to the number of series). When colored, a legend is drawn below the plot in the footer.

labels

Legend labels, one per series, used when colored. Defaults to the series' value-column names.

Details

Several series can share one cell: pass one triple per series and each row shows one point-and-bar per series, stacked vertically on the same shared scale (e.g. a risk difference for each of several treatment arms). Color the series and a legend is drawn in the footer, as for lt_dotplot().

Value

x with the error-bar column recorded.

Examples

d = data.frame(
  term = c("A", "B", "C"), est = c(0.2, -0.1, 0.4),
  lo = c(0.0, -0.3, 0.1), hi = c(0.4, 0.1, 0.7)
)
# a two-sided formula names the estimate (LHS) and the bounds (RHS)
lt(d) |> lt_errorbar(est ~ lo + hi, ref = 0)
# keep the bounds columns visible and add a labeled axis
lt(d) |> lt_errorbar(est ~ lo + hi, hide = FALSE, axis = "Effect")
# draw into a dedicated column, keeping every value column visible as text
lt(d) |> lt_errorbar(est ~ lo + hi, into = "forest", hide = FALSE)
# auto-fit the plot to its column (grows/shrinks as the column is resized)
lt(d) |> lt_errorbar(est ~ lo + hi, into = "forest", width = "100%")
# several series stacked in each cell, colored with a footer legend
d2 = data.frame(
  term = c("A", "B"), e1 = c(0.2, 0.4), l1 = c(0, 0.2), u1 = c(0.4, 0.6),
  e2 = c(-0.1, 0.1), l2 = c(-0.3, -0.1), u2 = c(0.1, 0.3)
)
lt(d2) |> lt_errorbar(
  e1 ~ l1 + u1, e2 ~ l2 + u2, ref = 0, color = TRUE,
  labels = c("Drug A", "Drug B")
)

lt documentation built on Oct. 17, 2026, 9:06 a.m.