Skip to content

Conversation

@teunbrand
Copy link
Contributor

This PR aims to fix tidyverse/ggplot2#6317.

Briefly, the default_aes fields have changed to more often include expressions that are to be evaluated instead of fixed values. This causes a problem for thematic in extracting these defaults, as thematic expects the fixed values.
This PR uses ggplot2's new getter to resolve this information, which makes thematic compatible again with the impending ggplot2 version.

In addition, I've seen some visual changes that appear to be due to a miscalculation of the axis.ticks.length family of theme settings. I haven't familiarised myself too much with how thematic derives these, but it might be worth reviewing at some point.

As an aside, ggplot2 has revamped how defaults are determined, which are now mostly determined via the theme. Partially this overlaps with thematic goals, so I think there might be an opportunity to leverage ggplot2's new theme settings to reduce the effort thematic has to do.

We plan to release the new ggplot2 version in May 2025, so I'd be great if we didn't break thematic at that point.

@gadenbuie
Copy link
Member

As an aside, ggplot2 has revamped how defaults are determined, which are now mostly determined via the theme. Partially this overlaps with thematic goals, so I think there might be an opportunity to leverage ggplot2's new theme settings to reduce the effort thematic has to do.

Thanks @teunbrand! Are there any materials that would serve as a good place to get up to speed on these recent changes?

@teunbrand
Copy link
Contributor Author

teunbrand commented Jan 31, 2025

There isn't any material about this out yet, but we plan to make a blog post about it.
For now, this is a little bit of a TL;DR:

As mentioned, default aesthetics are now often expressions with the from_theme() keyword that directs the defaults to be filled in from the theme.

library(ggplot2)

GeomPoint$default_aes
#> Aesthetic mapping: 
#> * `shape`  -> `from_theme(pointshape)`
#> * `colour` -> `from_theme(ink)`
#> * `size`   -> `from_theme(pointsize)`
#> * `fill`   -> NA
#> * `alpha`  -> NA
#> * `stroke` -> `from_theme(borderwidth)`

Mirroring these layer defaults is a geom theme element that we can populate as follows. If we look at the defaults above, we would expect the ink setting to affect the point colour, but no default in the point geom looks up from the paper setting.

plot <- ggplot(mpg, aes(displ, hwy)) +
  geom_point()
plot + theme(geom = element_geom(ink = "limegreen", paper = "grey30"))

The ink/paper distinction is more important for setting full themes, which populates the elements including the geom element.

plot + theme_gray(ink = "limegreen", paper = "grey30")

In addition, we can set default palettes from the theme like so:

plot + aes(colour = cty) +
  theme(palette.colour.continuous = c("limegreen", "grey30"))

Created on 2025-01-31 with reprex v2.1.1

You can find all palettes that ggplot2 populates here: https://github.com/tidyverse/ggplot2/blob/1bfb3c9b26a046a878d97a15cccf108973a8150a/R/theme-elements.R#L786-L799

@teunbrand teunbrand changed the title Compatibility with ggplot2 3.6.0 Compatibility with ggplot2 4.0.0 Jun 12, 2025
@teunbrand
Copy link
Contributor Author

This PR should now also make thematic compatible with the S7 changes in ggplot2. I recommend reviewing the visual tests too, as the following mentioned above still holds:

In addition, I've seen some visual changes that appear to be due to a miscalculation of the axis.ticks.length family of theme settings. I haven't familiarised myself too much with how thematic derives these, but it might be worth reviewing at some point.

Code can be tested with the development version of ggplot2 by installing it as follows:

# install.packages("pak")
pak::pak("tidyverse/ggplot2")

We aim to release the new ggplot2 version in about 2 weeks, and hope you can submit a fix to CRAN around that time. Hopefully this will inform you in a timely manner.

Best wishes,
Teun

@cpsievert
Copy link
Collaborator

cpsievert commented Jun 12, 2025

Thank you @teunbrand (and sorry you've had to navigate multiple of my gnarly ggplot2 hacks), it is much appreciated!!

@cpsievert cpsievert merged commit ec223b3 into rstudio:main Jun 12, 2025
12 checks passed
@teunbrand
Copy link
Contributor Author

Thanks for working together to tackle problems that sort of are my fault!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dev ggplot2 appears to break thematic

4 participants