Learn R Programming

itsadug (version 2.0)

errorBars: Add error bars to a plot.

Description

Add vertical error bars.

Usage

errorBars(x, mean, ci, ci.l = NULL, minmax = NULL, horiz = FALSE, ...)

Arguments

x
Vector with x-values (or y-values in case horiz=TRUE).
mean
Vector with means.
ci
Vector with errors or confidence bands, e.g. SE values. If ci.l is not defined, the errors are assumed to be symmetric. If ci.l is defined, than ci is assumed to be the upper confidence band. Note that the c
ci.l
Optional: vector with error to calculate lower confidence band.
minmax
Optional argument, vector with two values indicating the minimum and maximum value for the error bars. If NULL (default) the error bars are not corrected.
horiz
Logical: whether or not to plot horizontal error bars. Defaults to FALSE (plotting vertical error bars).
...
Optional graphical parameters (see par).

See Also

Other Functions for plotting: addInterval, add_bars, alphaPalette, alpha, check_normaldist, color_contour, dotplot_error, drawArrows, emptyPlot, fadeRug, fill_area, getCoords, getFigCoords, getProps, gradientLegend, marginDensityPlot, plot_error, plot_image, plotsurface, rug_model

Examples

Run this code
data(simdat)
subj.dat <- with(simdat, 
    aggregate(Y, list(Group=Group, Condition=Condition, Subject=Subject), 
    mean))
avg <- with(subj.dat, tapply(x, list(Group, Condition), mean))
ses  <- with(subj.dat, tapply(x, list(Group, Condition), se))

# barplot:
b <- barplot(avg, beside=TRUE, col=c("gray", "forestgreen"),
    main="Average Y", xlab="Condition",
    legend.text=c("Adults", "Children"), args.legend=list(x="topleft"))
errorBars(b, avg, 1.96*ses, xpd=TRUE, length=.05)

# line plot:
miny  <- with(subj.dat, tapply(x, list(Group, Condition), min))
maxy  <- with(subj.dat, tapply(x, list(Group, Condition), max))
emptyPlot(c(-1,4), range(avg), 
    main="Average Y", xlab="Condition")
group <- "Children"
errorBars(-1:4, avg[group,], maxy[group,], miny[group,], length=.05, 
    col="forestgreen", lwd=2)
points(-1:4, avg[group,], pch=21, type='o', lty=3, lwd=2,
    col="forestgreen", bg="white", xpd=TRUE)
# also horizontal bars possible:
errorBars(10, 1, 1.2, horiz=TRUE)

Run the code above in your browser using DataLab