Skip to content

Commit f0ce285

Browse files
authored
Explicitly define all theme elements (#3585)
* explicitly define all theme elements. closes #3584. * generate and cache all null theme on load
1 parent 86cc4d2 commit f0ce285

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

R/theme-defaults.r

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ theme_grey <- function(base_size = 11, base_family = "",
111111
# Throughout the theme, we use three font sizes, `base_size` (`rel(1)`)
112112
# for normal, `rel(0.8)` for small, and `rel(1.2)` for large.
113113

114-
theme(
114+
t <- theme(
115115
# Elements in this first block aren't used directly, but are inherited
116116
# by others
117117
line = element_line(
@@ -234,6 +234,9 @@ theme_grey <- function(base_size = 11, base_family = "",
234234

235235
complete = TRUE
236236
)
237+
238+
# make sure all elements are set to NULL if not explicitly defined
239+
ggplot_global$theme_all_null %+replace% t
237240
}
238241
#' @export
239242
#' @rdname ggtheme
@@ -455,7 +458,7 @@ theme_void <- function(base_size = 11, base_family = "",
455458
half_line <- base_size / 2
456459

457460
# Only keep indispensable text: legend and plot titles
458-
theme(
461+
t <- theme(
459462
line = element_blank(),
460463
rect = element_blank(),
461464
text = element_text(
@@ -508,6 +511,9 @@ theme_void <- function(base_size = 11, base_family = "",
508511

509512
complete = TRUE
510513
)
514+
515+
# make sure all elements are set to NULL if not explicitly defined
516+
ggplot_global$theme_all_null %+replace% t
511517
}
512518

513519

@@ -518,7 +524,7 @@ theme_test <- function(base_size = 11, base_family = "",
518524
base_rect_size = base_size / 22) {
519525
half_line <- base_size / 2
520526

521-
theme(
527+
t <- theme(
522528
line = element_line(
523529
colour = "black", size = base_line_size,
524530
linetype = 1, lineend = "butt"
@@ -639,4 +645,19 @@ theme_test <- function(base_size = 11, base_family = "",
639645

640646
complete = TRUE
641647
)
648+
649+
# make sure all elements are set to NULL if not explicitly defined
650+
ggplot_global$theme_all_null %+replace% t
651+
}
652+
653+
theme_all_null <- function() {
654+
# set all elements in the element tree to NULL
655+
elements <- sapply(
656+
names(ggplot_global$element_tree),
657+
function(x) NULL,
658+
simplify = FALSE, USE.NAMES = TRUE
659+
)
660+
661+
args <- c(elements, list(complete = TRUE))
662+
do.call(theme, args)
642663
}

R/zzz.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pathGrob <- NULL
3434
.zeroGrob <<- grob(cl = "zeroGrob", name = "NULL")
3535

3636
# create default theme, store for later use, and set as current theme
37+
ggplot_global$theme_all_null <- theme_all_null() # required by theme_grey()
3738
ggplot_global$theme_grey <- theme_grey()
3839
ggplot_global$theme_current <- ggplot_global$theme_grey
3940

tests/testthat/test-theme.r

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,38 @@ test_that("plot titles and caption can be aligned to entire plot", {
484484
expect_doppelganger("caption aligned to entire plot", plot)
485485

486486
})
487+
488+
test_that("provided themes explicitly define all elements", {
489+
elements <- names(ggplot_global$element_tree)
490+
491+
t <- theme_all_null()
492+
expect_true(all(names(t) %in% elements))
493+
expect_true(all(vapply(t, is.null, logical(1))))
494+
495+
t <- theme_grey()
496+
expect_true(all(names(t) %in% elements))
497+
498+
t <- theme_bw()
499+
expect_true(all(names(t) %in% elements))
500+
501+
t <- theme_linedraw()
502+
expect_true(all(names(t) %in% elements))
503+
504+
t <- theme_light()
505+
expect_true(all(names(t) %in% elements))
506+
507+
t <- theme_dark()
508+
expect_true(all(names(t) %in% elements))
509+
510+
t <- theme_minimal()
511+
expect_true(all(names(t) %in% elements))
512+
513+
t <- theme_classic()
514+
expect_true(all(names(t) %in% elements))
515+
516+
t <- theme_void()
517+
expect_true(all(names(t) %in% elements))
518+
519+
t <- theme_test()
520+
expect_true(all(names(t) %in% elements))
521+
})

0 commit comments

Comments
 (0)