tidyverse/ggplot2

`ggplot2` 4.0.0 renders custom theme unusable

Closed this issue · 4 comments

Since a while I am using the https://github.com/benostendorf/customplotr package to set my ggplot2 themes, but since I updated ggplot2 to 4.0.0 the usage of theme_custom2 raises an error:

# failing MRE
library(ggplot2)


ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point() +
  customplotr::theme_custom2
# output
Error in `plot_theme()`:
! The `legend.title` theme element must be a <element_text> object.
Run `rlang::last_trace()` to see where the error occurred.
Warning message:
customplotr::theme_custom2 is not a valid theme.
Please use `theme()` to construct themes. 

If I simply manually define the same theme manually it works as intended.

# working MRE
theme_custom2 <-
  ggplot2::theme(
    # text = element_text(family = "Helvetica-Narrow"),
    panel.grid = element_blank(),
    axis.line = element_line(size = customplotr::custom_linewidth),
    axis.ticks = element_line(size = customplotr::custom_linewidth),
    axis.ticks.length = unit(0.075, "cm"),
    # axis.text.x = element_text(hjust = 1),
    axis.title = element_text(size = 6),
    axis.text = element_text(size = 5, colour = "black"),
    plot.title = element_text(size = 7, hjust = 0.5),
    panel.background = element_blank(),
    legend.text = element_text(size = 5),
    legend.title = element_blank(),
    # legend.margin = margin(t = -1, r = 0, l = -0.3, unit = 'cm'),
    legend.key.size = unit(0.25, "line")
  )

ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point() +
  theme_custom2

Does anyone have a clue how to fix this?

It seems the problem is that the package stores the R object instead of calling ggplot2::theme(). So, I guess there's nothing we can do to fix this. Could you file the issue on the package's repo, not here?

https://github.com/benostendorf/customplotr/blob/d45d8acc944083077af44814cabce73ee341ebe2/data-raw/theme_custom2.R#L18

Thanks for your answer.

That's what I assumed, too. Just wanted to make sure. I'm in contact with the dev there already. So, I guess the R object would need to be recreated using updated packages?

Thanks.

So, I guess the R object would need to be recreated using updated packages?

I believe the package should not store the R object and call ggplot2::theme() every time because the internal structure of the result object of theme() is not the API and might change between versions. However, if the package author doesn't have enough time to fix it properly, probably recreating it would be the easiest thing to do at the moment.

Perfect, thanks for the help. I'll forward your suggestions :).