diff --git a/NEWS.md b/NEWS.md
index dcd22bdd8d..c1ad8337ad 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,5 +1,7 @@
# ggplot2 (development version)
+* Fixed bug in `guide_custom()` that would throw error with `theme_void()`
+ (@teunbrand, #5856).
* New helper function `ggpar()` to translate ggplot2's interpretation of
graphical parameters to {grid}'s interpretation (@teunbrand, #5866).
@@ -23,7 +25,7 @@ documentation updates.
(@teunbrand, #5757).
## Improvements
-
+
* When facets coerce the faceting variables to factors, the 'ordered' class
is dropped (@teunbrand, #5666).
* `coord_map()` and `coord_polar()` throw informative warnings when used
diff --git a/R/guide-custom.R b/R/guide-custom.R
index 16a737d3fd..990712b36b 100644
--- a/R/guide-custom.R
+++ b/R/guide-custom.R
@@ -117,7 +117,7 @@ GuideCustom <- ggproto(
)
# Add padding and background
- gt <- gtable_add_padding(gt, elems$margin)
+ gt <- gtable_add_padding(gt, elems$margin %||% margin())
gt <- gtable_add_grob(
gt, element_grob(elems$background),
diff --git a/tests/testthat/_snaps/guides/guide-custom-with-void-theme.svg b/tests/testthat/_snaps/guides/guide-custom-with-void-theme.svg
new file mode 100644
index 0000000000..ac52f62280
--- /dev/null
+++ b/tests/testthat/_snaps/guides/guide-custom-with-void-theme.svg
@@ -0,0 +1,34 @@
+
+
diff --git a/tests/testthat/_snaps/guides/stylised-guide-custom.svg b/tests/testthat/_snaps/guides/stylised-guide-custom.svg
new file mode 100644
index 0000000000..85d3aa01f9
--- /dev/null
+++ b/tests/testthat/_snaps/guides/stylised-guide-custom.svg
@@ -0,0 +1,37 @@
+
+
diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R
index bc359938ae..4930188025 100644
--- a/tests/testthat/test-guides.R
+++ b/tests/testthat/test-guides.R
@@ -1273,3 +1273,23 @@ test_that("old S3 guides can be implemented", {
)
)
})
+
+test_that("guide_custom can be drawn and styled", {
+
+ p <- ggplot() + guides(custom = guide_custom(
+ circleGrob(r = unit(1, "cm")),
+ title = "custom guide"
+ ))
+
+ expect_doppelganger(
+ "stylised guide_custom",
+ p + theme(legend.background = element_rect(fill = "grey50"),
+ legend.title.position = "left",
+ legend.title = element_text(angle = 90, hjust = 0.5))
+ )
+
+ expect_doppelganger(
+ "guide_custom with void theme",
+ p + theme_void()
+ )
+})