[R] ggplot2, 'se' variable in geom_errorbar's limits?

Eric Fail eric.fail at gmx.com
Sun Feb 20 00:42:04 CET 2011


Thank you Scott and Ista,

I really appreciate your help! I solved it with Scott's help on the ddply.

For the record, here is the working example that solves my initial question:

## install.packages(c("ggplot2", "plyr"))
require(ggplot2)
data(diamonds)
diamonds <- diamonds[1:100,c(2,7)]

# use ddply in plyr package (loaded with ggplot2) to get data to plot
diamonds_df <- ddply(diamonds, .(cut, color), summarise,
	mean_price = mean(price),
	se_price = sd(price)/sqrt(length(price))
)

limits <- aes(ymax = mean_price + se_price, ymin = mean_price - se_price)

ggplot(diamonds_df, aes(colour= color, x = cut, y = mean_price)) +
	geom_point() +
	geom_line(aes(group= color)) +
	geom_errorbar(limits, width=0.2)

Very grateful!

Eric


On Sat, Feb 19, 2011 at 6:17 PM, Scott Chamberlain
<myrmecocystus at gmail.com> wrote:
> Hi Eric,
> I would just include that third variable in the ddply call, for example:
> ddply(diamonds, .(cut, clarity, etc...), summarise,
> mean = mean(price,
> se = ...
> )
> where you can summarise by multiple variables within the ".(x, y, etc.)"\\
> I think that answers your question. Let me know if not. The example I sent
> earlier was just for simplicity.
> Scott
>
> On Saturday, February 19, 2011 at 4:58 PM, Eric Fail wrote:
>
> Hi Scott,
>
> Thank you for taking the time to look at my problem!
>
> I played around with your example and realized that in solving the
> problem with limits by summarizing the data I loose the option to
> split the data along some third variable, say the 'color' variable in
> the diamonds data.
>
> Any idea on how I can solve the problem directly in ggplot2? Any
> ggplot2-expects out there?
>
> Sincerely,
> Eric
>
>
> On Sat, Feb 19, 2011 at 4:51 PM, Scott Chamberlain
> <myrmecocystus at gmail.com> wrote:
>
> require(ggplot2)
>
> data(diamonds)
>
> diamonds <- diamonds[1:100,c(2,7)]
>
> # use ddply in plyr package (loaded with ggplot2) to get data to plot
>
> diamonds_df <- ddply(diamonds, .(cut), summarise,
>
> mean_price = mean(price),
>
> se_price = sd(price)/sqrt(length(price))
>
> )
>
> limits <- aes(ymax = mean_price + se_price, ymin = mean_price - se_price)
>
> ggplot(diamonds_df, aes(x = cut, y = mean_price)) +
>
> geom_point() +
>
> geom_errorbar(limits, width=0.2)
>
> Sincerely,
>
> Scott Chamberlain
>
> Rice University, EEB Dept.
>
> On Saturday, February 19, 2011 at 3:12 PM, Eric Fail wrote:
>
> Can't anybody give me a hint on how to solve this? I even bought the
> ggplot2-book, so you could also give a page (or a series of pages).
>
> Thanks,
> Eric
>
> On Thu, Feb 17, 2011 at 10:19 AM, Eric Fail <eric.fail at gmx.com> wrote:
>
> Dear R-list
>
> I'm working with with geom_errorbar; specifically I'm trying to
> reproduce the example Hadley Wickham have on
> http://had.co.nz/ggplot2/geom_errorbar.html (all in the button of the
> page) where he makes an nice plot with errorbars and then draw lines
> between the points.
>
> What confuses me is the 'limits' he defines for the errorbars from the
> se variable.
>
> First he creates a dataset,
>
> df <- data.frame(
>  trt = factor(c(1, 1, 2, 2)),
>  resp = c(1, 5, 3, 4),
>  group = factor(c(1, 2, 1, 2)),
>  se = c(0.1, 0.3, 0.3, 0.2)
> )
>
> # library(ggplot2)
>
> and then he creates some limits from the se variables.
>
> limits <- aes(ymax = resp + se, ymin=resp - se)
>
> [elements omitted]
>
> # and then he creates the plot (I'm interested in).
>
> p <- ggplot(df, aes(colour=group, y=resp, x=trt))
> p + geom_line(aes(group=group)) + geom_errorbar(limits, width=0.2)
>
> I can (of course) get Hadley's example to run, but I can't do it on my
> data as I don't have a 'se' variable/don't know how to create it. I
> have a group variable, a treatment variable, and a response variable,
> but no se variable.
>
> Could anyone out there explain how I create a 'se' variable in my data?
>
> I'm sure my reasoning is the one that is off, and not ggplot2 (I'm a big
> fan).
>
> Your help is appreciated!
>
> Thanks,
> Eric
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list