Skip to content

Commit

Permalink
transparent theme (#5018)
Browse files Browse the repository at this point in the history
* theme_transparent

* update unit tests

* document

* new snapshot

* transparent fills -> element_blank()

* move new theme

* adapt theme

* update tests

* move news bullet

* redocument

* cleanup duplicated news entry

---------

Co-authored-by: Teun van den Brand <[email protected]>
  • Loading branch information
topepo and teunbrand authored Dec 2, 2024
1 parent 4193a50 commit f9b9703
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions R/theme-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "",
Expand Down
11 changes: 11 additions & 0 deletions man/ggtheme.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/_snaps/prohibited-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
92 changes: 92 additions & 0 deletions tests/testthat/_snaps/theme/theme-transparent-large.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f9b9703

Please sign in to comment.