Reputation: 2800
I am often using the theme_hc() theme (from the package ggthemes) in ggplot2 graphs, combined with scale_colour_pander() or scale_fill_pander(). I want to make 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: Don't know how to add scale_colour_pander() to a 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: Don't know how to add o to a plot'
Is there a way to achieve the desired effect or should I keep combining the individual commands?
Upvotes: 8
Views: 2960
Reputation: 77116
the standard technique is to wrap those elements in a list,
p + list( theme_hc() , scale_colour_pander() , scale_fill_pander())
Upvotes: 9