psych (version 2.1.9)

error.bars: Plot means and confidence intervals


One of the many functions in R to plot means and confidence intervals. Can be done using barplots if desired. Can also be combined with such functions as boxplot to summarize distributions. Means and standard errors are calculated from the raw data using describe. Alternatively, plots of means +/- one standard deviation may be drawn.


error.bars(x,stats=NULL,data=NULL,group=NULL, ylab = "Dependent Variable",
xlab="Independent Variable", main=NULL,eyes=TRUE, ylim = NULL, xlim=NULL,alpha=.05, 
sd=FALSE, labels = NULL, pos = NULL,  arrow.len = 0.05,arrow.col="black",
 add = FALSE,bars=FALSE,within=FALSE, col=c("black","blue","red"),density=-10,



A data frame or matrix of raw data OR, a formula of the form DV ~ IV. If formula input is specified, is called.


A table of frequencies


Alternatively, a data.frame of descriptive stats from (e.g., describe). if specified, the means, sd, n and perhaps of se of a data set to be plotted


If using formula input, specify the object where the data may found


If not null, then do syntax


y label


x label


title for figure


if specified, the limits for the plot, otherwise based upon the data


if specified, the x limits for the plot, otherwise c(.5,nvar + .5)


should 'cats eyes' plots be drawn


alpha level of confidence interval -- defaults to 95% confidence interval


if TRUE, draw one standard deviation instead of standard errors at the alpha level


X axis label


where to place text: below, left, above, right


How long should the top of the error bars be?


What color should the error bars be?


add=FALSE, new plot, add=TRUE, just points and error bars


bars=TRUE will draw a bar graph if you really want to do that


should the error variance of a variable be corrected by 1-SMC?


color(s) of the catseyes. Defaults to blue.


If negative, solid colors, if positive, how many lines to draw


Percentages are based upon the row totals (default) column totals, or grand total of the data Table


If raw is FALSE, display the graphs in terms of probability, raw TRUE displays the data in terms of raw counts

other parameters to pass to the plot function, e.g., typ="b" to draw lines, lty="dashed" to draw dashed lines


Graphic output showing the means + x

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.

The function will return (invisibly) the cell means and standard errors.


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 of the t-distribution.

If within=TRUE, the error bars are corrected for the correlation with the other variables by reducing the variance by a factor of (1-smc). This allows for comparisons between variables.

The error bars are normally calculated from the data using the describe function. If, alternatively, a matrix of statistics is provided with column headings of values, means, and se, then those values will be used for the plot (using the stats option). If n is included in the matrix of statistics, then the distribution is drawn for a t distribution for n-1 df. If n is omitted (NULL) or is NA, then the distribution will be a normal distribution.

If sd is TRUE, then the error bars will represent one standard deviation from the mean rather than be a function of alpha and the standard errors.

See the last two examples for the case of plotting data with statistics from another function.

Alternatively, will take tabulated data and convert to either row, column or overall percentages, and then plot these as percentages with the equivalent standard error (based upon sqrt(pq/N)).

In August, 2018, the functionality of error.bars and were combined so that if groups are specified, then the error bars are done by group. Furthermore, if the x variable is a formula of the form DV ~ IV, then is called to do the plotting.

See Also

error.crosses for two way error bars, for error bars for different groups as well as error.dots.

The is useful for showing the results of one or two way ANOVAs in that it will display means and CIs for one or more DVs for one or two IVs.

In addition, as pointed out by Jim Lemon on the R-help news group, error bars or confidence intervals may be drawn using

function package
bar.err (agricolae)
plotCI (gplots)
xYplot (Hmisc)
dispersion (plotrix)
plotCI (plotrix)

For advice why not to draw bar graphs with error bars, see the page at


Run this code
x <- matrix(rnorm(1000),ncol=20)
boxplot(x,notch=TRUE,main="Notched boxplot with error bars")

#show 50% confidence regions and color each variable separately
   main="50 percent confidence limits",col=rainbow(ncol(attitude)) )
error.bars(attitude,bar=TRUE)  #show the use of bar graphs

#combine with a strip chart and boxplot
           main="Stripchart with 95 percent confidence limits")

#use statistics from somewhere else
#by specifying n, we are using the t distribution for confidences
#The first example allows the variables to be spaced along the x axis
my.stats <- data.frame(values=c(1,2,8),mean=c(10,12,18),se=c(2,3,5),n=c(5,10,20))
 error.bars(stats=my.stats,type="b",main="data with confidence intervals")
#don't connect the groups
my.stats <- data.frame(values=c(1,2,8),mean=c(10,12,18),se=c(2,3,5),n=c(5,10,20))
      error.bars(stats=my.stats,main="data with confidence intervals")
#by not specifying value, the groups are equally spaced
my.stats <- data.frame(mean=c(10,12,18),se=c(2,3,5),n=c(5,10,20))
rownames(my.stats) <- c("First", "Second","Third")

#Consider the case where we get stats from describe
temp <- describe(attitude)

#show these do not differ from the other way by overlaying the two

#n is omitted
#the error distribution is a normal distribution
my.stats <- data.frame(mean=c(2,4,8),se=c(2,1,2))
rownames(my.stats) <- c("First", "Second","Third")
#n is specified
#compare this with small n which shows larger confidence regions
my.stats <- data.frame(mean=c(2,4,8),se=c(2,1,2),n=c(10,10,3))
rownames(my.stats) <- c("First", "Second","Third")

#example of arrest rates (as percentage of condition)
arrest <- data.frame(Control=c(14,21),Treated =c(3,23))
rownames(arrest) <- c("Arrested","Not Arrested"),ylab="Probability of Arrest",xlab="Control vs Treatment",
main="Probability of Arrest varies by treatment")

#Show the raw  rates,raw=TRUE,ylab="Number Arrested",xlab="Control vs Treatment",
main="Count of Arrest varies by treatment")

#If a grouping variable is specified, the function calls
#Use to have more control over the output.
#Show  how to use grouping variables
error.bars(SATV + SATQ ~ gender, data=sat.act) #one grouping variable, formula input
error.bars(SATV + SATQ ~ education + gender,data=sat.act)#two  grouping variables

# }

Run the code above in your browser using DataLab