[R-sig-teaching] adding plus/minus 1 standard devaition into each bar in cluster bar chart

Joshua Wiley jwiley.psych at gmail.com
Mon Dec 13 22:17:15 CET 2010


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/




More information about the R-sig-teaching mailing list