[R] barplot with errorbars
marc_schwartz at me.com
Thu Feb 17 22:39:00 CET 2011
Thanks for the reference to the R News article. That seems like a lifetime ago... :-)
As an exercise in learning to use R's base graphics and some of the concepts of manipulating and annotating base R plots, it is a decent intro.
That being said, unless pressured (eg. weapon pointing at cranium), I would never actually use that particular format (the barplots) today in presenting data. The same goes for the barplot2() function in the gplots CRAN package, which I wrote, but have literally not used in years. That function automates barplots and confidence interval plotting, amongst other enhancements (eg. log scaled axes), to the base barplot() function.
If I may, allow me to offer some additional comments to Maria.
First, note that barplots are best used to present discrete counts or perhaps percentages, not continuous data. The y axis (in the case of a vertical barplot) is intended to start at 0 to allow the appropriate perception of count/percentage based differences within or between groups, as represented by the heights of the bars.
The addition of confidence intervals to barplots, especially only the upper bound, which are also known as dynamite plots (http://biostat.mc.vanderbilt.edu/wiki/Main/DynamitePlots), are generally frowned upon these days, though there are communities that still prefer that format for count data.
For continuous data however, point plots with confidence intervals are much preferred. Other alternatives include boxplots and violin plots, if one is wanting to visually present more information on the distribution of a continuous variable between groups, beyond point estimates for the means and CIs.
Frank has a nice intro document on statistical graphics here:
that you might want to review.
I would urge Maria to consider alternatives to using a barplot to present this data. There are various custom functions to aid with that (searching the list archives using rseek.org should yield good results) and the ?arrows and ?segments functions typically serve as the basis for drawing the CIs around the points representing the means.
On Feb 17, 2011, at 2:38 PM, Aldi Kraja wrote:
> Hi Toby and Maria,
> I did a check on Toby's suggestion and it is not there:
> R version 2.12.1 (2010-12-16)
> No help files found with alias or concept or title matching
> 'barploterrbar' using fuzzy matching.
> Also I went to the following location which does not exist.
> To Maria: here is a suggestion: to do the barplot with confidence
> intervals / errorbars:
> look at the following R news under www.r-project.org
> "An introduction to using R's base graphics." (see following citation)
> on page 4 you will find explanations and code how to build bars with se.
>  Marc Schwartz. R Help Desk: An introduction to using R's base
> graphics. /R News/, 3(2):2-6, October 2003. [ bib | http
> <http://CRAN.R-project.org/doc/Rnews/> | .pdf
> <http://CRAN.R-project.org/doc/Rnews/Rnews_2003-2.pdf> ]
> On 2/17/2011 10:08 AM, Toby Marthews wrote:
>> If you google "barplot with error bars" you immediately find http://svitsrv25.epfl.ch/R-doc/library/prada/html/barploterrbar.html .
>> From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Lathouri, Maria [m.lathouri06 at imperial.ac.uk]
>> Sent: 17 February 2011 16:00
>> To: r-help at r-project.org
>> Subject: [R] barplot with errorbars
>> Dear all
>> I have six variables of the average metal concentrations
>> Var1 4.77
>> Var2 23.5
>> Var3 5.2
>> Var4 12.3
>> Var5 42.1
>> Var6 121.2
>> I want to plot them as a barplot with error bars. Could you help me?
More information about the R-help