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

Boxplot staples #5372

Merged
merged 6 commits into from
Aug 17, 2023
Merged

Boxplot staples #5372

merged 6 commits into from
Aug 17, 2023

Conversation

teunbrand
Copy link
Collaborator

This PR aims to implement one of the features mentioned in #5216, namely: staples for boxplots.

Briefly, it adds a new argument staplewidth argument to geom_boxplot() that allows one to draw staples at the end of whiskers.

A few additional notes:

  • Staples are only drawn when staplewidth != 0 and 0 is the default, so existing plots won't be affected.
  • The staplewidth is relative to the width of the box.
  • Staples are styled in the same way as whiskers.

Some demonstrations:

Here you can see that the staplewidth is relative, as it adapts to the size of variable-width boxes.

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

p <- ggplot(mpg, aes(drv, displ))

p + geom_boxplot(staplewidth = 1, varwidth = TRUE)

Here you can see that it works as one would expect with a trick to make half-boxplots:

p + geom_boxplot(aes(xmax = after_scale(x)), staplewidth = 0.5)

With negative staplewidth you can flip the staples in half-boxplots.

p + geom_boxplot(aes(xmax = after_scale(x)), staplewidth = -0.5)

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

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit c76b9ae into tidyverse:main Aug 17, 2023
12 checks passed
@teunbrand teunbrand deleted the boxplot_staples branch August 17, 2023 16:19
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

Successfully merging this pull request may close these issues.

2 participants