Skip to content
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

Clean up theme addition #3570

Merged
merged 4 commits into from
Oct 23, 2019

Conversation

clauswilke
Copy link
Member

@clauswilke clauswilke commented Oct 14, 2019

Here is an attempt at fixing #3039. Recall: That issue shows that adding theme elements piecewise behaves differently from adding them all at once. The problem is that when adding a theme to a plot object, the current implementation pulls missing info from the default theme, but this is not the case when adding a theme to a theme. In my mind, this behavior is inconsistent. The addition should behave the same in both cases. The solution is to delete the plot-specific update function.

This is the reprex from #3039, which now works just fine:

library(ggplot2)

p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p1 <- p + theme_light() + 
  theme(axis.line.x = element_line(color = "blue")) +
  theme(axis.ticks.x = element_line(color = "red"))

p2 <- p + theme_light() +
  theme(axis.line.x = element_line(color = "blue"),
        axis.ticks.x = element_line(color = "red"))

identical(p1$theme, p2$theme)
#> [1] TRUE

p1

p2

Created on 2019-10-14 by the reprex package (v0.3.0)

However, this is potentially a breaking change (and in fact, some unit tests currently break), because partially updating root elements of a theme doesn't work anymore:
(Update: This has now been fixed, see below.)

library(ggplot2)

# this now breaks but works in current ggplot2 by magically pulling info from default theme
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point() +
  theme(text = element_text(color = "red"))
#> Error in FUN(X[[i]], ...): Theme element 'text' has NULL property: family, face, size, hjust, vjust, angle, lineheight, margin, debug

# this still works
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point() +
  theme(axis.title = element_text(color = "red"))

Created on 2019-10-14 by the reprex package (v0.3.0)

I think the new behavior is correct. If we're messing with a root element, we should have to specify it completely. However, if that's considered to be bad, we could also fix the problem at this point in the code:

ggplot2/R/theme.r

Lines 558 to 565 in 115c396

# If no parents, this is a "root" node. Just return this element.
if (is.null(pnames)) {
# Check that all the properties of this element are non-NULL
nullprops <- vapply(theme[[element]], is.null, logical(1))
if (any(nullprops)) {
stop("Theme element '", element, "' has NULL property: ",
paste(names(nullprops)[nullprops], collapse = ", "))
}

by pulling values from the ggplot2 default theme where available instead of issuing an error.

@clauswilke
Copy link
Member Author

@hadley @thomasp85 Could you provide feedback on this? Should I proceed along these lines? And should I implement the workaround for NULL values in root elements, as explained at the end?

@clauswilke
Copy link
Member Author

After some more thinking, I decided that filling in root elements from the default theme is probably the right way to go. The required changes to make this happen were much more involved than I expected, but I think overall the code is moving in the right direction and getting cleaner. There's now only one theme addition function, and it consists of only a few lines of code. Also, all the unit tests work again (two made incorrect assumptions under the new model and required fixing), including all visual tests without modification. The following reprex works again also.

library(ggplot2)

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point() +
  theme(text = element_text(color = "red"))

Created on 2019-10-14 by the reprex package (v0.3.0)

@clauswilke clauswilke changed the title WIP: Clean up theme addition Clean up theme addition Oct 15, 2019
Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

R/theme.r Outdated Show resolved Hide resolved
@clauswilke clauswilke merged commit 6f5ffea into tidyverse:master Oct 23, 2019
@clauswilke clauswilke deleted the issue-3039-theme-addition branch October 23, 2019 03:57
microly added a commit to microly/ggplot2 that referenced this pull request Oct 24, 2019
@lock
Copy link

lock bot commented Apr 25, 2020

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants