Skip to content

Balance geom label whitespace #5663

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

Merged
merged 3 commits into from
Jan 26, 2024

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Jan 25, 2024

This PR to the RC aims to fix an internally discussed issue.

Briefly, in the RC we always add the descent height at the bottom of the label, which is not always visually pleasing. In this PR, descent is also added to the top and descent is ignored when the label padding exceeds the descent.

I think a visual demonstration will perhaps explain it best.
In the current ggplot 3.4.4, the issue was that the descent was not applied at all, leaving some characters, like 'j' partially hanging outside the textbox with 0 padding.

library(ggplot2)
library(patchwork)

df <- data.frame(x = 1, y = 1, hjust = c(0, 1, 0, 1), vjust = c(0, 0, 1, 1))

p <- ggplot(df) +
  aes(
    x = x, y = y, 
    hjust = hjust, vjust = vjust,
    label = paste0("hjust = ", hjust, ", vjust = ", vjust)
  )

p1 <- p + geom_label(label.padding = unit(0, "pt")) + 
  labs(title = "padding = 0pt")
p2 <- p + geom_label(label.padding = unit(5, "pt")) + 
  labs(title = "padding = 5pt")
p1 / p2

Created on 2024-01-25 with reprex v2.1.0

The current development version mitigates this issue by always adding the descent to the bottom of the label (by means of #5030).
There are two issues with this:

  • It looks imbalanced to have plenty of whitespace at the bottom and not at the top.
  • The application of vjust is awkward, having the descent poke out beneath the anchor point when vjust = 0.

Same code with current RC branch:

This PR applies the descent only if the padding is insufficient, and simultaneously at the top and the bottom of the label.
This fixes the balance issue mentioned before. While at it, I also fixed the vjust issue.

Same code with this PR:

@teunbrand teunbrand changed the base branch from main to rc/3.5.0 January 25, 2024 14:10
@teunbrand teunbrand added this to the ggplot2 3.5.0 milestone Jan 25, 2024
Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

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

LGTM

@teunbrand teunbrand merged commit 87abade into tidyverse:rc/3.5.0 Jan 26, 2024
@teunbrand teunbrand deleted the balance_geom_label branch January 26, 2024 09:01
thomasp85 pushed a commit that referenced this pull request Feb 23, 2024
* balance labels

* get `vjust` right

* update snapshot
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