0

Somewhere I'm making a very simple mistake. Thank you for pointing out where this mistake is happening...

I'd like to create a violin plot with overlaying confidence intervals and a group means. Much like the example at this terrific new package.

The generic violin plot without any CI's works fine:

p <- ggplot(data=diamonds, aes(x=cut, y=price))
p + geom_violin(aes(x=cut, y=price)) +
  geom_jitter(height = 0, width = 0.1, alpha = 0.05) +
  geom_crossbar(stat="summary", fun.y=mean, fun.ymax=mean, fun.ymin=mean, 
                fatten=2, width=.5)

My issues is incorporating the confidence intervals.

I start by creating the necessary summary stats:

errbar_lims <- group_by(diamonds, cut) %>% 
  summarize(mean=mean(price), se=sd(price)/sqrt(n()),             
  upper=mean+(2*se), lower=mean-(2*se))

Then I thought to add to the plot with geom_errorbar:

p <- ggplot(data=diamonds, aes(x=cut, y=price))
p + geom_violin(aes(x=cut, y=price)) +
  geom_jitter(height = 0, width = 0.1, alpha = 0.05) +
  geom_crossbar(stat="summary", fun.y=mean, fun.ymax=mean, fun.ymin=mean, 
                fatten=2, width=.5) +
  geom_errorbar(aes(x= ymax=errbar_lims$upper, ymin=errbar_lims$lower), 
                stat='identity', width=.25)

But ggplot's error keeps informing me that I'm mapping to a single group aesthetic instead of the 5 groups that I'm trying to plot.

Perhaps my mistake is how I've set the mapping at the very beginning?


UPDATE

After reviewing a few of the comments after posting initially, the full script now looks as follows:

errbar_lims <- group_by(diamonds, cut) %>% 
  summarize(mean=mean(price), se=sd(price)/sqrt(n()), 
        upper=mean+(2*se), lower=mean-(2*se))

p <- ggplot(data=diamonds, aes(x=cut, y=price))
p + geom_violin(aes(x=cut, y=price)) +    
  geom_jitter(height = 0, width = 0.1, alpha = 0.05) +
  geom_crossbar(stat="summary", fun.y=mean, fun.ymax=mean, fun.ymin=mean, 
            fatten=2, width=.5) +
  geom_errorbar(aes(x = cut, ymin = lower, ymax = upper), errbar_lims, inherit = FALSE)

Yet an error persists:

Error in FUN(X[[i]], ...) : object 'price' not found
Devon O'Rourke
  • 237
  • 2
  • 11
  • 3
    You shouldn't specify `aes` with `$lower`. Pass data into `geom_errorbar` as argument and specify values as in any other geom layer. Something like this: `geom_errorbar(aes(ymin = lower, ymax = upper), errbar_lims)` – pogibas Sep 03 '18 at 12:23
  • Something odd: `Error in FUN(X[[i]], ...) : object 'price' not found` Is there any chance that the arguments you've suggested are failing are because the data containing the CI information are in a separate data.frame object (errbar_lims) than what the rest are (diamonds)? – Devon O'Rourke Sep 03 '18 at 14:14
  • In the last line `x= ymax=errbar_lims$upper` is not valid R code. – Rui Barradas Sep 03 '18 at 15:44
  • 1
    @DevonO'Rourke Your intuition is in the right direction. The layer's looking for `price` based on the top level `aes(y = price)`. Adding `inherit.aes = FALSE` will negate that. But you'll need to specify `x = cut` in this case too: `geom_errorbar(aes(x = cut, ymin = lower, ymax = upper), errbar_lims, inherit = FALSE)` – Z.Lin Sep 03 '18 at 15:47
  • Thanks @Z.Lin. It worked after adjusting the `inherit = FALSE` to `inherit.aes=FALSE` – Devon O'Rourke Sep 03 '18 at 19:58

1 Answers1

1

Applied to that fabulous code on github you pointed me too.

library(ggplot2)
View(ggplot2::diamonds)

ggstatsplot::ggbetweenstats(data = ggplot2::diamonds, x=cut, y=price, messages=FALSE)

tcratius
  • 525
  • 6
  • 15