Combining scale_fill_manual and scale_colour_manual /ggprotos

Hi
I'm trying to create a combined scale (proto?) but I am stuck in how to combine scales.
Here is a reprex of the problem. It would be great with a tip for a general solution that I can use to combine many scale_xxx parameters.

library(ggplot2)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, fill = Species, colour = Species)) +  
      geom_point(size = 3, shape = 21) + 
      scale_color_manual(values = c('darkred', 'darkblue', 'darkgreen')) + 
      scale_fill_manual(values = c('red', 'blue', 'green'))


# # Don't know how to combine scales/ protos
scale_custom <- function(p) {
      p + 
      scale_color_manual(values = c('darkred', 'darkblue', 'darkgreen')) +
      scale_fill_manual(values = c('red', 'blue', 'green'))
}

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, fill = Species, colour = Species)) +
      geom_point(size = 3, shape = 21) +
      scale_custom()
#> Error in scale_custom(): argument "p" is missing, with no default

Created on 2019-11-29 by the reprex package (v0.3.0)

See this post https://stackoverflow.com/questions/56405904/how-to-add-ggproto-objects-together-and-save-for-later-without-call-to-ggplot

I brief, just put your scale_xxx in a list and add that list to your ggplot:

scale_custom <- list(
      scale_color_manual(values = c('darkred', 'darkblue', 'darkgreen')),
      scale_fill_manual(values = c('red', 'blue', 'green'))
)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, fill = Species, colour = Species)) +
      geom_point(size = 3, shape = 21) +
      scale_custom

Cheers
Steen

1 Like

Thats super @stkrog! You made my evening.
I knew it was simple.
Can you update your answer by replacing " +" with "," in the list?

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