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

Rotated axis labels are not properly aligned #1878

Closed
clauswilke opened this issue Oct 26, 2016 · 7 comments
Closed

Rotated axis labels are not properly aligned #1878

clauswilke opened this issue Oct 26, 2016 · 7 comments
Assignees
Labels
bug an unexpected problem or unintended behavior

Comments

@clauswilke
Copy link
Member

In the following figure, the axis tick labels along the top x axis are not properly aligned. They all sit completely to the left of the axis tick rather than centered:

mtcars2 <- mtcars[1:15, ]
mtcars2$name <- row.names(mtcars2)
ggplot(mtcars2, aes(x = name, y = mpg, fill = name)) + 
  geom_bar(stat = 'identity', position = "identity") + 
  scale_y_reverse() +
  guides(fill = FALSE) +
  theme(axis.text.x = element_text(angle = 90, hjust=0)) +
  scale_x_discrete(position = "top")

image

No setting of vjust that I can add changes this, as far as I can tell. E.g., the following code creates the exact same output:

ggplot(mtcars2, aes(x = name, y = mpg, fill = name)) + 
  geom_bar(stat = 'identity', position = "identity") + 
  scale_y_reverse() +
  guides(fill = FALSE) +
  theme(axis.text.x = element_text(angle = 90, vjust=0.5, hjust=0)) +
  scale_x_discrete(position = "top")

As does any other vjust setting, including completely crazy ones such as -10 or 10.

@hadley
Copy link
Member

hadley commented Oct 26, 2016

Is this new in 2.2.0?

@clauswilke
Copy link
Member Author

I think it's new to 2.2.0, because vjust=0.5 works fine if I keep the x axis at the bottom of the plot (and different vjust settings move the text left and right in this case):

ggplot(mtcars2, aes(x = name, y = mpg, fill = name)) + 
     geom_bar(stat = 'identity', position = "identity") + 
     scale_y_reverse() +
     guides(fill = FALSE) +
     theme(axis.text.x = element_text(angle = 90, vjust=0.5, hjust=1))

image

ggplot(mtcars2, aes(x = name, y = mpg, fill = name)) + 
     geom_bar(stat = 'identity', position = "identity") + 
     scale_y_reverse() +
     guides(fill = FALSE) +
     theme(axis.text.x = element_text(angle = 90, vjust=0, hjust=1))

image

@hadley
Copy link
Member

hadley commented Oct 27, 2016

Thanks, makes sense. This one's for you @thomasp85

@hadley hadley added the bug an unexpected problem or unintended behavior label Oct 27, 2016
@thomasp85
Copy link
Member

This is not a bug per se, but rather an inconvenience fostered by a wish for both theme inheritance and nice defaults...

axis.text.x.top inherits from axis.text.x, but in order to look good by default it sets margin and just explicitly deferent from axis.text.x and these are thus not inherited when axis.text.x is set in a call to theme(). Setting it directly (theme(axis.text.x.top = element_text(vjust = 0.5))) solves your problem...

@clauswilke
Copy link
Member Author

Perfect, thanks. Is this explained somewhere in the documentation?

@apuhegde
Copy link

apuhegde commented Mar 20, 2018

That works for labels on the top. However, I'm facing this exact issue when I have labels on the bottom x-axis. Is there a solution to that? My R version is 3.4.3., RStudio 1.1.423
Thanks!

@lock
Copy link

lock bot commented Sep 16, 2018

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 Sep 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants