Skip to content
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

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

Open
billdenney opened this issue Apr 30, 2019 · 4 comments

Comments

@billdenney
Copy link

commented Apr 30, 2019

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

This comment has been minimized.

Copy link
Member

commented Apr 30, 2019

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

@paleolimbot paleolimbot added guides 📏 wip feature and removed wip labels May 24, 2019

@paleolimbot

This comment has been minimized.

Copy link
Collaborator

commented Jun 19, 2019

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

This comment has been minimized.

Copy link
Member

commented Jun 19, 2019

@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

This comment has been minimized.

Copy link
Collaborator

commented Jun 19, 2019

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.