Skip to content

Bug in setting fill of horizontal boxplots #4124

Closed

Description

@markpayneatwork

I suspect I have found a bug in geom_boxplot() when trying to set the color of the box to be a computed variable. ie.

> library(ggplot2)

> #Horizontal boxes with fill doesn't work with computed variables 
> ggplot(iris)+  geom_boxplot(aes(y=Species,x=Petal.Width,fill=..middle..)) 
Error in FUN(X[[i]], ...) : object 'middle' not found 
> ggplot(iris)+   geom_boxplot(aes(y=Species,x=Petal.Width,fill=..lower..))
 Error in FUN(X[[i]], ...) : object 'lower' not found
>

Other related variants on this theme seem to work ok

library(ggplot2)
#Vertical boxes with fill works with computed variables
ggplot(iris)+
  geom_boxplot(aes(x=Species,y=Petal.Width,fill=..middle..))

#Horizontal boxes without fill works
ggplot(iris)+
  geom_boxplot(aes(y=Species,x=Petal.Width))

#Horizontal boxes with categorical variables works
ggplot(iris)+
  geom_boxplot(aes(y=Species,x=Petal.Width,fill=Species))

Running ggplot Version: 3.3.2.9000 on Windows, built from the master branch on Github 2020-07-09 16:14:23 UTC

Activity

  1. yutannihilation commented on Jul 10, 2020

    @yutannihilation
    Member

    Thanks. This seems a documentation problem. You need x prefix for some of the computed variables if you draw geom_boxplot() horizontally (note that the latest recommended way to refer to computed variables is after_stat(var) instead of ..var..):

    library(ggplot2)
    
    ggplot(iris) +
      geom_boxplot(aes(y = Species, x = Petal.Width, group = Species, fill = after_stat(xmiddle)))

    Created on 2020-07-10 by the reprex package (v0.3.0)

  2. clauswilke commented on Jul 10, 2020

    @clauswilke
    Member

    This is the first I hear of this feature, so yes, definitely a documentation problem. :-)

    Is this documented anywhere currently? Do we also have ymiddle for regular orientation?

  3. yutannihilation commented on Jul 11, 2020

    @yutannihilation
    Member

    We have middle (and lower and upper) for x aesthetics, which feels a bit tricky.

    knitr::kable(
      data.frame(
        x = ggplot2:::ggplot_global$x_aes,
        y = ggplot2:::ggplot_global$y_aes
      )
    )
    x y
    x y
    xmin ymin
    xmax ymax
    xend yend
    xintercept yintercept
    xmin_final ymin_final
    xmax_final ymax_final
    xlower lower
    xmiddle middle
    xupper upper
    x0 y0

    Created on 2020-07-11 by the reprex package (v0.3.0)

  4. yutannihilation commented on Jul 11, 2020

    @yutannihilation
    Member

    I created a quick pull request for this (#4128). Honestly, I feel there would be some smarter way to fix this, but I think this is better than nothing for now...

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions