Learn R Programming

psych (version 1.0-92)

error.bars.by: Plot means and confidence intervals for multiple groups

Description

One of the many functions in R to plot means and confidence intervals. Meant mainly for demonstration purposes for showing the probabilty of replication from multiple samples. Can also be combined with such functions as boxplot to summarize distributions. Means and standard errors for each group are calculated using describe.by.

Usage

error.bars.by(x,group,by.var=FALSE,x.cat=TRUE,ylab =NULL,xlab=NULL,main=NULL,ylim= NULL, alpha=.05,sd=FALSE, labels=NULL,pos=NULL,arrow.len=.05,add=FALSE,bars=FALSE,within=FALSE,colors=c("black","blue","red"),lty=NULL,...)

Arguments

x
A data frame or matrix
group
A grouping variable
by.var
A different line for each group (default) or each variable
x.cat
Is the grouping variable categorical (TRUE) or continuous (FALSE
ylab
y label
xlab
x label
main
title for figure
ylim
if specified, the limits for the plot, otherwise based upon the data
alpha
alpha level of confidence interval. Default is 1- alpha =95% confidence interval
sd
sd=TRUE will plot Standard Deviations instead of standard errors
labels
X axis label
pos
where to place text: below, left, above, right
arrow.len
How long should the top of the error bars be?
add
add=FALSE, new plot, add=TRUE, just points and error bars
bars
Draw a barplot with error bars rather than a simple plot of the means
within
Should the s.e. be corrected by the correlation with the other variables?
colors
groups will be plotted in different colors (mod n.groups)
lty
line type may be specified in the case of not plotting by variables
...
other parameters to pass to the plot function, e.g., typ="b" to draw lines, lty="dashed" to draw dashed lines

Value

  • Graphic output showing the means + x% confidence intervals for each group. For ci=1.96, and normal data, this will be the 95% confidence region. For ci=1, the 68% confidence region.

    These confidence regions are based upon normal theory and do not take into account any skew in the variables. More accurate confidence intervals could be found by resampling.

Details

Drawing the mean +/- a confidence interval is a frequently used function when reporting experimental results. By default, the confidence interval is 1.96 standard errors.

This function was originally just a wrapper for error.bars but has been written to allow groups to be organized either as the x axis or as separate lines.

If desired, a barplot with error bars can be shown. Many find this type of plot to be uninformative (e.g., http://biostat.mc.vanderbilt.edu/DynamitePlots )

See Also

See Also as error.crosses, error.bars

Examples

Run this code
data(sat.act)
error.bars.by(sat.act[1:4],sat.act$gender)
error.bars.by(sat.act[5:6],sat.act$gender,bars=TRUE,labels=c("male","female"),main="SAT V and SAT Q by gender")  #draw a barplot

error.bars.by(sat.act[5:6],sat.act$education,bars=TRUE,xlab="Education",main="95 percent confidence limits of Sat V and Sat Q") 

error.bars.by(sat.act[5:6],sat.act$education,TRUE, xlab="Education")  #plot SAT V and SAT Q by education

Run the code above in your browser using DataLab