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:
Thanks for the response, @leungi! I am indeed developing a custom theme, but my question is around the possibility of conditionally changing theme settings based on the plot object passed to my theme_custom() function. In other words if the plot is not faceted, the theme would look one way and if the plot is faceted, it would apply different parameters to the theme.
Thanks — and yes I can see how I can make theme options arguments of the custom theme function. But what I’m wondering about is if my custom theme function can “detect” (or access) elements of the ggobject that theme_() modifies. If so, given your example, the theme would automatically set the font based on some element of the plot (say, does or does not have facets) rather than requiring a user to set is_flag = TRUE.
The basic idea is to rewrite ggplot_add.theme to set the conditional them based on the parameters of the plot it is added to. Here's the code to do it: