Create a custom ggplot2 function that combines theme and color

I often use the theme_hc () theme (from the ggthemes package) in ggplot2 graphs, combined with scale_colour_pander () or scale_fill_pander (). I want to create a custom function, now called myTheme, that combines these three functions into one.

I tried the following

myTheme <- function(){ theme_hc() + scale_colour_pander() + scale_fill_pander() } data <- data.frame(x=1:2,y=3:4) ggplot(data, aes(x=x, y=y)) + geom_point() + myTheme() 

But, apparently, R evaluates this first inside the function and gives an error: "Error: I don’t know how to add scale_colour_pander () to the theme object."

Then i tried

 myTheme <- function(){ ggplot() + theme_hc() + scale_colour_pander() + scale_fill_pander() } data <- data.frame(x=1:2,y=3:4) ggplot(data, aes(x=x, y=y)) + geom_point() + myTheme() 

Which returns: "Error: I don’t know how to add o to the plot"

Is there a way to achieve the desired effect, or should I continue to combine individual commands?

+5
source share
1 answer

the standard technique is to wrap these items in a list,

 p + list( theme_hc() , scale_colour_pander() , scale_fill_pander()) 
+5
source

Source: https://habr.com/ru/post/1239974/


All Articles