You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Request: Axis Text Warning or Automatic Rotation when Labels Overlap #3281
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.
# 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))
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.
@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.
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).
Activity
thomasp85 commentedon Apr 30, 2019
This is already being considered and will hopefully appear in the future
paleolimbot commentedon 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 = TRUEto 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 commentedon 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, andhjustare 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 currentelement_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 commentedon 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 typeguide_axis(check.overlap = ..., angle = ..., n_dodge = ...)(current thinking is that this goes incoord_cartesian()and/or inscale_(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
hjustandvjustthat 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 commentedon 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/