How to pass theme to use as argument of a function

I have written a function that sets up a ggplot plot. I would like to allow the user to set a theme of their choosing through an argument in the function call (I have named it themeToUse !) However, I cannot see how make this work. Using theme_set("theme_bw()") inside the function doesn't work, nor does theme_set(get("theme_bw()")) Clearly I don't understand anything like enough about themes and I admit that passing variable names is a bit of a dark art to me and I know that every time I need to do things like that I have to search around but searching the internet for this hasn't worked for me. I suspect someone out there knows the answer and that it's trivially easy. TIA! Chris

Here is an example of what I think you want to do.

DF <- data.frame(A = 1:4, B = 2:5)
Gplot <- function(themeToUse = theme_bw()) {
  ggplot(DF, aes(A, B)) + geom_point() + themeToUse
Plt <- Gplot(themeToUse = theme_dark())
Plt <- Gplot(themeToUse = theme_gray())

Oh how embarrassing!! It didn't occur to me that a simple unquoted passing was all I needed. I guess I should have realised that themes are just lists so this would work. I can confirm, of course, that using theme_set(themeToUse) also works to invoke the theme but I will use + themeToUse: simpler. Thanks FJCC (love the profile picture!) Chris

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.