panel.barplot | R Documentation |
This panel function allows to draw barplots 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.errbars' only with bars instead of points.
panel.barplot(
x,
y,
groups = NULL,
subscripts = NULL,
error_margin = NULL,
col = NULL,
ewidth = NULL,
fill = NULL,
fill_alpha = 0.5,
twoway = FALSE,
beside = FALSE,
draw_points = FALSE,
origin = NULL,
FUN_mean = function(x) mean(x, na.rm = TRUE),
FUN_errb = function(x) sd(x, na.rm = TRUE),
...
)
x, y |
(numeric, character) variables to be plotted. The x variable is treated as a grouping varibale, 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 |
(numeric) width of the error bars and whiskers |
fill |
(numeric, character) optionally specify custom fill for the bars (default NULL, takes colors from col) |
fill_alpha |
(numeric) scalar setting the transparency of the bars (default: 0.5) |
twoway |
(logical) draw both upper and lower boundaries (default: FALSE) |
beside |
(logical) draw bars/points next to each other (default: FALSE) |
draw_points |
(logical) overlay original points over barplot (default: FALSE) |
origin |
(numeric) Y coordinate where bars should originate (default NULL means bottom axis) |
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 |
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.barplot(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,
panel = function(x, y, ...) {
panel.barplot(x, y, ...)
}
)
# we can also use different variables for the x var, grouping,
# and paneling. As a visual control that error bars are drawn
# for the correct groups we overlay the single data points.
xyplot(mpg ~ factor(cyl) | factor(vs), mtcars,
groups = gear, lwd = 2, auto.key = list(columns = 3),
panel = function(x, y, ...) {
panel.barplot(x, y, beside = TRUE, draw_points = 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 y-scale 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.barplot(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, twoway = TRUE, fill = NA,
ylim = c(9, 36), groups = cyl,
lwd = 2, pch = 19, cex = 1.5,
panel = function(x, y, ...) {
panel.barplot(x, y, ...)
}
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.