[R] Bar Plots: Error Bars

0 views
Skip to first unread message

bshan101

unread,
Dec 3, 2009, 2:18:33 AM12/3/09
to r-h...@r-project.org

Hey Everyone,

I'm pretty new at R and wanted to try and make some graphs with dummy data
before using it to analyze my own. I successfully made a bar graph and error
bars, but I can't figure out how to align them properly (currently they are
not centered on the bars and some of them aren't even close). Here's the
code that I'm using:

> marks <- sample(4:10, size=50, replace=TRUE)
> dim(marks) <- c(10,5)
> classavg <- colMeans (marks, na.rm=FALSE, dims = 1)
> barplot(classavg, main="Class Average for Quizzes", xlab="Quiz",
> ylab="Average", names = c("1","2","3","4","5"), ylim=c(0,12), axis.lty=1)
box()
> max <- apply (marks, 2, max)
> min <- apply (marks, 2, min)
> arrows(1:5, min, 1:5, max, code=3, angle=90, length=0.125)

The error bars are simply ranges for now (I wanted to do that before I tried
standard deviation/error). If anyone could help me align them it would be
much appreciated!

brett

--
View this message in context: http://n4.nabble.com/Bar-Plots-Error-Bars-tp947407p947407.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@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.

Jeremy Baxter

unread,
Dec 3, 2009, 3:49:36 AM12/3/09
to r-h...@r-project.org
Brett

Hi

Searching http://www.Rseek.org will return many similar results. You might find
the following useful in terms of your question re aligning the bars:

http://users.fmg.uva.nl/rgrasman/rpages/2005/09/error-bars-in-plots.html

The discussion on this page also refers to the errbar function in the Hmisc
package. There are other options, searching Rseek will provide many!

Basically something along the lines of the following will 'fix' your graph:

marks <- sample(4:10, size=50, replace=TRUE)
dim(marks) <- c(10,5)
classavg <- colMeans (marks, na.rm=FALSE, dims = 1)

thegraph <- barplot(classavg, main="Class Average for Quizzes", xlab="Quiz",

ylab="Average", names = c("1","2","3","4","5"), ylim=c(0,12), axis.lty=1)

str(thegraph)


max <- apply (marks, 2, max)
min <- apply (marks, 2, min)

arrows(thegraph, min, thegraph, max, code=3, angle=90, length=0.125)

HTH,
Jeremy

> Hey Everyone,
>
> I'm pretty new at R and wanted to try and make some graphs with dummy data
> before using it to analyze my own. I successfully made a bar graph and
> error bars, but I can't figure out how to align them properly (currently
> they are not centered on the bars and some of them aren't even close).
> Here's the
>
> code that I'm using:
> > marks <- sample(4:10, size=50, replace=TRUE)
> > dim(marks) <- c(10,5)
> > classavg <- colMeans (marks, na.rm=FALSE, dims = 1)
> > barplot(classavg, main="Class Average for Quizzes", xlab="Quiz",
> > ylab="Average", names = c("1","2","3","4","5"), ylim=c(0,12), axis.lty=1)
>
> box()
>
> > max <- apply (marks, 2, max)
> > min <- apply (marks, 2, min)
> > arrows(1:5, min, 1:5, max, code=3, angle=90, length=0.125)
>
> The error bars are simply ranges for now (I wanted to do that before I
> tried standard deviation/error). If anyone could help me align them it
> would be much appreciated!
>
> brett

---------------
Jeremy Baxter, Statistics Department, Rhodes University, South Africa.
Views expressed above, no matter how badly spelt, are my own... I think?

[[alternative HTML version deleted]]

bshan101

unread,
Dec 3, 2009, 4:31:29 AM12/3/09
to r-h...@r-project.org

Thanks so much! And those links are really useful :)

brett

--
View this message in context: http://n4.nabble.com/Bar-Plots-Error-Bars-tp947407p947484.html


Sent from the R help mailing list archive at Nabble.com.

______________________________________________

Jose Gaspar Macia Vicente

unread,
Dec 3, 2009, 5:53:15 AM12/3/09
to bshan101, r-h...@r-project.org
Hi Brett,

have you tried the function "errbar", within the library "Hmisc"?
It works pretty well for barplots. You have to specify the coordinates where
error bars start ("x" and "y"), end then the ending points they end
("yminus=y-error" and "yplus=y+error").

The plotting function "barplot2" within the library "gplots" allows you also
to add error bars intrinsecally.

Hope it helps!

Jose

2009/12/3 bshan101 <bsha...@gmail.com>

[[alternative HTML version deleted]]

