Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error bars in box plots #105

Closed
j3ypi opened this issue Aug 12, 2018 · 11 comments
Closed

Add error bars in box plots #105

j3ypi opened this issue Aug 12, 2018 · 11 comments

Comments

@j3ypi
Copy link

j3ypi commented Aug 12, 2018

I'm not quiet sure whether I don't understand the syntax or it's not implemented yet, but how do I add an errorbar to a box plot with ggpubr?

Lets say we have the same toy data all over again.

library(tidyverse)
library(ggpubr)
df <- tibble(
  iq = rnorm(100, 100, 15),
  condition = rep(c("A", "B"), each = 50),
  time = rep(c("t1", "t2", "t1", "t2"), each = 25)
)

What I like to have is something like this:

ggplot(df, aes(x = time, y = iq)) + 
  stat_boxplot(geom = "errorbar", width = 0.4)+
  geom_boxplot() 

But I haven't been able to implement the exact same mechanism with ggpubr. I tried

ggboxplot(df, 
      x = "time", 
      y = "iq",
      add = "mean_se", 
      error.plot = "errorbar")

but it appears that this function returns the errorbar of the mean and not the limits I want to border the box plots with.

@kassambara
Copy link
Owner

kassambara commented Aug 12, 2018

stat_boxplot() adds a specific errorbar to the box plot using median +/- 1.5*IQR.

In ggpubr, you have the generic option add = median_iqr, which is a non parametric alternative of mean_sd.

I think that, we need a new argument in ggboxplot(), for example show.errorbar or boxplot.errorbar.

@j3ypi
Copy link
Author

j3ypi commented Aug 13, 2018

I agree. In addition, median_iqr doens't work the same way as the example with ggplot. The errorbars aren't at the limits of the box plots and you'll see a vertical line crossing the boxplot, probably because the errorbar is added after the call of geom_boxplot() internally.

You can observe the same behavior, if you exchange the order of those two function calls like this

ggplot(df, aes(x = time, y = iq)) + 
  geom_boxplot() +
  stat_boxplot(geom = "errorbar", width = 0.4)

Or isn't this the code you had in mind with median_iqr?

ggboxplot(df, 
      x = "time", 
      y = "iq",
      add = "median_iqr", 
      error.plot = "errorbar")

@j3ypi
Copy link
Author

j3ypi commented Aug 13, 2018

I took a quick look at the ggboxplot() function. You have a lot of helper functions and checking going on in the background and I haven't checked if it works like this, but it should be as easy as adding something like

 p <- ggplot(data, aes_string(x, y))

 if (show.errobar) {
    p <- p + 
      stat_boxplot(geom = "errorbar", width = 0.4)
  } 

  p <- p + geom_exec(geom_boxplot, data = data,
                     color = color, fill = fill, linetype = linetype,
                     size = size, width = width, notch = notch,
                     position = position_dodge(0.8), size = size,...)

in line 198. In addition adding the boolean argument show.errobar to ggboxplot(), .opts and ggboxplot_core().

I never checked an unfamiliar package before, so I'll need some time to create a pull request (assuming this works), if you won't implement it yourself first anyways.

kassambara added a commit that referenced this issue Aug 13, 2018
kassambara added a commit that referenced this issue Aug 13, 2018
@kassambara
Copy link
Owner

New arguments added: bxp.errorbar and bxp.errorbar.width . The following R code should works:

library(ggpubr)
df <- tibble(
  iq = rnorm(100, 100, 15),
  condition = rep(c("A", "B"), each = 50),
  time = rep(c("t1", "t2", "t1", "t2"), each = 25)
)
  
  ggboxplot(df, 
            x = "time", 
            y = "iq",
            bxp.errorbar = TRUE)

rplot48

@j3ypi
Copy link
Author

j3ypi commented Aug 13, 2018

Man you work fast. Keep up the good work!

@kassambara
Copy link
Owner

Thank you for the feedback, highly appreciated!

@RoseString
Copy link

@kassambara This is a great functionality. I noticed that the default color for the error bar is black. Do you know if it can be changed to the one I set in "color="? Thank you very much!

@Keniajin
Copy link

Thanks, @kassambara for the update, when I try

ggboxplot(df, 
          x = "time", 
          y = "iq",
          fill="condition",
          bxp.errorbar = TRUE)

The error bar doesn't appear on each of the fill boxplots

@jaydoc
Copy link

jaydoc commented Jun 19, 2020

Hi there

Has this been fixed? When using a variable as the "fill" or "color" condition, the bxp.errorbar function seems tobe calculated for the entire dataset grouped by the "x" variable, rather than the subgroups created by the "fill" condition.

Thanks, @kassambara for the update, when I try

ggboxplot(df, 
          x = "time", 
          y = "iq",
          fill="condition",
          bxp.errorbar = TRUE)

The error bar doesn't appear on each of the fill boxplots

@kassambara
Copy link
Owner

@RoseString, @Keniajin, @jaydoc: fixed now, thanks!

@jaydoc
Copy link

jaydoc commented Jun 19, 2020

That was literally the quickest a developer has fixed an issue in my experience! Thanks a bunch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants