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

Orientation-aware key glyphs #4757

Merged
merged 6 commits into from
Mar 23, 2022
Merged

Conversation

mjskay
Copy link
Contributor

@mjskay mjskay commented Mar 15, 2022

This PR closes #4732 by providing orientation-aware key glyphs for geom_boxplot(), geom_crossbar(), geom_pointrange(), and geom_linerange().

For example, before these changes:

library(ggplot2)
library(patchwork)

df <- data.frame(
  middle = 1:2,
  lower = 0:1,
  upper = 2:3,
  min = -1:0,
  max = 3:4,
  group = c("a","b")
)

p <- ggplot(df, aes(
  x = middle,
  y = group,
  xmiddle = middle,
  xlower = lower,
  xupper = upper,
  xmin = min,
  xmax = max,
  color = group
))

(p + geom_boxplot(stat = "identity")) +
  (p + geom_crossbar()) +
  (p + geom_linerange()) +
  (p + geom_pointrange())

Created on 2022-03-15 by the reprex package (v2.0.1)

After these changes:

Specifically, this PR:

  • modifies draw_key_boxplot(), draw_key_crossbar(), and draw_key_pointrange() to be orientation-aware
  • provides a new key glyph, draw_key_linerange(), which is an orientation-aware version of draw_key_path() (because draw_key_path() is used by other geoms whose legends should not be orientation-aware, I could not simply make draw_key_path() orientation aware).
  • changes the key glyph of geom_linerange() to be draw_key_linerange() instead of draw_key_path().
  • adds tests for horizontal and vertical legends. This might be too many graphical tests; I'm happy to reduce them --- perhaps by removing the vertical tests (the old behavior) and just leaving the horizontal ones. I have condensed the visual tests down to just two for horizontal legends; I'm not sure there's a clear way to reduce them to fewer.

This PR does not add orientation-aware key glyphs to geom_errorbar() or geom_errobarh(), as these geoms consist of both horizontal and vertical lines, so it seemed reasonable to leave them as is. In fact, geom_errorbarh() already has the same key glyph as geom_errorbar() rather than one of the opposite orientation, corroborating my sense that the legends for these two geoms do not need to be orientation-aware.

Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @mjskay

@hadley hadley merged commit 0e64d9c into tidyverse:main Mar 23, 2022
schloerke added a commit to schloerke/ggplot2 that referenced this pull request Mar 23, 2022
* master: (320 commits)
  Orientation-aware key glyphs (tidyverse#4757)
  Fix warning in geom_violin with draw_quantiles (tidyverse#4654)
  Fix misalignment in geom_dotplot when stackratio != 1 and stackdir != "up" (tidyverse#4734)
  Replace coord_equal in scale-identity.r (tidyverse#4759)
  Unified message format in `geom_smooth()` (tidyverse#4634)
  Add parentheses to mentions of binned_scale in scale-alpha and scale-viridis documentation (tidyverse#4735)
  Update geom-boxplot.r (tidyverse#4744)
  Remove unneeded backslash from diamonds docs (tidyverse#4711)
  Re-document
  Warn on unsupported geoms in annotate() (tidyverse#4721)
  Re-document
  Add Trump to presidential terms dataset (tidyverse#4702)
  Corrects bins / binwidth override documentation (tidyverse#4720)
  Update infrastructure to new best practices (tidyverse#4748)
  remove stringsAsFactor for the mapped_discrete as.data.frame method (tidyverse#4750)
  Workflow updates (tidyverse#4747)
  Ensure output is numeric even if ifelse clause is NA (tidyverse#4692)
  Add non_missing_aes to geom_tile (tidyverse#4683)
  Pass on binwidth and height to geom (tidyverse#4671)
  Check for range == NULL - happens if no data has been added to plot (tidyverse#4682)
  ...
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.

Legend glyphs for geom_pointrange do not follow their plotted direction
2 participants