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

Copy angle heuristic for more guides. #5957

Merged
merged 11 commits into from
Aug 28, 2024

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #4594.

Briefly it copies the heuristic from guide_axis(angle) to the following guides: guide_colourbar(), guide_coloursteps() and guide_bins().
Note that angle heuristic is applied in GuideLegend$override_elements() even though GuideLegend does not use the heuristic (per #4594 (comment)). While not applied in GuideLegend, it is inherited by and used in child guides.

Some examples. Note that for labels placed close to the edge (1st example '6000'), the label may escape the allocated space, but there is no easy fix for this at the moment.

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

p <- ggplot(msleep, aes(brainwt, sleep_total, colour = bodywt)) +
  geom_point(na.rm = TRUE)

p + guides(colour = guide_colourbar(angle = 45))

p + guides(colour = guide_coloursteps(angle = 45))

p + guides(colour = guide_bins(angle = 45))

Created on 2024-06-25 with reprex v2.1.0

Comment on lines +579 to +588
# Taking the sign of the (co)sine snaps the value to c(-1, 0, 1)
# Doing `x / 2 + 0.5` rescales it to c(0, 0.5, 1), which are good values for justification
# The rounding step ensures we can get (co)sine to exact 0 so it can become 0.5
# which we need for center-justifications
cosine <- sign(round(cos(radian), digits)) / 2 + 0.5
sine <- sign(round(sin(radian), digits)) / 2 + 0.5

# Depending on position, we might need to swap or flip justification values
hjust <- switch(position, left = cosine, right = 1 - cosine, top = 1 - sine, sine)
vjust <- switch(position, left = 1 - sine, right = sine, top = 1 - cosine, cosine)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm very pleased with this bit replacing a whole slew of nested if blocks.
cyclocomp::cyclocomp() says complexity has been reduced from 28 to 8 (>15 is generally considered 'too complex').

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 3109d11 into tidyverse:main Aug 28, 2024
13 checks passed
@teunbrand teunbrand deleted the angle_heuristic branch August 28, 2024 07:55
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.

Permit guide_axis() angle heuristics in guide_legend(), etc.
2 participants