Make your own color palettes with paletti

December 22, 2017
By

[This article was first published on That’s so Random, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Last week I blogged about the dutchmasters color palettes package, which was inspired
by the wonderful ochRe package. As
mentioned I shamelessly copied the package. I replaced the list with character
vectors containing hex colors and did a find and replace to make it
dutchmasters instead of ochRe. This was pretty ugly. I realized that when
we would refactor the ochRe functions, thus creating functions that create the functions, there would no longer be a need to copy-paste and find-and-replace. So that is what I did. I refactored and expanded ochRe’s chore into paletti. (Name chosen because I liked the ring of it). You grab it from Github, with devtools::install_github("edwinth/paletti").

Single palettes

paletti takes both single palettes (character vectors with hex codes) and
lists with palettes, like the ochRe and dutchmasters lists. Lets start with
a single palette, this might be useful when you want your coporate identity
colors translated into R. Here I just pick some colors found on the interweb

library(paletti)
library(ggplot2)
mycols <- c(
  red    = "#E27D60",
  blue   = "#085DCB",
  orange = "#E8A87C",
  purple = "#C38D9E",
  green  = "#41B3A3"
)
viz_palette(mycols)

plot of chunk unnamed-chunk-1

Now, ochRe provided us with two functions, one two create a ggplot scale for
colours and to create one for fills. These functions can be created in the
following fashion

mycols_fill  <- get_scale_fill(get_pal(mycols))
mycols_color <- get_scale_color(get_pal(mycols))

Both now can be used in ggplot

mtcars$cyl <- as.character(mtcars$cyl)
col_plot <- ggplot(mtcars, aes(mpg, drat, color = cyl)) +
  geom_point(size = 4) 
col_plot + mycols_color()

plot of chunk unnamed-chunk-3

fill_plot <- ggplot(mtcars, aes(cyl, fill = cyl)) + 
  geom_bar()
fill_plot + mycols_fill()

plot of chunk unnamed-chunk-3

Now, I said I expanded the ochRe code a bit. The function get_hex will
produce a function that will return a function in which you can directly return
the hex code by typing its unquoted name. Handy if you want an exact color from
your palette. Prerequisit is that your palette is a named character vector.

mycols_hex <- get_hex(mycols)
fill_plot + scale_fill_manual(values = mycols_hex(blue, purple, red))

plot of chunk unnamed-chunk-4

Multiple palettes in a list

Both ochRe and dutchmasters offer multiple palettes in a list. The only
difference from a single palette is that in the returned function you have to
specify the name of the palette youw want to use. If you don’t, it defaults to
the first palette in the list.

ochRe_col         <- get_scale_color(get_pal(ochre_palettes))
dutchmasters_fill <- get_scale_fill(get_pal(dutchmasters))

col_plot + ochRe_col("lorikeet")

plot of chunk unnamed-chunk-5

fill_plot + dutchmasters_fill("view_of_Delft")

plot of chunk unnamed-chunk-5

And the same holds for the get_hex function. You can feed a list with palettes
as well. Note that the palettes that you are going to call must have named
elements.

dutchmasters_hex <- get_hex(dutchmasters)
fill_plot + scale_fill_manual(values = dutchmasters_hex("anatomy",
                              black(cloak), red(muscle), brown(table)))

plot of chunk unnamed-chunk-6

That’s it, off you go! Add your own color palette(s) and start plotting. Once
again a major thanks to the ochRe team for the inspiration and the foundations
on which paletti is built.

To leave a comment for the author, please follow the link and comment on their blog: That’s so Random.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)