Skip to content

Axis text outside panel with inward ticks #4392

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 3 commits into from
Apr 8, 2021

Conversation

teunbrand
Copy link
Collaborator

This PR attempts to fix #4276 by following the suggestion at the end of that issue.

In brief, the current axis text placement with inward ticks is uncomfortable:

# Current behaviour
library(ggplot2)

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  guides(x.sec = "axis", y.sec = "axis") +
  theme(axis.ticks.length = unit(-11, "pt"))

Created on 2021-03-27 by the reprex package (v1.0.0)

With this PR, text placement is outside the panel boundary regardless of tick length:

library(ggplot2)

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  guides(x.sec = "axis", y.sec = "axis") +
  theme(axis.ticks.length = unit(-11, "pt"))

Created on 2021-03-27 by the reprex package (v1.0.0)

You can still get the same plot as before, but it costs more effort to do the wrong thing:

library(ggplot2)

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  guides(x.sec = "axis", y.sec = "axis") +
  theme(axis.ticks.length  = unit(-11, "pt"),
        axis.text.x.top    = element_text(margin = margin(b = -11 + 2.2)),
        axis.text.x.bottom = element_text(margin = margin(t = -11 + 2.2)),
        axis.text.y.left   = element_text(margin = margin(r = -11 + 2.2)),
        axis.text.y.right  = element_text(margin = margin(l = -11 + 2.2)))

Created on 2021-03-27 by the reprex package (v1.0.0)

Copy link
Member

@clauswilke clauswilke left a comment

Choose a reason for hiding this comment

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

This looks good to me.

@clauswilke clauswilke added this to the ggplot2 3.3.4 milestone Mar 27, 2021
@thomasp85 thomasp85 merged commit a878bdb into tidyverse:master Apr 8, 2021
@thomasp85
Copy link
Member

Thanks!

@teunbrand teunbrand deleted the inward_ticks_text branch April 8, 2021 11:24
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.

Feature request: better text placement with inward facing axis ticks
3 participants