Another error.bars function is found in the psych package.
For zanaty's data, a log transform makes more sense:
package(psych)
error.bars.by(my.data$data,my.data$cat2,by.var=TRUE,bars=TRUE,sd=TRUE)
#original data
error.bars.by(log(my.data$data),my.data$cat2,by.var=TRUE,bars=TRUE,sd=TRUE)
#log transformed data
error.bars.by(log(my.data$data),my.data$cat1*(my.data$cat2+3),by.var=TRUE,bars=TRUE,sd=TRUE)
#to group by cat1 and cat2
?error.bars.by
Bill
At 10:20 AM +0100 12/13/10, Adams, Zeno wrote:
In Michael Crawley's "The R Book" (which I can highly recommend for
teaching undergraduate statistics) the author proposes a function
that adds the standard errors with the line function. The function
has three arguments: The means, the standard errors, and the bar
labels. For a reproducible dataset such as:
dat1 <- data.frame(income = c((2000 + 500*rnorm(10)),(2500 +
500*rnorm(10))), gender = rep(c("m","f"),c(10,10)))
with
ybar <- tapply(dat1$income, dat1$gender, mean)
se <- tapply(dat1$income, dat1$gender, sd)/10
labels <- as.character(levels(dat1$gender))
as the means, se, and labels, respectively
the function is:
error.bars<-function(yv,z,nn) {
xv<-
barplot(yv,ylim=c(0,(max(yv)+max(z))),names=nn,ylab=deparse(substitute(yv)
))
g=(max(xv)-min(xv))/50
for (i in 1:length(xv)) {
lines(c(xv[i],xv[i]),c(yv[i]+z[i],yv[i]-z[i]))
lines(c(xv[i]-g,xv[i]+g),c(yv[i]+z[i], yv[i]+z[i]))
lines(c(xv[i]-g,xv[i]+g),c(yv[i]-z[i], yv[i]-z[i]))
}}
which can be used after plotting the data:
barplot(ybar)
error.bars(ybar, se, labels)
Zeno
-----Original Message-----
From: [email protected] on behalf of Ali Zanaty
Sent: Sat 12/11/2010 3:43 PM
To: [email protected]
Subject: [R-sig-teaching] adding plus/minus 1 standard devaition
into eachbar in cluster bar chart
Dear All R users:
I hope that this message finds all of you well.
Currently, I am teaching an undergraduate course in statistics.
If possible, Could you please help me out how to add plus/minus 1 standard
deviation added to each bar into R cluster bar chart.
You can use the following artificial data.
data cat 1 cat 2
23 1 1
45 1 1
34 1 1
5 1 1
32 1 1
44 1 1
3 2 1
1 2 1
2 2 1
3 2 1
4 2 1
5 1 2
6 1 2
7 1 2
8 1 2
544 2 2
78 2 2
543 2 2
34 2 2
89 2 2
9 1 3
43 1 3
23 1 3
45 1 3
7 1 3
5 1 3
7 1 3
6 2 3
867 2 3
3 2 3
4 2 3
5 2 3
6 2 3
7 2 3
8 2 3
9 2 3
I need to create a bar chart for the mean of data column for each category. I
know how to create the cluster bar chart. But I need your help with how to add
plus/minus 1 standard deviation into each bar.
Thank you so much for your helps and for your attention to this matter, and I
look forward to hearing from you.
Regards,
zanaty
[[alternative HTML version deleted]]
_______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
EBS European Business School gemeinnuetzige GmbH, Universitaet fuer
Wirtschaft und Recht i.Gr. - Amtsgericht Wiesbaden HRB 19951 -
Umsatzsteuer-ID DE 113891213 Geschaeftsfuehrung: Prof. Dr.
Christopher Jahns, President; Prof. Dr. Rolf Tilmes, Dean Business
School; Sabine Fuchs, CMO; Prof. Dr. Dr. Gerrick Frhr. v.
Hoyningen-Huene, Dean Law School; Aufsichtsrat: Dr. Hellmut K.
Albrecht, Vorsitzender
[[alternative HTML version deleted]]
_______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
_______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-teaching