An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-teaching/attachments/20101211/75436d6e/attachment.pl>
adding plus/minus 1 standard devaition into each bar in cluster bar chart
7 messages · Ali Zanaty, Adams, Zeno, William Revelle +4 more
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-teaching/attachments/20101213/62d87d9a/attachment.pl>
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: r-sig-teaching-bounces at r-project.org on behalf of Ali Zanaty
Sent: Sat 12/11/2010 3:43 PM
To: r-sig-teaching at r-project.org
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]]
_______________________________________________ R-sig-teaching at r-project.org 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]] _______________________________________________ R-sig-teaching at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
Hi Ali,
Here is another suggestion. What I really like about using ggplot2 is
the graphs are elegant (at least to my eye), and extremely flexible.
For instance, using the method I show below, suppose you decided you
wanted +/- 1 standard error instead of standard deviation, no problem,
just change the summary function "myfun" and you're done. ggplot2
takes care of applying your function to every level of cat2, you do
not have to do this yourself with tapply() or by(). What if you
wanted a point +/- 1 sd instead of using a lot of ink to create bars?
All you need to do is ask for geom = c("point", "errorbar"). In other
words, it becomes very easy to ask for different types of graphical
objects (bars, errorbars, points, lines, etc.) and use different
summary functions, without a lot of effort or change on your part.
Now if only someone would do the same for cooking...
Cheers,
Josh (code follows)
## Your data in copy-and-pastable format
zdat <- structure(list(data = c(23L, 45L, 34L, 5L, 32L, 44L, 3L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 544L, 78L, 543L, 34L, 89L, 9L, 43L,
23L, 45L, 7L, 5L, 7L, 6L, 867L, 3L, 4L, 5L, 6L, 7L, 8L, 9L),
cat1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"2"), class = "factor"), cat2 = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L), .Label = c("1", "2", "3"), class = "factor")), .Names = c("data",
"cat1", "cat2"), row.names = c(NA, -36L), class = "data.frame")
## summary function, calculates, the mean, mean - sd, and mean + sd
myfun <- function(x) {
mx <- mean(x, na.rm = TRUE)
sx <- sd(x, na.rm = TRUE)
out <- data.frame(y = mx, ymin = mx - sx, ymax = mx + sx)
return(out)
}
## ggplot2 package, great for graphing
library(ggplot2)
qplot(x = cat2, y = data, data = zdat,
fill = cat2, geom = c("bar", "errorbar"),
stat = "summary", fun.data = myfun)
## basic info: what is on x, y, and where is data
## aesthetics: fill colour, and ask for bars and errorbars
## tell it to calculate summaries using "myfun" defined earlier
On Sat, Dec 11, 2010 at 6:43 AM, Ali Zanaty <zanaty2005 at yahoo.com> wrote:
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]]
_______________________________________________ R-sig-teaching at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Before doing that, I'd highly recommend reading http://biostat.mc.vanderbilt.edu/twiki/pub/Main/TatsukiRcode/Poster3.pdf Hadley
On Sat, Dec 11, 2010 at 8:43 AM, Ali Zanaty <zanaty2005 at yahoo.com> wrote:
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]]
_______________________________________________ R-sig-teaching at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-teaching/attachments/20101214/181887bf/attachment.pl>
Superior plots can be made pretty easily in ggplot2 library(ggplot2) ggplot() + stat_boxplot(aes(y = mpg,x = factor(gear)),data=mtcars,width = 0.5) + geom_point(aes(x = factor(gear),y = mpg),data=mtcars,position = position_jitter(height = 0.0,width = 0.1)) There is also a convinience function in Deducer that does much the same thing: library(Deducer) data(mtcars) oneway.plot(formula=mpg~gear,data=mtcars,alpha=1) #with one variable oneway.plot(formula=d(mpg,disp,hp,drat)~gear,data=mtcars, alpha=1) #with multiple variables
On Dec 14, 2010, at 6:43 AM, Stuart Wagenius wrote:
Hadley, Thanks for the recommended reading. Is there R code to make recommended plots? Stuart On Mon, Dec 13, 2010 at 5:22 PM, Hadley Wickham <hadley at rice.edu> wrote:
Before doing that, I'd highly recommend reading http://biostat.mc.vanderbilt.edu/twiki/pub/Main/TatsukiRcode/Poster3.pdf Hadley On Sat, Dec 11, 2010 at 8:43 AM, Ali Zanaty <zanaty2005 at yahoo.com> wrote:
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]]
_______________________________________________ R-sig-teaching at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
-- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/
_______________________________________________ R-sig-teaching at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
[[alternative HTML version deleted]]
_______________________________________________ R-sig-teaching at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-teaching