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

Ista Zahn izahn at psych.rochester.edu
Sun Feb 20 01:59:48 CET 2011


Hi Eric,
Again, it's not really a ggplot question. If you want to plot
separately by color, you need to calculate means and standard errors
separatly by color also:

data(diamonds)

 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(x = cut, y = mean_price, color=color)) +
 geom_point(position=position_dodge(width=0.9)) +
 geom_errorbar(limits, position=position_dodge(width=0.9), width=0.9)

see

?ddply

For details.

Best,
Ista
On Sat, Feb 19, 2011 at 10:58 PM, Eric Fail <eric.fail at gmx.com> wrote:
> Hi Scott,
>
> Thank you for ta king 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.
>>
>>
>
> ______________________________________________
> 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list