Skip to content

Boxplot staples - #5372

Merged
teunbrand merged 6 commits into
tidyverse:mainfrom
teunbrand:boxplot_staples
Aug 17, 2023
Merged

teunbrand merged 6 commits into
tidyverse:mainfrom
teunbrand:boxplot_staples

Conversation

@teunbrand

Copy link
Copy Markdown
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

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Member

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
@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