Skip to content

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
#
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:
#
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:

  
    
#
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:

  
    
#
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: