Kamaloka
Kamaloka

Reputation: 139

Exporting custom color palettes

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 !

Upvotes: 0

Views: 722

Answers (3)

Allan Cameron
Allan Cameron

Reputation: 173803

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

Upvotes: 1

Matt
Matt

Reputation: 7385

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")

Upvotes: 0

Kent Johnson
Kent Johnson

Reputation: 3388

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

Upvotes: 0

Related Questions