Skip to content

Whiskers and outliers for geom_boxplot with transformed scale #3336

Closed

Description

@mbertolacci

Boxplot whiskers and outliers seem to be incorrect when using geom_boxplot with a transformed scale. It probably affects notches too, but I haven't checked.

The hinges are fine, which makes sense—monotonic transformations preserve quantiles.

library(ggplot2)
df <- data.frame(x = 1, y = c(1, 4, 5, 6, 9))
print(ggplot(df, aes(x, y)) + geom_boxplot())

print(ggplot(df, aes(x, y)) + geom_boxplot() + scale_y_sqrt())

Created on 2019-05-20 by the reprex package (v0.3.0)

I'm using ggplot2 3.1.1.

Activity

  1. paleolimbot commented on May 20, 2019

    @paleolimbot
    Member

    You're right, boxplots are calculated based on the transformed values, which affects the calculation of outliers. However, StatBoxplot$compute_group() has access to the x and y scales (and their transformations), so it is possible to change this.

    https://github.com/tidyverse/ggplot2/blob/master/R/stat-boxplot.r#L82-L85

  2. added
    bug
    on May 21, 2019
  3. paleolimbot commented on May 27, 2019

    @paleolimbot
    Member

    It's worth noting that this (surprise at Stats being calculated on transformed values) has been reported several times (#2880, #2804), and at least once it was deemed as a won't-fix: #2804 (comment)

  4. mbertolacci commented on May 27, 2019

    @mbertolacci
    Author

    Ah, sorry I missed those issues—I only searched for boxplot related ones.

    Computing stats on the transformed values is potentially quite reasonable from a statistical perspective. If that's the way this goes, I would then just suggest a documentation update to note the behaviour since it's a bit surprising.

  5. clauswilke commented on Jun 1, 2019

    @clauswilke
    Member

    This topic came up at Lee Wilkinson's talk yesterday at SDSS 2019: Stats are computed on the transformed values. If you want the stats to be calculated on the original values then you need to work with a transformed coordinate system: https://ggplot2.tidyverse.org/reference/coord_trans.html

    This is common confusion in ggplot2: Many people try to use scales for things that should be done in coords. Setting axis limits without dropping data points is another common issue.

  6. paleolimbot commented on Jun 2, 2019

    @paleolimbot
    Member

    If that's true, it should be much easier to make a pretty looking plot with coord_trans() - right now the defaults don't produce the plot most people are looking for, xlim and ylim parameters aren't named the same as coord_cartesian(), non-default axis expansion isn't possible (#3338 and #2990), second axes aren't well-supported (#2990), and some of the nice things you can do like use NA as one of the limits aren't possible in coords (#2907). Even using breaks = scales::log_breaks() doesn't really work in coord_trans().

    library(ggplot2)
    
    p <- ggplot(diamonds, aes(cut, price)) +
      geom_boxplot() 
    
    p + aes(y = log10(price))

    p + scale_y_log10()

    p + coord_trans(y = "log10")

    p + coord_trans(y = "log10") + scale_y_continuous(breaks = scales::log_breaks())

    Created on 2019-06-02 by the reprex package (v0.2.1)

  7. lock commented on Dec 28, 2019

    @lock

    This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

  8. locked and limited conversation to collaborators on Dec 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buglayers 📈

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions