panel.errbars: Draw error bars in lattice plots

panel.errbarsR Documentation

Draw error bars in lattice plots

Description

This panel function allows to draw symbols with error bars for arbitrary groups of data points. Error bars will be drawn for groups of identical x values with optional subsetting by grouping or paneling variables. This function is very similar to 'panel.barplot' only with points instead of bars.

Usage

panel.errbars(
  x,
  y,
  groups = NULL,
  subscripts = NULL,
  error_margin = NULL,
  col = NULL,
  ewidth = 0.08,
  beside = FALSE,
  draw_points = FALSE,
  FUN_mean = function(x) mean(x, na.rm = TRUE),
  FUN_errb = function(x) sd(x, na.rm = TRUE),
  ...
)

Arguments

x, y

(numeric, character) variables to be plotted. The x variable is treated as a grouping variable, i.e. error bars are calculated between groups of unique x values.

groups

grouping variable passed down from xyplot (does not need to be specified)

subscripts

subscripts passed down from xyplot (does not need to be specified)

error_margin

optional input for error margins if errors are not to be computed, but supplied directly. Can be a vector of length(y), or a two-column matrix with first column representing lower and second column upper bounds for each point. Default is NULL. If supplied, FUN_errb is ignored.

col

(character) color (vector) to be used for points and lines. The default, NULL, uses colors supplied by the top level function.

ewidth

width of the error bar whiskers

beside

(logical) draw bars/points next to each other (default: FALSE)

draw_points

(logical) overlay original points over error barplot (default: FALSE)

FUN_mean

the function used to calculate group (x-variable) means

FUN_errb

the function used to calculate group (x-variable) errors

...

other arguments passed to the function

Examples

library(lattice)
data(mtcars)

# mean and stdev error bars are drawn for
# common x values
xyplot(mpg ~ factor(cyl), mtcars, 
  lwd = 2, pch = 19, cex = 1.5,
  panel = function(x, y, ...) {
    panel.errbars(x, y, ...)
  }
)

# using the same variable for x and grouping will
# result in typical lattice behavior
xyplot(mpg ~ factor(cyl), mtcars,
  groups = cyl, lwd = 2, pch = 19, cex = 1.5,
  panel = function(x, y, ...) {
    panel.errbars(x, y, ...)
  }
)

# we can also use different variables for the x var, grouping,
# and paneling.
xyplot(mpg ~ factor(cyl) | factor(vs), mtcars,
  groups = gear, lwd = 2, pch = 19, cex = 1.5,
  panel = function(x, y, ...) {
    panel.errbars(x, y, beside = TRUE, ...)
  }
)

# alternatively, means and error margins can be supplied directly. 
# In this case means are supplied as unique combinations
# of y and x while error_margin is a separate vector with same length as y.
mtcars_means <- data.frame(
  cyl = sort(unique(mtcars$cyl)),
  mpg = with(mtcars, tapply(mpg, cyl, mean)),
  stdev = with(mtcars, tapply(mpg, cyl, sd))
)

# you might have to adjust the yscale as it is determined from the
# range of the y variable only, ignoring the extension through error bars.
xyplot(mpg ~ factor(cyl), mtcars_means,
  error_margin = mtcars_means$stdev,
  ylim = c(9, 36), groups = cyl,
  lwd = 2, pch = 19, cex = 1.5,
  panel = function(x, y, ...) {
    panel.errbars(x, y, ...)
  }
)

# if you supply a two column matrix as the error_margin argument,
# error bars with different lower and upper bounds can be drawn
error_mat <- matrix(ncol = 2, 1:6)
xyplot(mpg ~ factor(cyl), mtcars_means,
 error_margin = error_mat,
 ylim = c(9, 36), groups = cyl,
 lwd = 2, pch = 19, cex = 1.5,
 panel = function(x, y, ...) {
   panel.errbars(x, y, ...)
 }
)

m-jahn/lattice-tools documentation built on Dec. 3, 2023, 11:14 p.m.