I'm working on a custom ggplot2 theme for my company and was thinking it could be nifty to automatically modify elements of the theme depending on certain characteristics of the the plot object. For instance, would there be a way to specify that if the plot contains facets, add a border to each panel?
I guess the question is really, can I access the current
gg object from within a custom
theme() call and then conditionally apply certain theme elements? Basically I would want the following to work without having to add the final
theme(panel.border = element_rect(color = "gray 50", fill = NA)) line:
library(ggplot2) ggplot(mtcars, aes(mpg, wt)) + geom_point() + theme_minimal()
ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(vars(cyl)) + theme_minimal() + theme(panel.border = element_rect(color = "gray 50", fill = NA))
Created on 2019-09-26 by the reprex package (v0.3.0)