[R] Bar plot with error bars

Manuel Morales Manuel.A.Morales at williams.edu
Tue Oct 23 13:05:51 CEST 2007


Hi Katherine,

If you have the original data, you can use the function barplot.CI from
the package sciplot which will calculate the means and CI's for you.
Examples are at: http://mutualism.williams.edu/sciplot

If you have the means and CI's already calculated, the following
function will do what you want:

CI.plot <- function(mean, std, ylim=c(0, max(CI.H)), ...) {
    CI.H <- mean+1.96*std # Calculate upper CI
    CI.L <- mean-1.96*std # Calculate lower CI
    xvals <- barplot(mean, ylim=ylim, ...) # Plot bars
    arrows(xvals, mean, xvals, CI.H, angle=90) # Draw error bars
    arrows(xvals, mean, xvals, CI.L, angle=90)
}

On Mon, 2007-10-22 at 17:03 -0400, Katherine Jones wrote:
> Apologies if this has been asked before. I am having trouble  
> understanding the R mailing list never mind R!
> 
> I am relatively new to R having migrated from Minitab nd SPSS. I  
> have managed to do some more complicated statistics such as  
> hierarchical partitioning of variance on an 80,000 record dataset but  
> have to admit that drawing a simple bar plot I could do by hand is  
> proving extremely difficult!
> 
> I have Crawley's 'The R book' which is proving  a big help, but still  
> does not provide me with all of the information I need.
> 
> What I want to do:-
> I have a small dataset, with 'rate' as the dependent variable (y axis  
> variable) and two groups ('cat' and 'box') which I have calculated  
> the mean and standard error for and would like to display as a bar  
> plot. I would like to display the standard error for each group NOT a  
> pooled standard error (i.e. error bars will be different heights on  
> the two bars). In the Crawley book, he just repeats the same error  
> bars across all groups, and I don't know how to specify the different  
> error for each bar.
> 
> What I have managed to do so far:-
> -calculate mean and standard error
> -plots means with confidence intervals
> -plot means as bar plot but with no error bars or confidence intervals
> -everything except what I want to do!
> 
> I have learnt a lot about R but it is getting rather frustrating that  
> I can do complex GLMs but cannot do a simple plot in 4 hours! I have  
> migrated to R because I like the way it can do almost anything and  
> because I'm trying to go almost completely freeware as everytime I  
> move institution I run into trouble with getting software licenses  
> which also tend to be expensive. However, I'm not the most computer  
> literate person and have grown up with 'menus' and 'windows'.
> 
> Any help much appreciated as it will save me much time and frustration!
> 
> Dr. Katherine Jones
> Department of Biology,
> Carleton University
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
http://mutualism.williams.edu



More information about the R-help mailing list