0

I am looking for a way to create my own color palettes that could be called within ggplot, and exported and shared with my team - not requiring to define palettes at each .R code. Something like custom Excel theme colors saved as *.thmx for instance.

It would ideally looks like this :

ggplot(mtcars, aes(wt, mpg)) +
    geom_point(size=4, aes(colour = factor(cyl))) +
    scale_colour_brewer(palette="Mypalette")

with Mypalette being somehow saved in the desktop and being callable directly, not requiring to define it within my code beforehand.

or like Viridis package - probably not the simplest solution

ggplot(mtcars, aes(wt, mpg)) +
    geom_point(size=4, aes(colour = factor(cyl))) +
    scale_color_viridis(discrete=TRUE)

My goal is to end up with common shared palettes allowing visual coherence within graphs made by several contributors.

If you have any tips or advice, I'm more than interrested !

Thanks a lot !

Kamaloka
  • 81
  • 5
  • Related: https://stackoverflow.com/questions/36476751/associate-a-color-palette-with-ggplot2-theme – MrFlick Mar 10 '20 at 20:06
  • Related: https://stackoverflow.com/questions/10504724/change-the-default-colour-palette-in-ggplot – MrFlick Mar 10 '20 at 20:08
  • 1
    Set up a package!! That’s what I’ve done at my company. The second link by @MrFlick is a very good starting point for how the functions have to look like. That’s basically the same approach I followed in the package I wrote for my company. I also added a theme for our corporate design. The package could be put on github or you can setup your own local miniCran. So everybody can use it like every other package. – stefan Mar 10 '20 at 20:41
  • Yes that is the approach I will favor in a first instance. I believe it is the best and straight forward one. Thanks for the links – Kamaloka Mar 10 '20 at 20:56
  • I think it will also be a good idea to store the breaks used to colour your data, this makes it possible to compare plots using the same data: i.e. scale_color_manual(breaks = c("2", "1", "0.5"), values=c("red", "blue", "green")) – Kempie Mar 10 '20 at 21:04
  • @Kamaloka Setting up a package might be good for all sorts of reasons, but it's not the most straightforward solution - see my edited answer – Allan Cameron Mar 10 '20 at 21:34

3 Answers3

1

Presumably you would like to be able to do something simple like add + scale_colour_company() in your plots. It's actually very easy to do this without needing a whole package (though of course if you have multiple R users at your company there might be other good reasons for doing that anyway).

Suppose you wanted to be able to do this:

data.frame(x = runif(30), y = runif(30), z = factor(rep(letters[1:3], 10))) %>%
  ggplot(aes(x = x, y = y, colour = z)) +
  geom_point(size = 5) + 
  scale_colour_company()

enter image description here

All you need is to define a palette function that takes a single integer and returns a vector of characters representing colours. For example:

company_palette <- function(n)
{
  company_colours <- c("forestgreen", "steelblue1", "#FD759A", "#A39847")
  if(n > length(company_colours)) stop("Need more company colours!")
  return(company_colours[seq(n)])
}

Now you can create your ggplot-compatible functions very simply:

scale_fill_company <- function() discrete_scale("fill", "A", palette = company_palette)
scale_colour_company <- function() discrete_scale("colour", "A", palette = company_palette)

Save these 6 lines in a .R script, and you're good to go

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
0

To share palettes with your team, you could make a package with the palettes. Look at one of the many palette packages for an example, say https://github.com/nanxstats/ggsci

Kent Johnson
  • 3,320
  • 1
  • 22
  • 23
0

Managing your team sounds like the most difficult part of this process. Do you work on a shared directory? Do you use GitHub or a similar platform?

If you're on a shared directory, you could standardize some R code that reads in a file and assigns colors to a variable called "Mypallette".

You just need to assign a character vector to Mypallette using hex codes, and educate your team about using the process.

Example:

Mypallette <- c("#CA0020","#F4A582", "#D3D3D3", "#92C5DE", "#0571B0")
Matt
  • 7,255
  • 2
  • 12
  • 34
  • Yes we are. This is what we have been doing so far. It is simply a matter of skiping those visual aspects which does not really fascinate anyone. Thanks for your answer. – Kamaloka Mar 10 '20 at 20:51