From 2a6aa1093d855a380efd27ae6209c7ce72229e74 Mon Sep 17 00:00:00 2001 From: Max Kuhn Date: Fri, 21 Oct 2022 10:24:17 -0400 Subject: [PATCH 01/11] theme_transparent --- NEWS.md | 2 ++ R/theme-defaults.r | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/NEWS.md b/NEWS.md index 1055701665..4254c3571b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -155,6 +155,8 @@ precedence between `bins` and `binwidth`. (@eliocamp, #4651) * Updated documentation for `geom_smooth()` to more clearly describe effects of the `fullrange` parameter (@thoolihan, #4399). + +* Added `theme_transparent()` with transparent backgrounds. # ggplot2 3.3.6 This is a very small release only applying an internal change to comply with diff --git a/R/theme-defaults.r b/R/theme-defaults.r index 50d3d5bf0e..8f7a5dd8de 100644 --- a/R/theme-defaults.r +++ b/R/theme-defaults.r @@ -671,3 +671,34 @@ theme_all_null <- function() { args <- c(elements, list(complete = TRUE)) inject(theme(!!!args)) } + +#' @export +#' @rdname ggtheme +theme_transparent <- function(base_size = 11, base_family = "", + base_line_size = base_size / 22, + base_rect_size = base_size / 22) { + # Based on theme_bw + theme_grey( + base_size = base_size, + base_family = base_family, + base_line_size = base_line_size, + base_rect_size = base_rect_size + ) %+replace% + theme( + # white background and dark border + panel.background = element_rect(fill = "transparent", colour = NA), + plot.background = element_rect(fill = "transparent", colour = NA), + legend.background = element_rect(fill = "transparent", colour = NA), + legend.key = element_rect(fill = "transparent", colour = NA), + panel.border = element_rect(fill = NA, colour = "grey20"), + # make gridlines dark, same contrast with white as in theme_grey + panel.grid = element_line(colour = "grey92"), + panel.grid.minor = element_line(linewidth = rel(0.5)), + # contour strips to match panel contour + strip.background = element_rect(fill = "grey85", colour = "grey20"), + + complete = TRUE + ) +} + + From 9c2cd43eda2795599d9d36fa030e01066656fef7 Mon Sep 17 00:00:00 2001 From: Max Kuhn Date: Fri, 21 Oct 2022 10:27:27 -0400 Subject: [PATCH 02/11] update unit tests --- tests/testthat/test-theme.r | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/testthat/test-theme.r b/tests/testthat/test-theme.r index 3d64894b15..95b88847e1 100644 --- a/tests/testthat/test-theme.r +++ b/tests/testthat/test-theme.r @@ -328,6 +328,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", { @@ -477,6 +478,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", { @@ -538,6 +542,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", { @@ -554,6 +559,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", plot + theme_transparent(base_size = 33)) }) test_that("axes can be styled independently", { From 8882bee2dd481fd4556048d6aede3404f40489f7 Mon Sep 17 00:00:00 2001 From: Max Kuhn Date: Fri, 21 Oct 2022 10:31:46 -0400 Subject: [PATCH 03/11] document --- NAMESPACE | 1 + man/ggtheme.Rd | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 9cfee5593e..e18bdcc79b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -683,6 +683,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/man/ggtheme.Rd b/man/ggtheme.Rd index 2f53a4f33c..15d5606fa3 100644 --- a/man/ggtheme.Rd +++ b/man/ggtheme.Rd @@ -11,6 +11,7 @@ \alias{theme_classic} \alias{theme_void} \alias{theme_test} +\alias{theme_transparent} \title{Complete themes} \usage{ theme_grey( @@ -82,6 +83,13 @@ theme_test( base_line_size = base_size/22, base_rect_size = base_size/22 ) + +theme_transparent( + base_size = 11, + base_family = "", + base_line_size = base_size/22, + base_rect_size = base_size/22 +) } \arguments{ \item{base_size}{base font size, given in pts.} From a099878ace6c7b32031be906a61cfb0244eb0bfd Mon Sep 17 00:00:00 2001 From: Max Kuhn Date: Fri, 21 Oct 2022 12:39:16 -0400 Subject: [PATCH 04/11] new snapshot --- .../_snaps/theme/theme-transparent.svg | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tests/testthat/_snaps/theme/theme-transparent.svg diff --git a/tests/testthat/_snaps/theme/theme-transparent.svg b/tests/testthat/_snaps/theme/theme-transparent.svg new file mode 100644 index 0000000000..1606f306ad --- /dev/null +++ b/tests/testthat/_snaps/theme/theme-transparent.svg @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1 + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +x +y + +z + + + + +a +b +theme_transparent + + From 3acb3cd3335feb86f1c8a02bf5f0db65ba20f58c Mon Sep 17 00:00:00 2001 From: Max Kuhn Date: Wed, 26 Oct 2022 09:06:14 -0400 Subject: [PATCH 05/11] transparent fills -> element_blank() --- R/theme-defaults.r | 10 +++++----- tests/testthat/_snaps/theme/theme-transparent.svg | 5 ----- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/R/theme-defaults.r b/R/theme-defaults.r index 8f7a5dd8de..f5c8f3f2f1 100644 --- a/R/theme-defaults.r +++ b/R/theme-defaults.r @@ -685,11 +685,11 @@ theme_transparent <- function(base_size = 11, base_family = "", base_rect_size = base_rect_size ) %+replace% theme( - # white background and dark border - panel.background = element_rect(fill = "transparent", colour = NA), - plot.background = element_rect(fill = "transparent", colour = NA), - legend.background = element_rect(fill = "transparent", colour = NA), - legend.key = element_rect(fill = "transparent", colour = NA), + 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 = "grey20"), # make gridlines dark, same contrast with white as in theme_grey panel.grid = element_line(colour = "grey92"), diff --git a/tests/testthat/_snaps/theme/theme-transparent.svg b/tests/testthat/_snaps/theme/theme-transparent.svg index 1606f306ad..a361cdf93d 100644 --- a/tests/testthat/_snaps/theme/theme-transparent.svg +++ b/tests/testthat/_snaps/theme/theme-transparent.svg @@ -18,7 +18,6 @@ - @@ -26,7 +25,6 @@ - @@ -84,11 +82,8 @@ x y - z - - a b From 0e065ce5b0aa81876073617b723b22f27e1d6903 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Nov 2024 14:32:22 +0100 Subject: [PATCH 06/11] move new theme --- R/theme-defaults.R | 60 ++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/R/theme-defaults.R b/R/theme-defaults.R index 496cfce2a7..801079ba59 100644 --- a/R/theme-defaults.R +++ b/R/theme-defaults.R @@ -497,6 +497,35 @@ theme_classic <- function(base_size = 11, base_family = "", ) } +#' @export +#' @rdname ggtheme +theme_transparent <- function(base_size = 11, base_family = "", + base_line_size = base_size / 22, + base_rect_size = base_size / 22) { + # Based on theme_bw + theme_grey( + base_size = base_size, + base_family = base_family, + base_line_size = base_line_size, + base_rect_size = base_rect_size + ) %+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 = "grey20"), + # make gridlines dark, same contrast with white as in theme_grey + panel.grid = element_line(colour = "grey92"), + panel.grid.minor = element_line(linewidth = rel(0.5)), + # contour strips to match panel contour + strip.background = element_rect(fill = "grey85", colour = "grey20"), + + complete = TRUE + ) +} + #' @export #' @rdname ggtheme theme_void <- function(base_size = 11, base_family = "", @@ -744,34 +773,3 @@ theme_all_null <- function() { args <- c(elements, list(complete = TRUE)) inject(theme(!!!args)) } - -#' @export -#' @rdname ggtheme -theme_transparent <- function(base_size = 11, base_family = "", - base_line_size = base_size / 22, - base_rect_size = base_size / 22) { - # Based on theme_bw - theme_grey( - base_size = base_size, - base_family = base_family, - base_line_size = base_line_size, - base_rect_size = base_rect_size - ) %+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 = "grey20"), - # make gridlines dark, same contrast with white as in theme_grey - panel.grid = element_line(colour = "grey92"), - panel.grid.minor = element_line(linewidth = rel(0.5)), - # contour strips to match panel contour - strip.background = element_rect(fill = "grey85", colour = "grey20"), - - complete = TRUE - ) -} - - From 59566175ef1e390399c8a2deeb71d59c722491fb Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Nov 2024 14:52:43 +0100 Subject: [PATCH 07/11] adapt theme --- R/theme-defaults.R | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/R/theme-defaults.R b/R/theme-defaults.R index 801079ba59..88aa05146b 100644 --- a/R/theme-defaults.R +++ b/R/theme-defaults.R @@ -500,14 +500,19 @@ 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) { + 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 + base_rect_size = base_rect_size, + ink = ink, paper = paper ) %+replace% theme( panel.background = element_blank(), @@ -515,13 +520,14 @@ theme_transparent <- function(base_size = 11, base_family = "", legend.background = element_blank(), legend.key = element_blank(), # theme_bw specifications - panel.border = element_rect(fill = NA, colour = "grey20"), + 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 = "grey92"), - panel.grid.minor = element_line(linewidth = rel(0.5)), + panel.grid = element_line(colour = col_mix(ink, paper, 0.92)), # contour strips to match panel contour - strip.background = element_rect(fill = "grey85", colour = "grey20"), - + strip.background = element_rect( + fill = col_mix(ink, paper, 0.85), + colour = col_mix(ink, paper, 0.20), + ), complete = TRUE ) } From 5724ed479b5ebd5eca0df838b4652a293275ffe9 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Nov 2024 15:12:12 +0100 Subject: [PATCH 08/11] update tests --- tests/testthat/_snaps/prohibited-functions.md | 4 + .../_snaps/theme/theme-transparent-large.svg | 92 ++++++++++++++ .../_snaps/theme/theme-transparent.svg | 116 +++++++++--------- tests/testthat/test-theme.R | 2 +- 4 files changed, 155 insertions(+), 59 deletions(-) create mode 100644 tests/testthat/_snaps/theme/theme-transparent-large.svg 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1 + + + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +x +y +z + + +a +b +theme_transparent_large + + diff --git a/tests/testthat/_snaps/theme/theme-transparent.svg b/tests/testthat/_snaps/theme/theme-transparent.svg index a361cdf93d..067326449a 100644 --- a/tests/testthat/_snaps/theme/theme-transparent.svg +++ b/tests/testthat/_snaps/theme/theme-transparent.svg @@ -20,73 +20,73 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - - + + - - -1 + + +1 - - - - - -1.0 -1.5 -2.0 -2.5 -3.0 -1.0 -1.5 -2.0 -2.5 -3.0 - - - - - -x -y -z - - -a -b -theme_transparent + + + + + +1.0 +1.5 +2.0 +2.5 +3.0 +1.0 +1.5 +2.0 +2.5 +3.0 + + + + + +x +y +z + + +a +b +theme_transparent diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R index d4a40062c8..2f7f7874f9 100644 --- a/tests/testthat/test-theme.R +++ b/tests/testthat/test-theme.R @@ -682,7 +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", plot + theme_transparent(base_size = 33)) + expect_doppelganger("theme_transparent_large", plot + theme_transparent(base_size = 33)) }) test_that("setting 'spacing' and 'margins' affect the whole plot", { From 1a540179ba5ff1497c6dbcc3ae3da7846c8769ec Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Nov 2024 15:13:21 +0100 Subject: [PATCH 09/11] move news bullet --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 4cce1069d9..883b416850 100644 --- a/NEWS.md +++ b/NEWS.md @@ -202,6 +202,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. # ggplot2 3.5.1 @@ -925,7 +926,6 @@ gains in rendering speed. * Updated documentation for `geom_smooth()` to more clearly describe effects of the `fullrange` parameter (@thoolihan, #4399). -* Added `theme_transparent()` with transparent backgrounds. * Updated documentation for `geom_smooth()` to more clearly describe effects of the `fullrange` parameter (@thoolihan, #4399). From fbe4a5ea4437fa937ec921113d9c4666d424ca34 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Nov 2024 15:23:22 +0100 Subject: [PATCH 10/11] redocument --- man/ggtheme.Rd | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/man/ggtheme.Rd b/man/ggtheme.Rd index b05394a93d..57dd519355 100644 --- a/man/ggtheme.Rd +++ b/man/ggtheme.Rd @@ -9,9 +9,9 @@ \alias{theme_dark} \alias{theme_minimal} \alias{theme_classic} +\alias{theme_transparent} \alias{theme_void} \alias{theme_test} -\alias{theme_transparent} \title{Complete themes} \usage{ theme_grey( @@ -94,17 +94,17 @@ theme_classic( paper = "white" ) -theme_void( +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 = "white" + paper = alpha(ink, 0) ) -theme_test( +theme_void( base_size = 11, base_family = "", header_family = NULL, @@ -114,11 +114,14 @@ theme_test( paper = "white" ) -theme_transparent( +theme_test( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, - base_rect_size = base_size/22 + base_rect_size = base_size/22, + ink = "black", + paper = "white" ) } \arguments{ From 66a43395488ba8ab768ae1a7062c0504b4b595fa Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 2 Dec 2024 10:07:45 +0100 Subject: [PATCH 11/11] cleanup duplicated news entry --- NEWS.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 4d9a39283d..b40ea07f25 100644 --- a/NEWS.md +++ b/NEWS.md @@ -212,7 +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. +* Added `theme_transparent()` with transparent backgrounds (@topepo). # ggplot2 3.5.1 @@ -933,9 +933,6 @@ gains in rendering speed. * Key glyphs for `geom_boxplot()`, `geom_crossbar()`, `geom_pointrange()`, and `geom_linerange()` are now orientation-aware (@mjskay, #4732) -* Updated documentation for `geom_smooth()` to more clearly describe effects of the - `fullrange` parameter (@thoolihan, #4399). - * Updated documentation for `geom_smooth()` to more clearly describe effects of the `fullrange` parameter (@thoolihan, #4399).