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

Feature request: export plot_theme() #5801

Open
teunbrand opened this issue Mar 25, 2024 · 0 comments · May be fixed by #5804
Open

Feature request: export plot_theme() #5801

teunbrand opened this issue Mar 25, 2024 · 0 comments · May be fixed by #5804

Comments

@teunbrand
Copy link
Collaborator

I think it would be neat to have access to the function that completes a theme.
By the time ggplot_build() is run, the theme isn't completed yet, and by the time ggplot_gtable() is run, the theme settings are lost.
My primary use case would be in testing guide extensions: tweak a theme setting and test if it is applied correctly in the guide.
I'd like to do roughly something like this, but this currently doesn't work because the theme isn't complete.

library(ggplot2)
p <- ggplot(mpg, aes(displ, hwy, colour = factor(cyl))) + geom_point()
build <- ggplot_build(p)

guides <- build$plot$guides
my_theme <- build$plot$theme

# Does not work
grob <- guides$assemble(my_theme + theme(legend.direction = "horizontal"))
#> Error in as.unit(e1): object is not coercible to a unit
# Do test on `grob`
grob <- guides$assemble(my_theme + theme(legend.direction = "vertical"))
#> Error in as.unit(e1): object is not coercible to a unit
# Do test on `grob`

To make this work, I'd have to use the unexported plot_theme() function.

my_theme <- theme(!!!ggplot2:::plot_theme(build$plot))
grob <- guides$assemble(my_theme + theme(legend.direction = "vertical"))
# Do test on `grob`
grob <- guides$assemble(my_theme + theme(legend.direction = "vertical"))
# Do test on `grob`

Created on 2024-03-25 with reprex v2.1.0

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

Successfully merging a pull request may close this issue.

1 participant