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

Extra boxplot features #5423

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Sep 15, 2023

This PR aims to fix #5126 and fix #3459.

Briefly, it adds 9 new aesthetics to geom_boxplot() to set the colour, linewidth and linetype for whiskers, staples and median line.
These all work by setting the default as NULL and then inheriting from the main colour, linewidth or linetype if not set explicitly.

I'm still unsure about some decisions, so that is why this PR is marked work in progress (WIP). Some considerations:

  • Should we use snake_case or dot.case for these aesthetics?
  • I had to include the median line settings in geom_crossbar() to get them to work in the boxplot. Should we include these as aesthetics to geom_crossbar() as well? If yes, is median_{aes} appropriate, or is middle_{aes} more appropriate?
  • Should these new aesthetics be mapped (currently the case), or fixed (like outlier.colour)? If they should be fixed, the scale interaction point below becomes moot.
  • I'm unsure about the interaction with scales. I'm reluctant to mirror all scale types for these new aesthetics, but having to set scale_colour_hue(aesthetics = "median_colour") is also a bit of pain for the user. In addition, scale_linewidth_*() and scale_linetype_*() scales don't expose the aesthetics argument, so there isn't really a way to scale these.
  • For completeness, should we mirror the setting for the box as well? I.e. introduce box_colour, box_linewidth and box_linetype?

A demonstration:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

ggplot(mpg, aes(class, displ)) +
  geom_boxplot(
    # Using new aesthetics as mapped aesthetics
    aes(median_colour = class, whisker_colour = class),
    # Using them as unmapped aesthetics
    whisker_linetype = 2, staple_linewidth = 1,
    staplewidth = 0.5 # show staples
  ) +
  # The mapped aesthetics need to be explicit in scales
  scale_colour_hue(aesthetics = c("median_colour", "whisker_colour"))

Created on 2023-09-15 with reprex v2.0.2

@teunbrand
Copy link
Collaborator Author

I've removed these extra settings as aesthetics and instead added them as fixed parameters. To reproduce the previous boxplot, one can now use:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
#> Warning: package 'testthat' was built under R version 4.3.1

ggplot(mpg, aes(class, displ, colour = class)) +
  geom_boxplot(
    box.colour = "black",
    staple.colour = "black",
    staple.linewidth = 1,
    whisker.linetype = 2,
    staplewidth = 0.5
  )

Created on 2023-10-27 with reprex v2.0.2

@teunbrand teunbrand changed the title WIP: Extra boxplot features Extra boxplot features Oct 27, 2023
@teunbrand teunbrand marked this pull request as ready for review October 27, 2023 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants