-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Improvements to geom_label()
#5030
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
Conversation
Sync branches
Hi @clauswilke, might I ask you to review this PR if you have some time to spare? I think you have the most experience with textboxes, so I'd welcome your suggestions. |
I think the problem you'll have is that if you have one label I've always felt that text at a given font size should have a defined ascent and descent regardless of content (and in fact, that's how ascent and descent are defined in typography), but grid doesn't work that way if I recall correctly, and as your examples show. |
Thanks that is a keen observation I hadn't thought of myself, so that is useful! I guess I could use In your example, it also seems like the space above |
Just to add my own opinion, we should def use the same descent irrespective of the content of the text box, and Not sure if supporting |
Thanks for the thoughts. I'll explore around a bit to see to what degree |
… into geom_label_angle
… into geom_label_angle
Some updates:
Showing the devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
data <- data.frame(
text = c('abc', 'def', 'gjq'),
angle = c(0, 45, 90),
x = c(1, 2, 3)
)
ggplot(data, aes(x, label = text, angle = angle)) +
geom_label(aes(y = 1), label.padding = unit(0, "lines"), size = 10) +
geom_label(
aes(y = 2), size = 10,
label.padding = margin(t = 20, r = 0, b = 5, l = 5)
) +
xlim(0.5, 3.5) +
ylim(0.5, 2.5) Created on 2023-01-16 with reprex v2.0.2 |
have you tested the impact on performance? Not that When this is merged it would be good to at least inform ggrepel so they can test whether the new label grobs affect their package |
It is good that you asked, because I just did, and printing a plot with 100 labels takes 2.5x longer with this PR (695ms vs 268ms). Most of this extra time is spend doing viewport operations. I think I should spend some time trying to make this more efficient.
I will, but I ran their unit tests and they all pass with this PR. |
I don't think |
If I remember correctly from writing gridtext, I felt that titleGrob was overly complex and convoluted. You don't need a second viewport just to add margins around text. You can just make a bigger box and put the text in the right location so there are margins. So there may be efficiency gains from a refactoring. Lines 107 to 156 in f139f40
|
Yeah I did some testing and a lot of the performance loss is by using titleGrob and not necessarily the viewport that I use here to rotate the labels. Perhaps it makes more sense to put in another PR where we refactor |
Performance hit is now only 26% slower (100 labels, 344ms vs 273ms), which is an improvement over the 160% slower we had earlier. I find this an acceptable trade-off for the features it offers, so I'll merge this. |
This PR aims to fix #2785 and fix #4753.
Briefly:
angle
aesthetic.grobDescent(text_grob)
into account.fontsize
parameter, that allows the typicalunit(0.25, "lines")
label padding to get appropriate text sizes.The 2nd and 3rd points lead to visual changes, but I'd argue that these are appropriate.
The reprex provided in #2785 (comment)
The reprex from #4753 (comment)
Created on 2022-11-05 by the reprex package (v2.0.1)