-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Conversation
…into angle_heuristic
# 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) |
There was a problem hiding this comment.
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').
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This PR aims to fix #4594.
Briefly it copies the heuristic from
guide_axis(angle)
to the following guides:guide_colourbar()
,guide_coloursteps()
andguide_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.
Created on 2024-06-25 with reprex v2.1.0