panel.annotate: Draw summary text labels in lattice plots

View source: R/panel_annotate.R

panel.annotateR Documentation

Draw summary text labels in lattice plots

Description

This panel function allows to draw text labels such as the mean on arbitrary groups of data points. Text labels will be drawn for groups of identical x values with optional subsetting by grouping or paneling variables. This function complements 'panel.errbars()' and 'panel.barplot' as it supports drawing labels beside each other for different groups.

Usage

panel.annotate(
  x,
  y,
  groups = NULL,
  subscripts,
  FUN_mean = mean,
  digits = NULL,
  offset = NULL,
  ewidth = 0.08,
  beside = FALSE,
  col = NULL,
  group.number = NULL,
  ...
)

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).

FUN_mean

(function) the function that used to calculate summary statistics.

digits

(numeric) number of digits for numeric text labels.

offset

(numeric) offset in native plot units used to adjust the position of text labels. If offset is length 1, only the vertical position is adjusted; if it is length 2 the horizontal and vertical position is adjusted.

ewidth

(numeric) width of the error bar whiskers.

beside

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

col

(character) color (vector) to be used for (groups of) labels. The default, NULL, uses colors supplied by the top level function.

group.number

internal parameter only used when function is called as panel.groups argument from within panel.superpose. Does not need to be specified manually

...

other arguments passed to the function

Examples

library(lattice)
data(mtcars)

# annotate mean values 
xyplot(mpg ~ factor(cyl) | factor(vs), mtcars,
  lwd = 2, pch = 19, offset = 3, digits = 1,
  panel = function(x, y, ...) {
    panel.errbars(x, y, ...)
    panel.annotate(x, y, ...)
  }
)

# works also with grouping variable. Takes the same arguments
# "beside" and "ewidth" as panel.errbars() and panel.barplot()
# to plot labels for different groups aside of each other
xyplot(mpg ~ factor(cyl) | factor(vs), mtcars,
  lwd = 2, pch = 19, groups = gear, digits = 1,
  offset = 3, beside = TRUE,
  panel = function(x, y, ...) {
    panel.errbars(x, y, ...)
    panel.annotate(x, y, ...)
  }
)

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