Skip to content

Fix calculation of theme defaults #2080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@

* `position_jitter()` gains a `seed` argument that allows specifying a random seed for reproducible jittering (#1996, @krlmlr).

* Fixed bug where a new complete `theme` may fail to override all elements of the default `theme`.
(@has2k1, #2058, #2079)

### sf

Expand Down
4 changes: 3 additions & 1 deletion R/labeller.r
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@ build_strip <- function(label_df, labeller, theme, horizontal) {
grobs_right <- apply(grobs_right, 1, function(strips) {
gtable_matrix("strip", matrix(strips, nrow = 1), widths, unit(1, "null"), clip = "on")
})
theme$strip.text.y$angle <- adjust_angle(theme$strip.text.y$angle)
if (inherits(theme$strip.text.y, "element_text")) {
theme$strip.text.y$angle <- adjust_angle(theme$strip.text.y$angle)
}
grobs_left <- apply(labels, c(1, 2), ggstrip, theme = theme,
horizontal = horizontal)
widths <- unit(apply(grobs_left, 2, max_width), "cm")
Expand Down
20 changes: 20 additions & 0 deletions R/theme-defaults.r
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ theme_classic <- function(base_size = 11, base_family = "",
theme_void <- function(base_size = 11, base_family = "",
base_line_size = base_size / 22,
base_rect_size = base_size / 22) {
half_line <- base_size / 2
theme(
# Use only inherited elements and make almost everything blank
# Only keep indispensable text
Expand All @@ -388,10 +389,29 @@ theme_void <- function(base_size = 11, base_family = "",
),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks.length = unit(0, "pt"),
legend.key.size = unit(1.2, "lines"),
legend.position = "right",
legend.text = element_text(size = rel(0.8)),
legend.title = element_text(hjust = 0),
strip.text = element_text(size = rel(0.8)),
strip.switch.pad.grid = unit(0.1, "cm"),
strip.switch.pad.wrap = unit(0.1, "cm"),
panel.ontop = FALSE,
panel.spacing = unit(half_line, "pt"),
plot.margin = unit(c(0, 0, 0, 0), "lines"),
plot.title = element_text(
size = rel(1.2),
hjust = 0, vjust = 1,
margin = margin(t = half_line * 1.2)),
plot.subtitle = element_text(
size = rel(0.9),
hjust = 0, vjust = 1,
margin = margin(t = half_line * 0.9)),
plot.caption = element_text(
size = rel(0.9),
hjust = 1, vjust = 1,
margin = margin(t = half_line * 0.9)),

complete = TRUE
)
Expand Down
11 changes: 9 additions & 2 deletions R/theme.r
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,15 @@ theme <- function(line,


# Combine plot defaults with current theme to get complete theme for a plot
plot_theme <- function(x) {
defaults(x$theme, theme_get())
plot_theme <- function(x, default_theme=theme_get()) {
complete <- attr(x$theme, "complete")
if (is.null(complete)) {
default_theme
} else if (complete) {
x$theme
} else {
defaults(x$theme, default_theme)
}
}

#' Modify properties of an element in a theme object
Expand Down
34 changes: 17 additions & 17 deletions tests/figs/themes/theme-void-large.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 17 additions & 17 deletions tests/figs/themes/theme-void.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tests/testthat/test-theme.r
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ test_that("Complete and non-complete themes interact correctly with ggplot objec
expect_false(attr(p$plot$theme, "complete"))
expect_equal(p$plot$theme$text$colour, "red")
expect_equal(p$plot$theme$text$face, "italic")

})

test_that("theme(validate=FALSE) means do not validate_element", {
Expand Down Expand Up @@ -224,6 +225,19 @@ test_that("Elements can be merged", {
)
})

test_that("Final calculated plot theme should not blend a complete theme with the default theme", {
default_theme <- theme_gray() + theme(axis.text.x = element_text(colour = "red"))

ptheme <- plot_theme(qplot(1:3, 1:3) + theme_void(), default_theme)
expect_null(ptheme$axis.text.x)

ptheme <- plot_theme(qplot(1:3, 1:3) + theme_gray(), default_theme)
expect_true(is.null(ptheme$axis.text.x$colour) || ptheme$axis.text.x$colour != "red")

ptheme <- plot_theme(qplot(1:3, 1:3) + theme(axis.text.y = element_text(colour = "blue")), default_theme)
expect_equal(ptheme$axis.text.x$colour, "red")
expect_equal(ptheme$axis.text.y$colour, "blue")
})

# Visual tests ------------------------------------------------------------

Expand Down