Frank E Harrell Jr

unread,
Dec 3, 2009, 8:56:30 AM12/3/09
to r-h...@r-project.org
Bar charts with error bars are far inferior to dot charts and other
types of displays. One of many problems is demonstrated if you draw a
bar chart displaying temperature in F then re-draw it on the degrees C
scale. See http://biostat.mc.vanderbilt.edu/DynamitePlots for much more
information. The error bars lull us into an assumption that symmetric
confidence intervals are OK, among other things.

Frank

--
Frank E Harrell Jr Professor and Chair School of Medicine
Department of Biostatistics Vanderbilt University

Heinz Tuechler

unread,
Dec 3, 2009, 9:52:06 AM12/3/09
to Frank E Harrell Jr, r-h...@r-project.org
Frank,

the example on http://biostat.mc.vanderbilt.edu/DynamitePlots is
nice, and I agree with you. Just one minor question: would it be
possible to mention as "An article with nice dot plots" a paper,
which is freely available?

Heinz

Jim Lemon

unread,
Dec 3, 2009, 9:58:30 PM12/3/09
to Frank E Harrell Jr, r-h...@r-project.org
On 12/04/2009 12:56 AM, Frank E Harrell Jr wrote:
> Bar charts with error bars are far inferior to dot charts and other
> types of displays. One of many problems is demonstrated if you draw a
> bar chart displaying temperature in F then re-draw it on the degrees C
> scale. See http://biostat.mc.vanderbilt.edu/DynamitePlots for much
> more information. The error bars lull us into an assumption that
> symmetric confidence intervals are OK, among other things.
>
> Frank
>
I could promote the "dispersion" function as capable of displaying
asymmetric confidence intervals on whatever location indicators you
prefer (have a look at the second example for "hierobarp" for example),
but the problem is deeper than that. The point of graphic displays is to
convey information to someone else, not to tell ourselves what we
already know. Do people cheat with pie charts, bar charts, etc? Sure,
and we could cheat with dot charts, too. Graphic displays are typically
shown to an audience that knows less about the topic than the presenter
in the hope that an obscure relationship will be clarified. If I am
presenting to colleagues in my field, I will use much more informative
and difficult to understand graphics than if I am summarizing the same
results to the general public. If both groups leave the respective
presentations enlightened, I have done well.

Jim

Frank E Harrell Jr

unread,
Dec 3, 2009, 11:16:07 PM12/3/09
to Jim Lemon, r-h...@r-project.org
Jim Lemon wrote:
> On 12/04/2009 12:56 AM, Frank E Harrell Jr wrote:
>> Bar charts with error bars are far inferior to dot charts and other
>> types of displays. One of many problems is demonstrated if you draw a
>> bar chart displaying temperature in F then re-draw it on the degrees C
>> scale. See http://biostat.mc.vanderbilt.edu/DynamitePlots for much
>> more information. The error bars lull us into an assumption that
>> symmetric confidence intervals are OK, among other things.
>>
>> Frank
>>
> I could promote the "dispersion" function as capable of displaying
> asymmetric confidence intervals on whatever location indicators you
> prefer (have a look at the second example for "hierobarp" for example),
> but the problem is deeper than that. The point of graphic displays is to
> convey information to someone else, not to tell ourselves what we
> already know. Do people cheat with pie charts, bar charts, etc? Sure,
> and we could cheat with dot charts, too. Graphic displays are typically
> shown to an audience that knows less about the topic than the presenter
> in the hope that an obscure relationship will be clarified. If I am
> presenting to colleagues in my field, I will use much more informative
> and difficult to understand graphics than if I am summarizing the same
> results to the general public. If both groups leave the respective
> presentations enlightened, I have done well.
>
> Jim
>
>

Jim I'll just add that in many cases the best graphics are just as easy
or easier to read and understand than bad graphics. And dot charts have
a much less chance of being misleading than bar and pie charts,
especially if more data characteristics or the data themselves appear.

Frank
--
Frank E Harrell Jr Professor and Chair School of Medicine
Department of Biostatistics Vanderbilt University

______________________________________________

Greg Snow

unread,
Dec 4, 2009, 1:01:18 PM12/4/09
to Jim Lemon, Frank E Harrell Jr, r-h...@r-project.org
This reminds me of a quote I saw once (I think it may have been in one of those Murphy's Laws calendars), my parahpase:

If you make someone think that they are thinking,
They will love you for it.
If you make them actually think,
They will hate you for it.

This explains why people love pie charts and hate more effective graphs.

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg...@imail.org
801.408.8111

Reply all
Reply to author
Forward
0 new messages