Skip to content

Axis caps #5289

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

Merged
merged 5 commits into from
May 22, 2023
Merged

Axis caps #5289

merged 5 commits into from
May 22, 2023

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #4907.

Briefly, it adds a cap argument to guide_axis() that truncates the axis line to the breaks. There are 4 different options, presented below. Also TRUE/FALSE are shortcuts for "both"/"none".

I've thought about allowing user-defined coordinates as the capping points, either by function or input on data values, but that'd complicate the implementation quite a bit. I also thought that this is probably simple enough not to separate out to a separate guide.

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

ggplot(mtcars, aes(hp, disp)) +
  geom_point() +
  theme(axis.line = element_line()) +
  guides(
    x = guide_axis(cap = "both"),
    y = guide_axis(cap = "upper"),
    y.sec = guide_axis(cap = "lower"),
    x.sec = guide_axis(cap = "none")
  )

Created on 2023-04-26 with reprex v2.0.2

@grantmcdermott
Copy link

+1ing as a user who has been looking forward to this capability for a while :-)

OTOH am I correct in thinking that, even with this PR and #4879, there still isn't a way to enable this capping behaviour globally? E.g. You still can't add guides as part of theme_set(), right?

@teunbrand
Copy link
Collaborator Author

That's right, I haven't added this as a theme option, but it might be something to consider. You can of course do the following:

capped_axes <- guides(
    x = guide_axis(cap = "both"),
    y = guide_axis(cap = "upper"),
    y.sec = guide_axis(cap = "lower"),
    x.sec = guide_axis(cap = "none")
  )
  
p1 <- ggplot(...) + ... + capped_axes
P2 <- ggplot(...) + ... + capped_axes
# etc.

@grantmcdermott
Copy link

Thanks, your suggestion is effectively what I do now (with your very nice ggh4x package). But I do think there’s additional value in make it “themeable”. Adding my support to your consideration.

@teunbrand
Copy link
Collaborator Author

I think it might make more sense to fold in #4271 first before adding this as a theme setting, that way you could set default guides in the theme.

@grantmcdermott
Copy link

SGTM, thanks Teun.

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 222d33d into tidyverse:main May 22, 2023
@teunbrand teunbrand deleted the axis_cap branch May 22, 2023 17:16
@teunbrand teunbrand mentioned this pull request Jun 22, 2023
@eejd
Copy link

eejd commented Jul 30, 2024

I know this is merged, but I wanted to second that the option to switch guides capping from complete to truncated should be supported as a theme element / option as well. I have been waiting for this feature and have used various workarounds, but I realized recently working on a large project where I needed to change a lot of aesthetics across a large number of plots, the ability to reformat as much as possible via theme application is very useful.

@teunbrand
Copy link
Collaborator Author

The way guides are implemented, that is not an easy feature to incorporate.
The capping is calculated before axes get access to theme settings, so at that point it is too late to truncate.
At the point axes get access to theme settings, it no longer has access to the scales/coord to rescale or deform the outermost positions.

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.

Natively Cap the Axis Lines, as featured on the lemon package (feature)
4 participants