Function and palette queries

Hi everyone!

I'm trying to make a package containing a few themes and palettes for ggplot2.

I have a couple of questions that I couldn't find the answer to on Google; I'd be really grateful to anyone who can help me out!

1. How do I combine two (or more) functions into one function?
I want to combine the theme and scale_colour functions into one function. For example, I'd like to just write "+ viridisbw()" and have the viridis colour scales + the bw theme. Is there any way of writing a viridisbw() function that is just a combination of these two functions? When I tried to write two functions inside one, only the last written function worked.

2. How do I plot only colour palettes?
In other words, how do I basically do display.brewer.pal for my own palettes?

Many thanks in advance!

Part One

plt = ggplot(mapping = aes(x = 1:10, y = 1:10, color = 1:10)) +
  geom_point()

ggplot2 lets you add lists of items, so your function may look something like:

myfunc = function(){
  list(theme_bw(), scale_color_viridis_c())
}

See now:

patchwork::wrap_plots(plt, plt + myfunc())

image

Part Two

You can visualise colours using the scales package.

scales::show_col(c("red", "blue", "green"))

1 Like

Bless you Jack! Thanks a ton.

This topic was automatically closed 7 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.