error bar question (mean_sdl)

1,319 views
Skip to first unread message

Vivek

unread,
Jan 29, 2009, 12:14:46 AM1/29/09
to ggplot2

Hi,

I'm very new to R. I want to make a scatter plot that has of all my
categorical data add the mean and standard deviation as error bars.

I am using the mean_sdl function with stat_summary. The default for
this is 2SD. How can I change this to one SD.

Many thanks!
VK

bn<-read.csv("bn.csv", header = TRUE)
plot1 <- ggplot(bn, aes(strain,sum5minavg))
plot1<- plot1 + geom_point(colour = "gray59", size = 2)

#error bars and mean (from http://had.co.nz/ggplot2/stat_summary.html)
stat_sum_single <- function(fun, geom="point", ...) {
stat_summary(fun=fun, colour="red", geom=geom, size = 3, ...)
}

plot1<- plot1 + stat_sum_single(mean) #this plots the mean point in
Red


stat_sum_df <- function(fun, geom="crossbar", ...) {
stat_summary(fun=fun, colour="red", geom=geom, width=0.2, ...)
}

#draw error bars - only draws +/-2SD error bars
plot1<- plot1 + stat_sum_df(fun="mean_sdl", geom = "errorbar")




David Hajage

unread,
Jan 29, 2009, 3:46:04 AM1/29/09
to Vivek, ggplot2
Hello Vivek,

I think you must create a new function :

library(ggplot2)
c <- qplot(cyl, mpg, data=mtcars)
c + stat_summary(fun = "mean_sdl", geom = "errorbar") # default

mean_sdl_custom <- function(data) {
  data.frame("y" = mean(data$y), "ymin" = mean(data$y) - sd(data$y), "ymax" = mean(data$y) + sd(data$y))
}

c + stat_summary(fun = "mean_sdl_custom", geom = "errorbar") # with the new function


David

David Hajage

unread,
Jan 29, 2009, 5:39:09 AM1/29/09
to David Hajage, Vivek, ggplot2
The creation of a new function for stat_summary is not actually very easy.

A way could be to include a function "like" this one in ggplot2 ?

summary.fun <- function(y, ymin = NULL, ymax = NULL) {
  if (is.null(ymin) & is.null(ymax)) {
    res <- function(x) y(x)
  }
  else {
    res <- function(data) data.frame("y" = y(data$y), "ymin" = ymin(data$y), "ymax" = ymax(data$y))
  }
  return(res)

}

c <- qplot(cyl, mpg, data=mtcars)
c + stat_summary(fun = "mean_sdl", geom = "errorbar")
c + stat_summary(fun = summary.fun(mean, min, max), geom = "errorbar")
c + stat_summary(fun = summary.fun(sd), geom = "point", colour = "red")

david

2009/1/29 David Hajage <dha...@gmail.com>

hadley wickham

unread,
Jan 29, 2009, 2:50:29 PM1/29/09
to David Hajage, David Hajage, Vivek, ggplot2
Hi David,

That's a good suggestion. I really don't like the way that
stat_summary currently works and I think your suggestion could be
incorporated directly, so you could just do something like

c + stat_summary(y = mean, ymin = min, ymax = max, geom = "errorbar")

I'll think about changing that for the next release.

Thanks!

Hadley
--
http://had.co.nz/

David Hajage

unread,
Jan 29, 2009, 3:38:31 PM1/29/09
to hadley wickham, David Hajage, Vivek, ggplot2
Thank you !

David

Vivek

unread,
Jan 29, 2009, 6:55:26 PM1/29/09
to ggplot2
Thank you David and Hadley!

Vivek

On Jan 29, 2:38 pm, David Hajage <dhaj...@gmail.com> wrote:
> Thank you !
>
> David
>
> On Thu, Jan 29, 2009 at 8:50 PM, hadley wickham <h.wick...@gmail.com> wrote:
> > Hi David,
>
> > That's a good suggestion.  I really don't like the way that
> > stat_summary currently works and I think your suggestion could be
> > incorporated directly, so you could just do something like
>
> > c + stat_summary(y = mean, ymin = min, ymax = max, geom = "errorbar")
>
> > I'll think about changing that for the next release.
>
> > Thanks!
>
> > Hadley
>
> > On Thu, Jan 29, 2009 at 4:39 AM, David Hajage <dhajag...@gmail.com> wrote:
> > > The creation of a new function for stat_summary is not actually very
> > easy.
>
> > > A way could be to include a function "like" this one in ggplot2 ?
>
> > > summary.fun <- function(y, ymin = NULL, ymax = NULL) {
> > >   if (is.null(ymin) & is.null(ymax)) {
> > >     res <- function(x) y(x)
> > >   }
> > >   else {
> > >     res <- function(data) data.frame("y" = y(data$y), "ymin" =
> > ymin(data$y),
> > > "ymax" = ymax(data$y))
> > >   }
> > >   return(res)
> > > }
>
> > > c <- qplot(cyl, mpg, data=mtcars)
> > > c + stat_summary(fun = "mean_sdl", geom = "errorbar")
> > > c + stat_summary(fun = summary.fun(mean, min, max), geom = "errorbar")
> > > c + stat_summary(fun = summary.fun(sd), geom = "point", colour = "red")
>
> > > david
>
> > > 2009/1/29 David Hajage <dhaj...@gmail.com>
>
> > >> Hello Vivek,
>
> > >> I think you must create a new function :
>
> > >> library(ggplot2)
> > >> c <- qplot(cyl, mpg, data=mtcars)
> > >> c + stat_summary(fun = "mean_sdl", geom = "errorbar") # default
>
> > >> mean_sdl_custom <- function(data) {
> > >>   data.frame("y" = mean(data$y), "ymin" = mean(data$y) - sd(data$y),
> > >> "ymax" = mean(data$y) + sd(data$y))
> > >> }
>
> > >> c + stat_summary(fun = "mean_sdl_custom", geom = "errorbar") # with the
> > >> new function
>
> > >> David
>
> > >> On Thu, Jan 29, 2009 at 6:14 AM, Vivek <vivek...@gmail.com> wrote:
>
> > >>> Hi,
>
> > >>> I'm very new to R.  I want to make a scatter plot that has of all my
> > >>> categorical data add the mean and standard deviation as error bars.
>
> > >>> I am using the mean_sdl function with stat_summary.  The default for
> > >>> this is 2SD.  How can I change this to one SD.
>
> > >>> Many thanks!
> > >>> VK
>
> > >>> bn<-read.csv("bn.csv", header = TRUE)
> > >>> plot1 <- ggplot(bn, aes(strain,sum5minavg))
> > >>> plot1<- plot1 + geom_point(colour = "gray59", size = 2)
>
> > >>> #error bars and mean (fromhttp://had.co.nz/ggplot2/stat_summary.html)
Reply all
Reply to author
Forward
0 new messages