Image Credit: Pexels

Color Palettes

The colors are from WSJ article https://www.wsj.com/articles/the-kavanaugh-effect-political-debates-shake-up-the-workplace-1538949970

I will use the colors extracted from the solid bar at the bottom of the image. How did I do that? I used ColorZilla addon for Firefox: https://addons.mozilla.org/en-US/firefox/addon/colorzilla/

Here are the hex codes for 7 colors I extracted:

# Create a color palette from WSJ article
 
wsjPal <- c('#65C1E8',
            '#D85B63',
            '#D680AD',
            '#5C5C5C',
            '#C0BA80',
            '#FDC47D',
            '#EA3B46')

Try out a graph using the new color palette

ggplot(mpg, aes(class)) +
  geom_bar(aes(fill = class), show.legend = FALSE) +
  scale_fill_manual(values = wsjPal) +
  theme_minimal()

Scatter plot

ggplot(mpg, aes(hwy, cty)) +
  geom_point(aes(color = class, size = cyl),
             alpha = 0.6,
             show.legend = FALSE) +
  scale_color_manual(values = wsjPal) +
  scale_size_continuous(range = c(2, 7)) +
  theme_minimal()

What happens when you have more groups compared to colors in the palette?

Get the billionnaires data

rich <- readRDS(here::here('top10_billionnaires.RDS'))

Take a look at the data

head(rich, 10)
## # A tibble: 10 x 6
##    name              lastName   netWorth  rank position  year
##    <chr>             <chr>         <dbl> <int>    <int> <dbl>
##  1 Jeff Bezos        Bezos        112000     1        1  2018
##  2 Bill Gates        Gates         90000     2        2  2018
##  3 Warren Buffett    Buffett       84000     3        3  2018
##  4 Bernard Arnault   Arnault       72000     4        4  2018
##  5 Mark Zuckerberg   Zuckerberg    71000     5        5  2018
##  6 Amancio Ortega    Ortega        70000     6        6  2018
##  7 Carlos Slim Helu  Slim Helu     67100     7        7  2018
##  8 Charles Koch      Koch          60000     8        8  2018
##  9 Larry Ellison     Ellison       58500    10       10  2018
## 10 Michael Bloomberg Bloomberg     50000    11       11  2018

This will not work (Any idea why not? Think about it before running the code.)

ggplot(rich, aes(x = year, y = netWorth)) +
  geom_line(aes(color = name), show.legend = FALSE) +
  scale_color_manual(values = wsjPal) +
  theme_minimal()

We have 10 groups (people) in the data frame but there are only 7 colors in our palette. Either add more colors to the palette or cap number of names to 7. I will do the latter.

inner_join(rich,
           rich %>%
             filter(year == 2017 & rank <= 7) %>% 
             select(name),
           by = 'name'
           ) %>% 
  ggplot(aes(x = year,
             y = netWorth)
         ) +
  geom_line(aes(color = name)) +
  scale_color_manual(values = wsjPal) +
  theme_minimal()

Can we create our own color palette?

Yes, we can! One easy way of doing it is to use paletti package.

devtools::install_github("edwinth/paletti")

Visualize the colors from wsjPal

paletti::viz_palette(wsjPal)

Now use these colors in fill and color

myFill  <- paletti::get_scale_fill(paletti::get_pal(wsjPal))
myColor <- paletti::get_scale_color(paletti::get_pal(wsjPal))

Without our colors

ggplot(mpg, aes(class)) +
  geom_bar(aes(fill = class), show.legend = FALSE) +
  theme_minimal()

With our colors

ggplot(mpg, aes(class)) +
  geom_bar(aes(fill = class), show.legend = FALSE) +
  theme_minimal() +
  myFill()

Using premade color palettes outside ggplot2

(This code is not evaluated here so it will not produce any output.)

# Scientific journals and even some fun palettes like Rick and Morty
install.packages("ggsci")

vignette("ggsci")

# Wes Anderson colors
install.packages('wesanderson')

# Mother of all packages
# This has collection of palettes from several packages

install.packages("paletteer")

Related

comments powered by Disqus