Skip to content

Feature Request: Axis Text Warning or Automatic Rotation when Labels Overlap #3281

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

Closed
billdenney opened this issue Apr 30, 2019 · 5 comments · Fixed by #3398
Closed

Feature Request: Axis Text Warning or Automatic Rotation when Labels Overlap #3281

billdenney opened this issue Apr 30, 2019 · 5 comments · Fixed by #3398
Labels
feature a feature request or enhancement guides 📏 wip work in progress

Comments

@billdenney
Copy link
Contributor

I sometimes work with data where the axis text may overlap, and it would be helpful if ggplot either warned when the axis text overlapped or even better could automatically adjust rotation and justification when overlap occurs.

library(ggplot2)

data_with_long_names <-
  data.frame(
    A=c(paste0(rep(LETTERS, 3), collapse=""), paste0(rep(letters, 3), collapse="")),
    B=1
  )

# Feature request: This gives a warning
ggplot(data_with_long_names, aes(x=A, y=B)) +
  geom_point()

# Option for consideration as an interface to automatically fix the issue
#ggplot(data_with_long_names, aes(x=A, y=B)) +
#  geom_point() +
#  theme(axis.text.x.overlap=element_text(angle=45, hjust=1))

Created on 2019-04-30 by the reprex package (v0.2.0).

@thomasp85
Copy link
Member

This is already being considered and will hopefully appear in the future

@paleolimbot paleolimbot added guides 📏 wip work in progress feature a feature request or enhancement and removed wip work in progress labels May 24, 2019
@paleolimbot
Copy link
Member

Referencing #3322 as I'm working on this today as part of that project. The current strategies I think that I can build in to the current axis function relatively easily are (1) smart ordering of the labels and using check.overlap = TRUE to hide "lower priority" labels, (2) stacking alternate labels into multiple rows/columns, and (3) rotating labels (you can already rotate the labels using the theme, but the details are hard to get right). Depending on how well this works, we'll see if one of these methods can't be applied by default.

@clauswilke
Copy link
Member

@paleolimbot Have you thought about how you want the user to turn on or off/customize this? I think ideally it would be encapsulated into a new element type, so that it can be all customized by setting the appropriate element type and arguments in the theme.

The reason why rotated labels are difficult is because we don't have enough parameters to specify how rotation should be performed. Just angle, vjust, and hjust are not enough, because in the general case you also need to specify the reference point around which the label is rotated. This doesn't fit into the current element_text() setup, and a general fix would again require a different element type. So it's best if we can rewrite this section of axis drawing code with that in mind.

For an example of the need for more parameters, see the readme here:
https://github.com/clauswilke/gridtext

@paleolimbot
Copy link
Member

Right now there's no way to get any extra information to draw_axis(), which will make this PR a bit useless until we can get a place where the user can type guide_axis(check.overlap = ..., angle = ..., n_dodge = ...) (current thinking is that this goes in coord_cartesian() and/or in scale_(x|y)_continuous(guide = ...)...but specifying in the scales gets hairy with non-cartesian coords). I'm mostly doing it first because it's easier to review as a PR, and because Hadley said to work on this part next.

It's currently possible to get rotated axis labels looking good at any angle with a very complicated combo of hjust and vjust that I imagine could be parameterized less confusingly using gridtext. With any luck I'll have a PR ready later today and I'd love any feedback you have on the text rotation (or any of it, really).

@lock
Copy link

lock bot commented Mar 27, 2020

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement guides 📏 wip work in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants