csdaw / ggprism

ggplot2 extension inspired by GraphPad Prism
https://csdaw.github.io/ggprism/
170 stars 21 forks source link

Error on add_pvalue with geom_jitter() aes #32

Closed ewissel closed 5 months ago

ewissel commented 5 months ago

Hey there,

I am currently running the following R code:

p_vals <- tibble::tribble(
  ~group1, ~group2, ~p.adj,   ~y.position,
  "audi",   "dodge",     0.01 , 3.99,
  "audi",   "chevrolet",  0.01,  3.5
)

mpg %>%
  mutate(displ = as.numeric(displ)) %>% 
  ggplot(aes(x = manufacturer, y = displ, fill = manufacturer))+
  geom_bar(position=position_dodge(), stat="identity") +
  geom_jitter(height = 0, width = 0.2, size = 4) +
  ylim(0,4) + 
  theme_prism(base_size = 16)+
  scale_fill_manual(values = cbbPalette)+ 
  theme(legend.position = "none") +
  add_pvalue(p_vals, label = "p = {p.adj}", tip.length = 0, label.size = 4)

And receiving the following error:

Error:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `FUN()`:
! object 'manufacturer' not found
Backtrace:
  1. base (local) `<fn>`(x)
  2. ggplot2:::print.ggplot(x)
  4. ggplot2:::ggplot_build.ggplot(x)
  5. ggplot2:::by_layer(...)
 12. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
 13. l$compute_aesthetics(d, plot)
 14. ggplot2 (local) compute_aesthetics(..., self = self)
 15. base::lapply(aesthetics, eval_tidy, data = data, env = env)
 16. rlang (local) FUN(X[[i]], ...)
Error in (function (mapping = NULL, data = NULL, stat = "bracket", position = "identity", :

ℹ Error occurred in the 3rd layer.
Caused by error in `FUN()`:
! object 'manufacturer' not found

When I run the same code without the add_pvalues() command, it creates the plot. I can also add pvalue lines with geom_signif(), but those stay small / don't get larger with the theme_prism(base_size=16) argument so I'd like to make the add_pvalues() arg work .

Thanks for any advice! Not sure how to fix this one~

csdaw commented 5 months ago

Hi there,

Thanks, this is a known bug (see #31 for example). The solution for now is to move fill = manufacturer into geom_bar().

Specifically:

mpg %>%
  mutate(displ = as.numeric(displ)) %>% 
  ggplot(aes(x = manufacturer, y = displ))+
  geom_bar(aes(fill = manufacturer), position=position_dodge(), stat="identity") +
  geom_jitter(height = 0, width = 0.2, size = 4) +
  ylim(0,4) + 
  theme_prism(base_size = 16)+
  scale_fill_manual(values = cbbPalette)+ 
  theme(legend.position = "none") +
  add_pvalue(p_vals, label = "p = {p.adj}", tip.length = 0, label.size = 4)

Hope this helps, Charlotte

ewissel commented 5 months ago

since there is another open issue of the same bug i'll close this. thanks for your help!