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

Bug-fix: text justification with rotation not implemented in geom_label() #5424

Merged
merged 1 commit into from
Oct 4, 2023

Conversation

aphalo
Copy link
Contributor

@aphalo aphalo commented Sep 15, 2023

The code in geom_label() should pass to compute_just() the same arguments that geom_text() passes, including both x and y and angle in each of the two calls, one for hjust and one for vjust. This affects the handling of "inward" and "outward" justification that depending on the angle require different numerical values for justification.

Reprex run AFTER applying this bug fix.

(There is a separate problem in compute_just(), affecting both geometries as vjust for the 90 degrees rotated text seems to be "outward" instead of "inward", while hjust is correct. I will try fix this separate problem and make a separate pull request in a few days.)

library(ggplot2)

df <- data.frame(
  x = c(0, 0, 1, 1, 0.5),
  y = c(0, 1, 0, 1, 0.5),
  text = c("bottom-left", "top-left", "bottom-right", "top-right", "center-middle")
)

ggplot(df, aes(x, y, label = text)) +
  geom_text(hjust = "inward", vjust = "inward", angle = 90)

ggplot(df, aes(x, y, label = text)) +
  geom_text(hjust = "inward", vjust = "inward", angle = -90)

ggplot(df, aes(x, y, label = text)) +
  geom_label(hjust = "inward", vjust = "inward", angle = 90)

ggplot(df, aes(x, y, label = text)) +
  geom_label(hjust = "inward", vjust = "inward", angle = -90)

Created on 2023-09-15 with reprex v2.0.2

Now that geom_label() supports rotation with angle, it is necessary to pass the angle as the third argument to compute_just() as it is done in geom_text() to ensure correct text justification.
@aphalo aphalo changed the title Bug-fix: text justification with rotation in not implemented geom_label() Bug-fix: text justification with rotation not implemented in geom_label() Sep 15, 2023
@aphalo aphalo marked this pull request as ready for review September 15, 2023 16:30
@teunbrand
Copy link
Collaborator

To make it slightly more visual what the issue was:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

df <- data.frame(
  x = c(0, 0, 1, 1, 0.5),
  y = c(0, 1, 0, 1, 0.5),
  text = c("bottom-left", "top-left", "bottom-right", "top-right", "center-middle")
)

ggplot(df, aes(x, y, label = text)) +
  geom_label(hjust = "inward", vjust = "inward", angle = 90, colour = "red") +
  geom_text(hjust = "inward", vjust = "inward", angle = 90)

ggplot(df, aes(x, y, label = text)) +
  geom_label(hjust = "inward", vjust = "inward", angle = -90, colour = "red") +
  geom_text(hjust = "inward", vjust = "inward", angle = -90)

Created on 2023-09-18 with reprex v2.0.2

Copy link
Collaborator

@teunbrand teunbrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting together this PR! It looks good to me.
If you want, you can add either a separate news bullet, or add your username to the bullet below:

ggplot2/NEWS.md

Line 102 in 25f54d9

* `geom_label()` now uses the `angle` aesthetic (@teunbrand, #2785)

@aphalo
Copy link
Contributor Author

aphalo commented Sep 29, 2023

Thanks! This is such a small PR that there is no need to add my name, I think. In an earlier PR I wrote myself the whole matching bugfix for geom_text().

@teunbrand teunbrand merged commit 182405b into tidyverse:main Oct 4, 2023
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants