Skip to content

Commit 6dd9bb6

Browse files
authored
Absent legend titles take up no space (#5452)
* ignore absent title * Add test * Add news bullet * Fix snapshot
1 parent 8835709 commit 6dd9bb6

File tree

4 files changed

+188
-41
lines changed

4 files changed

+188
-41
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* Legend titles no longer take up space if they've been removed by setting
4+
`legend.title = element_blank()` (@teunbrand, #3587).
5+
36
* New function `check_device()` for testing the availability of advanced
47
graphics features introduced in R 4.1.0 onwards (@teunbrand, #5332).
58

R/guide-legend.R

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -536,28 +536,32 @@ GuideLegend <- ggproto(
536536
)
537537
heights <- head(vec_interleave(!!!heights), -1)
538538

539-
# Measure title
540-
title_width <- width_cm(grobs$title)
541-
title_height <- height_cm(grobs$title)
542-
543-
# Combine title with rest of the sizes based on its position
544-
widths <- switch(
545-
params$title.position,
546-
"left" = c(title_width, hgap, widths),
547-
"right" = c(widths, hgap, title_width),
548-
c(widths, max(0, title_width - sum(widths)))
549-
)
550-
heights <- switch(
551-
params$title.position,
552-
"top" = c(title_height, vgap, heights),
553-
"bottom" = c(heights, vgap, title_height),
554-
c(heights, max(0, title_height - sum(heights)))
555-
)
539+
has_title <- !is.zero(grobs$title)
540+
if (has_title) {
541+
# Measure title
542+
title_width <- width_cm(grobs$title)
543+
title_height <- height_cm(grobs$title)
544+
545+
# Combine title with rest of the sizes based on its position
546+
widths <- switch(
547+
params$title.position,
548+
"left" = c(title_width, hgap, widths),
549+
"right" = c(widths, hgap, title_width),
550+
c(widths, max(0, title_width - sum(widths)))
551+
)
552+
heights <- switch(
553+
params$title.position,
554+
"top" = c(title_height, vgap, heights),
555+
"bottom" = c(heights, vgap, title_height),
556+
c(heights, max(0, title_height - sum(heights)))
557+
)
558+
}
556559

557560
list(
558561
widths = widths,
559562
heights = heights,
560-
padding = elements$padding
563+
padding = elements$padding,
564+
has_title = has_title
561565
)
562566
},
563567

@@ -603,29 +607,34 @@ GuideLegend <- ggproto(
603607
)
604608

605609
# Offset layout based on title position
606-
switch(
607-
params$title.position,
608-
"top" = {
609-
key_row <- key_row + 2
610-
label_row <- label_row + 2
611-
title_row <- 2
612-
title_col <- seq_along(sizes$widths) + 1
613-
},
614-
"bottom" = {
615-
title_row <- length(sizes$heights) + 1
616-
title_col <- seq_along(sizes$widths) + 1
617-
},
618-
"left" = {
619-
key_col <- key_col + 2
620-
label_col <- label_col + 2
621-
title_row <- seq_along(sizes$heights) + 1
622-
title_col <- 2
623-
},
624-
"right" = {
625-
title_row <- seq_along(sizes$heights) + 1
626-
title_col <- length(sizes$widths) + 1
627-
}
628-
)
610+
if (sizes$has_title) {
611+
switch(
612+
params$title.position,
613+
"top" = {
614+
key_row <- key_row + 2
615+
label_row <- label_row + 2
616+
title_row <- 2
617+
title_col <- seq_along(sizes$widths) + 1
618+
},
619+
"bottom" = {
620+
title_row <- length(sizes$heights) + 1
621+
title_col <- seq_along(sizes$widths) + 1
622+
},
623+
"left" = {
624+
key_col <- key_col + 2
625+
label_col <- label_col + 2
626+
title_row <- seq_along(sizes$heights) + 1
627+
title_col <- 2
628+
},
629+
"right" = {
630+
title_row <- seq_along(sizes$heights) + 1
631+
title_col <- length(sizes$widths) + 1
632+
}
633+
)
634+
} else {
635+
title_row <- NA
636+
title_col <- NA
637+
}
629638

630639
df <- cbind(df, key_row, key_col, label_row, label_col)
631640

Lines changed: 119 additions & 0 deletions
Loading

tests/testthat/test-guides.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,22 @@ test_that("Axis titles won't be blown away by coord_*()", {
495495
# expect_doppelganger("guide titles with coord_sf()", plot + coord_sf())
496496
})
497497

498+
test_that("absent titles don't take up space", {
499+
500+
p <- ggplot(mtcars, aes(disp, mpg, colour = factor(cyl))) +
501+
geom_point() +
502+
theme(
503+
legend.title = element_blank(),
504+
legend.margin = margin(),
505+
legend.position = "top",
506+
legend.justification = "left",
507+
legend.key = element_rect(colour = "black"),
508+
axis.line = element_line(colour = "black")
509+
)
510+
511+
expect_doppelganger("left aligned legend key", p)
512+
})
513+
498514
test_that("axis guides can be capped", {
499515
p <- ggplot(mtcars, aes(hp, disp)) +
500516
geom_point() +

0 commit comments

Comments
 (0)