Plotting confidence interval bars from summary statistics

A bit like a box plot. I mean not necessarily the standard upper confidence interval, lower confidence interval, mean, and data range-showing box plots, but I mean like a box plot with just the three pieces of data: the 95% confidence interval and mean.

This is a screenshot of a journal article which had exactly what I want:

I would also like to know how I would use the software the answerer mentions to create such a plot.

• What you call "the standard upper confidence interval, lower confidence interval, mean, and data range-showing box plots" are not standard box plots at all. The standard box plot shows median, quartiles, minimum and maximum and often other details. – Nick Cox Jul 19 '13 at 19:13
• @Nick Right: but what are such graphics actually called? (I'm not sure.) – whuber Jul 19 '13 at 19:14
• This kind of plot is standard (it's not a box plot, though) and can be produced by all major statistical packages: Stata, SAS, R, MATLAB. I guess you could do it with Excel as well. – COOLSerdash Jul 19 '13 at 19:15
• @COOLSerdash great! Could you write up an answer for MATLAB since I already have MATLAB? – Josh Pinto Jul 19 '13 at 19:16
• I'd call these confidence interval plots, but not every plot really needs a distinct name. Every plot deserves a text caption, however.... – Nick Cox Jul 19 '13 at 19:20

In MATLAB, you might want to try the errorbar function: http://www.mathworks.de/de/help/matlab/ref/errorbar.html

Alternatively, you can do it the dumb and manual way. For example, given a matrix of data points "a", you can calculate your means using the function m = mean(a), calculate your CIs (depending on what CI you need), and plot the results by hand.

Demonstration if you already know the mean and CI, assuming CIs are in a matrix CI (first and second column) and means are in a matrix a:

plot(1:length(CI),a,'o','markersize', 10)           % plot the mean
hold on;
plot(1:length(CI),CI(1,:),'v','markersize', 6)              % plot lower CI boundary
hold on;
plot(1:length(CI),CI(2,:),'^','markersize', 6)              % plot upper CI boundary
hold on;

for I = 1:length(CI)                                        % connect upper and lower bound with a line
line([I I],[CI(1,I) CI(2,I)])
hold on;
end;

axis([0 length(CI)+1 min(CI(1,:))*0.75 max(CI(2,:))*1.25])  % scale axis

Demonstration in the case where you know individual measurements, for a repeated-measures experiment, 3+ conditions, one condition per column, one subject per line in matrix a, no missing samples, 95% CI as by MATLAB's ttest():

[H,P,CI] = ttest(a);                                        % calculate 95% CIs for every column in matrix a
% CIs are now in the matrix CI!

plot(1:length(CI),[mean(a)],'o','markersize', 10)           % plot the mean
hold on;
plot(1:length(CI),CI(1,:),'v','markersize', 6)              % plot lower CI boundary
hold on;
plot(1:length(CI),CI(2,:),'^','markersize', 6)              % plot upper CI boundary
hold on;

for I = 1:length(CI)                                        % connect upper and lower bound with a line
line([I I],[CI(1,I) CI(2,I)])
hold on;
end;

axis([0 length(CI)+1 min(CI(1,:))*0.75 max(CI(2,:))*1.25])  % scale axis
• Read my latest edit, I'm sorry but some people who are annoying the hell out of me, edited out the fact I only know the 95% CI and mean no additional data. – Josh Pinto Jul 22 '13 at 1:07
• @BrentonHorne did the link to the errorbar function not help? It looks to be exactly what you want. – Peter Ellis Jul 22 '13 at 4:57
• As Peter said, use errorbar() then. Or use the script I wrote, but ignore the first line with the ttest and just feed it mean and CI directly. I'll edit it in I guess. – jona Jul 22 '13 at 7:59
• @BrentonHorne To be fair, you never discussed the point extensively or even mention it at all in the body of the question. It was implied by the title but this title also mistakenly mentioned box plots, which is the issue the edit tried to fix. It's unfortunate that the word “only” disappeared in the process but nobody purposefully edited it out a big part of your question. Besides, the answer did contain a solution and the rest of the text might be interesting to other readers. – Gala Jul 22 '13 at 8:14
• I ran the code it went well, the problem is that I would like it if there was some means by which I could join the upper CI and lower CI with a line like in the screenshot in my question. – Josh Pinto Jul 22 '13 at 17:40

Look if this helps you. R solution:

par(mfrow=c(2,1)) # to stack the charts on column

#Dataset 1

upperlimit = c(10,12,8,14)
lowerlimit = c(5,9,4,7)
mean = c(8,10,6,12)

df = data.frame(cbind(upperlimit,lowerlimit,mean))

plot(df$mean, ylim = c(0,30), xlim = c(1,4)) install.packages("plotrix") require(plotrix) plotCI(df$mean,y=NULL, uiw=df$upperlimit-df$mean, liw=df$mean-df$lowerlimit, err="y",      pch=20, slty=3, scol = "black", add=TRUE)

#Dataset 2

upperlimit_2 = upperlimit*1.5
lowerlimit_2 = lowerlimit*0.8
mean_2 = upperlimit_2-lowerlimit_2

df_2 = data.frame(cbind(upperlimit_2,lowerlimit_2,mean_2))

plot(df$mean_2, ylim = c(0,30), xlim = c(1,4)) plotCI(df_2$mean_2,y=NULL, uiw=df_2$upperlimit_2-df_2$mean_2, liw=df_2$mean_2- df_2$lowerlimit_2, err="y", pch=20, slty=3, scol = "black", add=TRUE)

rm(upperlimit,lowerlimit,mean,df,upperlimit_2,lowerlimit_2,mean_2,df_2) #remove the objects stored from workspace

par(mfrow=c(1,1)) # go back to default (one graph at a time)

• and how do I run such a code? I am totally new to R so you'll have to walk me through it because I am clueless. – Josh Pinto Jul 19 '13 at 20:27
• I can help you to give a shot, but firstly you need to follow two steps: i) install R Studio on your pc, ii) provide a reproducible example: stackoverflow.com/questions/5963269/… – Andre Silva Jul 19 '13 at 20:31
• @AndreSilva, I think you went the extra mile there. Great support and mentoring! – doug.numbers Jul 19 '13 at 22:14
• This is what I got for running your code i.stack.imgur.com/yrZaJ.png – Josh Pinto Jul 22 '13 at 19:41
• @BrentonHome. Strange. I just tested and it worked. The "error" bars are nor appearing. Did you instal the plotrix package? I added a program command line in the code that installs the plotrix package. Please, try again and let know. – Andre Silva Jul 22 '13 at 19:45

This type of plot in R using ggplot2, though you might have to do some fiddling with the axis font size:

library(ggplot2)
data.estimates = data.frame(
var   = c('1', '2', '3', '4', '5', '6', '7', '8', '9'),
par = c(1.12210,0.18489,1.22011,1.027446235,0.43521,0.53464,1.93316,-0.43806,-0.12029),
se = c(0.42569,0.32162,0.58351,0.771608551,0.24803,0.65372,0.92717,0.45939,0.51558))
data.estimates$idr <- exp(data.estimates$par)
data.estimates$upper <- exp(data.estimates$par + (1.96*data.estimates$se)) data.estimates$lower <- exp(data.estimates$par - (1.96*data.estimates$se))

p2 <- ggplot(data.estimates, aes(var,idr, size=10)) + theme_bw(base_size=10)
p2 + geom_point() +geom_errorbar(aes(x = var, ymin = lower, ymax = upper, size=2), width = 0.2) + scale_y_log10(limits=c(0.1, 50), breaks=c(0.1, 0.5, 1, 5, 10, 25, 50)) + xlab("Site") + ylab("RR")

In Stata use serrbar or ciplot (SSC) or eclplot (Stata Journal, SSC).

Assuming you have access to the original data you can do this in R with the lineplot.CI function in the sciplot library

Example with mtcars dataset:

lineplot.CI(x.factor=gear, response=mpg, group=vs, data=mtcars)

Note that lineplot.CI by default plots SE bars (it can be changed defining a new function with the argument ci.fun to plot 95% CI intervals)

lineplot.CI(x.factor=gear, response=mpg, group=vs, data=mtcars, ci.fun=function(x) c(mean(x)-1.96*se(x), mean(x)+1.96*se(x)))

This could be done in R with points() (or plot(..., type="p")) and segments(). There might also be R functions designed to create the CI's for you, but those might require the original data. The multiple panels in the same figure created with par(mfrow=c(4,1)). If you don't know any R, this would be hard to do easily (as in, you would have to learn a bit more R or get someone to help with your specific data set).

GraphPad Prism can easily make this kind of graph, plotting error bars from error values you enter. Create a grouped table formatted for entery of mean, - error and + error.

• Is it possible to get a free copy of it that isn't just a 30 day trial version? – Josh Pinto Jul 19 '13 at 19:55
• @BrentonHorne: No. It is not free software. – Harvey Motulsky Jul 20 '13 at 1:17