Skip to content

Commit 4eb6587

Browse files
authored
Fix missing margin error in guide_custom() (#5858)
* fallback to empty margin * add tests * add news bullet
1 parent 6c2e102 commit 4eb6587

File tree

5 files changed

+95
-2
lines changed

5 files changed

+95
-2
lines changed

NEWS.md

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

3+
* Fixed bug in `guide_custom()` that would throw error with `theme_void()`
4+
(@teunbrand, #5856).
35
* New helper function `ggpar()` to translate ggplot2's interpretation of
46
graphical parameters to {grid}'s interpretation (@teunbrand, #5866).
57

@@ -23,7 +25,7 @@ documentation updates.
2325
(@teunbrand, #5757).
2426

2527
## Improvements
26-
28+
2729
* When facets coerce the faceting variables to factors, the 'ordered' class
2830
is dropped (@teunbrand, #5666).
2931
* `coord_map()` and `coord_polar()` throw informative warnings when used

R/guide-custom.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ GuideCustom <- ggproto(
117117
)
118118

119119
# Add padding and background
120-
gt <- gtable_add_padding(gt, elems$margin)
120+
gt <- gtable_add_padding(gt, elems$margin %||% margin())
121121

122122
gt <- gtable_add_grob(
123123
gt, element_grob(elems$background),
Lines changed: 34 additions & 0 deletions
Loading
Lines changed: 37 additions & 0 deletions
Loading

tests/testthat/test-guides.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,3 +1273,23 @@ test_that("old S3 guides can be implemented", {
12731273
)
12741274
)
12751275
})
1276+
1277+
test_that("guide_custom can be drawn and styled", {
1278+
1279+
p <- ggplot() + guides(custom = guide_custom(
1280+
circleGrob(r = unit(1, "cm")),
1281+
title = "custom guide"
1282+
))
1283+
1284+
expect_doppelganger(
1285+
"stylised guide_custom",
1286+
p + theme(legend.background = element_rect(fill = "grey50"),
1287+
legend.title.position = "left",
1288+
legend.title = element_text(angle = 90, hjust = 0.5))
1289+
)
1290+
1291+
expect_doppelganger(
1292+
"guide_custom with void theme",
1293+
p + theme_void()
1294+
)
1295+
})

0 commit comments

Comments
 (0)