diff --git a/NAMESPACE b/NAMESPACE
index 67649360da..becf512278 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -721,6 +721,7 @@ export(theme_minimal)
export(theme_replace)
export(theme_set)
export(theme_test)
+export(theme_transparent)
export(theme_update)
export(theme_void)
export(transform_position)
diff --git a/NEWS.md b/NEWS.md
index 65e8c9fc48..b40ea07f25 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -212,6 +212,7 @@
* `stat_summary_bin()` no longer ignores `width` parameter (@teunbrand, #4647).
* Added `keep.zeroes` argument to `stat_bin()` (@teunbrand, #3449)
* `coord_sf()` no longer errors when dealing with empty graticules (@teunbrand, #6052)
+* Added `theme_transparent()` with transparent backgrounds (@topepo).
# ggplot2 3.5.1
diff --git a/R/theme-defaults.R b/R/theme-defaults.R
index 8be7999d4d..88aa05146b 100644
--- a/R/theme-defaults.R
+++ b/R/theme-defaults.R
@@ -497,6 +497,41 @@ theme_classic <- function(base_size = 11, base_family = "",
)
}
+#' @export
+#' @rdname ggtheme
+theme_transparent <- function(base_size = 11, base_family = "",
+ header_family = NULL,
+ base_line_size = base_size / 22,
+ base_rect_size = base_size / 22,
+ ink = "black", paper = alpha(ink, 0)) {
+ force(ink)
+ # Based on theme_bw
+ theme_grey(
+ base_size = base_size,
+ base_family = base_family,
+ header_family = header_family,
+ base_line_size = base_line_size,
+ base_rect_size = base_rect_size,
+ ink = ink, paper = paper
+ ) %+replace%
+ theme(
+ panel.background = element_blank(),
+ plot.background = element_blank(),
+ legend.background = element_blank(),
+ legend.key = element_blank(),
+ # theme_bw specifications
+ panel.border = element_rect(fill = NA, colour = col_mix(ink, paper, 0.20)),
+ # make gridlines dark, same contrast with white as in theme_grey
+ panel.grid = element_line(colour = col_mix(ink, paper, 0.92)),
+ # contour strips to match panel contour
+ strip.background = element_rect(
+ fill = col_mix(ink, paper, 0.85),
+ colour = col_mix(ink, paper, 0.20),
+ ),
+ complete = TRUE
+ )
+}
+
#' @export
#' @rdname ggtheme
theme_void <- function(base_size = 11, base_family = "",
diff --git a/man/ggtheme.Rd b/man/ggtheme.Rd
index e86accb182..57dd519355 100644
--- a/man/ggtheme.Rd
+++ b/man/ggtheme.Rd
@@ -9,6 +9,7 @@
\alias{theme_dark}
\alias{theme_minimal}
\alias{theme_classic}
+\alias{theme_transparent}
\alias{theme_void}
\alias{theme_test}
\title{Complete themes}
@@ -93,6 +94,16 @@ theme_classic(
paper = "white"
)
+theme_transparent(
+ base_size = 11,
+ base_family = "",
+ header_family = NULL,
+ base_line_size = base_size/22,
+ base_rect_size = base_size/22,
+ ink = "black",
+ paper = alpha(ink, 0)
+)
+
theme_void(
base_size = 11,
base_family = "",
diff --git a/tests/testthat/_snaps/prohibited-functions.md b/tests/testthat/_snaps/prohibited-functions.md
index afc77c3c60..007e6521c4 100644
--- a/tests/testthat/_snaps/prohibited-functions.md
+++ b/tests/testthat/_snaps/prohibited-functions.md
@@ -219,6 +219,10 @@
[1] "base_size" "base_family" "header_family" "base_line_size"
[5] "base_rect_size"
+ $theme_transparent
+ [1] "base_size" "base_family" "header_family" "base_line_size"
+ [5] "base_rect_size"
+
$theme_void
[1] "base_size" "base_family" "header_family" "base_line_size"
[5] "base_rect_size"
diff --git a/tests/testthat/_snaps/theme/theme-transparent-large.svg b/tests/testthat/_snaps/theme/theme-transparent-large.svg
new file mode 100644
index 0000000000..ebf6962840
--- /dev/null
+++ b/tests/testthat/_snaps/theme/theme-transparent-large.svg
@@ -0,0 +1,92 @@
+
+
diff --git a/tests/testthat/_snaps/theme/theme-transparent.svg b/tests/testthat/_snaps/theme/theme-transparent.svg
new file mode 100644
index 0000000000..067326449a
--- /dev/null
+++ b/tests/testthat/_snaps/theme/theme-transparent.svg
@@ -0,0 +1,92 @@
+
+
diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R
index cf98a1bb3f..2f7f7874f9 100644
--- a/tests/testthat/test-theme.R
+++ b/tests/testthat/test-theme.R
@@ -350,6 +350,7 @@ test_that("all elements in complete themes have inherit.blank=TRUE", {
expect_true(inherit_blanks(theme_linedraw()))
expect_true(inherit_blanks(theme_minimal()))
expect_true(inherit_blanks(theme_void()))
+ expect_true(inherit_blanks(theme_transparent()))
})
test_that("elements can be merged", {
@@ -496,6 +497,9 @@ test_that("provided themes explicitly define all elements", {
t <- theme_test()
expect_true(all(names(t) %in% elements))
+
+ t <- theme_transparent()
+ expect_true(all(names(t) %in% elements))
})
test_that("Theme elements are checked during build", {
@@ -661,6 +665,7 @@ test_that("themes don't change without acknowledgement", {
expect_doppelganger("theme_light", plot + theme_light())
expect_doppelganger("theme_void", plot + theme_void())
expect_doppelganger("theme_linedraw", plot + theme_linedraw())
+ expect_doppelganger("theme_transparent", plot + theme_transparent())
})
test_that("themes look decent at larger base sizes", {
@@ -677,6 +682,7 @@ test_that("themes look decent at larger base sizes", {
expect_doppelganger("theme_light_large", plot + theme_light(base_size = 33))
expect_doppelganger("theme_void_large", plot + theme_void(base_size = 33))
expect_doppelganger("theme_linedraw_large", plot + theme_linedraw(base_size = 33))
+ expect_doppelganger("theme_transparent_large", plot + theme_transparent(base_size = 33))
})
test_that("setting 'spacing' and 'margins' affect the whole plot", {