-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Unexpected behavior in element_text() horizontal justification #2653
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
Comments
Thanks for reporting this. I believe this bug is due to the change in how |
@hadley @clauswilke I think we should revert this change, it's going to break a lot of users' plots. |
@karawoo I'll look into it. However, I'm not yet convinced about the cause of this problem, because I checked for this particular issue when I made the patch, if I remember correctly. I think this has actually to do with changed grob heights due to fixed descenders. A workaround exists, but I agree it's not intuitive (why library(ggplot2)
mtcars$name <- rownames(mtcars)
ggplot(mtcars, aes(x=name, y=carb)) +
geom_bar(stat="identity") + theme(axis.text.x=element_text(angle=60, hjust=1, vjust = 0.65)) Created on 2018-05-23 by the reprex package (v0.2.0). |
When looking at this in debug mode, we see that for axis tick labels along the x axis the reference point travels vertically for different library(ggplot2)
mtcars$name <- rownames(mtcars)
ggplot(mtcars, aes(x=name, y=carb)) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle=60, hjust=1, vjust = 0, debug = TRUE)) ggplot(mtcars, aes(x=name, y=carb)) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle=60, hjust=1, vjust = 0.5, debug = TRUE)) ggplot(mtcars, aes(x=name, y=carb)) +
geom_bar(stat="identity") +
theme(axis.text.x=element_text(angle=60, hjust=1, vjust = 1, debug = TRUE)) There are two possible alternative ways to fix this:
|
I see what the problem is now. It arises in Lines 47 to 48 in eecc450
with
but that's not a general solution (it fails at other angles). Let me ponder this some more. I'd rather fix this in a forward-looking manner than going back to behavior that is broken in other ways. |
I've been pondering this issue the whole afternoon and I'm not sure how to fix this. (We can revert, of course.) The bigger problem I'm seeing is that we are using two different justification systems throughout ggplot, and things break one way or another when we mix them (as is the case for axis tick labels): One system uses a fixed reference point and moves the text relative to that point. E.g., Since I don't even know what the correct behavior is in the general case, I just created a PR that reverts to the previous model specifically when the justification model is mixed: #2654 With this PR: library(ggplot2)
mtcars$name <- rownames(mtcars)
ggplot(mtcars, aes(x=name, y=carb)) +
geom_bar(stat="identity") + theme(axis.text.x=element_text(angle=60, hjust=1)) Created on 2018-05-23 by the reprex package (v0.2.0). |
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/ |
In the upcoming release of ggplot2 (2.3.0) the behavior of element_text() has changed. Specifically it looks like the justification anchors have changed in relation to the text. For Example:
In the current release:

In the development version:

The text was updated successfully, but these errors were encountered: