-
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
Axis guide for logarithmic ticks #5500
Conversation
Merge branch 'main' into axis_logticks # Conflicts: # DESCRIPTION # NAMESPACE # _pkgdown.yml # man/ggplot2-ggproto.Rd # tests/testthat/test-guides.R
Now has customisation options for short ticks. Long ticks inherit from Still unsure if there wouldn't be a better argument name than In example below, you can see that the smallest ticks can be turned off on the x-axis, and the inheritance on the y-axis. devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
ggplot(msleep, aes(bodywt, brainwt)) +
geom_point(na.rm = TRUE) +
scale_x_log10(guide = guide_axis_logticks(
short = 2,
short_theme = element_blank()
)) +
scale_y_log10(guide = guide_axis_logticks(
short_theme = element_line(linewidth = 2)
)) +
theme(
axis.minor.ticks.y.left = element_line(colour = "red")
) Created on 2023-11-22 with reprex v2.0.2 |
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
Thanks for the review Thomas! |
This PR aims to fix #5325, in particular through #5325 (comment).
Briefly, it adds a new axis guide,
guide_axis_logticks()
that draws ticks at log10-spaced positions.In comparison to
annotation_logticks()
:?annotation_logticks
says: "These tick marks probably make sense only for base 10.", which I agree with.clip = "off"
when outside. Of course, can do equivalent ofannotation_logticks(outside = FALSE)
by setting negative tick lengths.In comparison with
guide_axis()
:key
is used for the labels. The ticks get their own 'shadow'-key.axis.ticks.{position}.{aes}
theme setting, so no tick uses theaxis.minor.ticks.{position}.{aes}
setting.Why WIP?
annotation_logticks()
as superseded?guide_axis()
#5410 gets merged, we may think about providing a custombreaks
and/orlabels
argument.A bunch of demonstrations; ticks are placed in non-transformed, original data ranges. The label placement shows that
guide_axis_logticks()
does not intervene with that part of the guide and takes these verbatim from the scale.Of course, these ticks start to make more sense with a log transformation:
Tick lengths are set in the guide. If not a
<unit>
they are interpreted as relative to the theme's tick length setting.By default, the ticks are placed 1 order of magnitude outside the limits. To clip these to the actual limits rather than the expanded limits, you can set
expanded = FALSE
.When a user has manually transformed their data, the scale doesn't know about this so the ticks would be off. To intervene, one can set
prescale_base
to calculate appropriate ticks.Because ticks are placed in original data-space, this works naturally with compound transformations, like the reverse log10 transform.
When the data include 0 or negative numbers, the ticks are mirrored around 0.
The reason for this is as follows:
Of course, some decision then has to be made where near 0 one should stop drawing ticks, as otherwise one would end up with an infinite amount of ticks. The default is
0.1
but the user can control this too.Just to show that I'm not cheating this mirroring by seeing if the scale name is
"pseudo-log"
, we can do the same with custom transformations:Created on 2023-10-28 with reprex v2.0.2