-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Justifying the legend with respect to the full plot area rather than the panel #4020
Comments
Yes, that's a good point. Marking this as TODO for ggplot2 3.4. |
Possibly related, #3989 also proposes more legend alignment options |
Agreed that this would make sense |
Would it be possible to do the same for plot.tag.position? If the tag position is set to "topleft" it's also misaligned with the title. library(ggplot2)
ggplot(data = diamonds, mapping = aes(x = carat, y = price, color = cut)) +
geom_point() +
labs(title = "A plot title",
tag = 'Figure 1') +
theme(plot.title.position = "plot",
plot.tag.position = 'topleft') |
@malcalakovalski Use library(ggplot2)
ggplot(data = diamonds, mapping = aes(x = carat, y = price, color = cut)) +
geom_point() +
labs(
title = "A plot title",
tag = "Figure 1"
) +
theme(
plot.title.position = "plot",
plot.tag.position = c(0, 1),
plot.tag = element_text(hjust = 0, vjust = 1)
)
|
@clauswilke Thank you! But is there a way to do that such that the tag is above the title? |
You could add some top margin to the title with |
That works for my current purposes. Thank you! |
Is there a workaround for it? |
This should not be terribly difficult to implement, as Hadley already left the solution in his comments: Lines 232 to 234 in 0d0de37
However, this brings us to a harder problem: what would the name of this argument be? |
There are a bunch of I also like the |
Does anyone have an idea of how to make this work now until a full solution is implemented? Not knowing much about the ggplot internals, I'm not sure I follow @teunbrand's suggestion above. |
It was not a suggestion. The only thing that has prevented me so far from putting in a PR to fix this is that I don't know what the setting should be named. |
Ah, I see. |
* Add legends in all positions * Assemble separate guide boxes * Add position argument to guides * reoxygenate * adapt tests * deal with old R units * rename manual position to "inside" * resolve spacing once * omit 'inside' option in justification * Move more responsibility to `Guides$draw()` * Propagate "manual" -> "inside" rename * Fallback for inside position * Rearrange methods into logical order * remove vestigial stuff * Separate numeric inside positioning from `legend.position` argument * Implement plot-wise justification (#4020) * Partially revert bd917cf * Add extra justification theme settings * Document `legend.justification.{position}` * Apply justification * Prevent FP warnings by partial matching * Switch to new inside position * Add test for justification per position * Fix subsetting bug * always add gtable rows/cols * adjust table dimension expectations * adapt test * Don't calculate key sizes twice * Use `calc_element()` * Use conventional indexing * prevent partial matching * Move justification responsiblity to `Guides$package_box()` * Fix bug * incorporate guide_custom * incorporate guide_custom
Considering this fixed by #5488. |
@teunbrand I might be missing how to do this in v3.5.0. How would you modify the original example to have it start justified to the plot? library(ggplot2)
ggplot(
data = diamonds |> dplyr::slice_sample(prop = 0.01),
mapping = aes(x = carat, y = price, color = cut)
) +
geom_point() +
labs(title = "A plot title") +
theme(
plot.title.position = "plot",
legend.position = "top",
legend.justification = c(0, 1)
) |
Should be possible by adding |
Now that the plot titles and captions can be aligned with respect to the full plot area, it would be great if the same was true for the legend. At the moment, if the legend is positioned at the top of the plot below the title it is aligned to the plot panel, making it misaligned with the title:
Created on 2020-05-22 by the reprex package (v0.3.0)
Experimenting with negative values in
legend.justification = c(x,y)
can get the legend to align with the title, but the value is dependent on the size of the y-axis labelsThe text was updated successfully, but these errors were encountered: