Skip to content

ggplot2 4.0.0 renders custom theme unusable #6618

@gernophil

Description

@gernophil

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions