Using and creating color palettes


The taylor package comes with it's own class of color palettes, inspired by the work of Josiah Parry in the cpcinema package.

Creating palettes

taylor uses vctrs to create a special vector class of color palettes that can be used to create and visualize palettes. We can create a palette using the color_palette() function. We only have to pass a vector of hexadecimal values or valid R color (from colors()), and a palette is created that will print a preview of the colors.

library(taylor)

my_pal <- color_palette(c("firebrick", "turquoise", "#0051ba"))
my_pal

We can also use color_palette() on an existing palette to interpolate additional values, by specifying that we want more colors than were originally specified.

my_big_pal <- color_palette(my_pal, n = 10)
my_big_pal

Similarly, if we have a large color palette, we can select just a few representative colors.

my_small_pal <- color_palette(my_big_pal, n = 5)
my_small_pal

Built-in palettes

The taylor package comes with a few palettes built-in, based on Taylor Swift's album covers. They can be viewed using taylor::album_palettes.

album_palettes

Or we can access a single palette.

album_palettes$fearless_tv

Also included is a palette that includes one representative color from each album, taylor::album_compare.

album_compare

Using color palettes with ggplot2

The taylor package comes with a set of functions built in for plotting in ggplot2 with the album palettes. For example, we can use scale_fill_taylor_c() to create a continuous scale based on one of the album palettes. For more details on how to use the scale functions included in taylor, check out vignette("plotting").

library(ggplot2)

p <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
  geom_tile() +
  theme_minimal()

p + scale_fill_taylor_c(album = "Fearless (Taylor's Version)")

You can also use your custom palettes with ggplot2. For example, we can create a palette of greens, and then use ggplot2::scale_fill_gradientn() or ggplot2::scale_color_gradientn() to use the palette.

green_pal <- color_palette(c("#E5F5E0", "#A1D99B", "#31A354"))
green_pal

ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
  geom_tile() +
  scale_fill_gradientn(colors = green_pal) +
  theme_minimal()

Finally, if we have a discrete scale, we can use ggplot2::scale_fill_manual() or ggplot2::scale_color_manual(). Here, we use the palmerpenguins to map our palette to the species of penguin.

library(palmerpenguins)

penguin_pal <- color_palette(c(Adelie = "firebrick",
                               Chinstrap = "goldenrod",
                               Gentoo = "navy"))
penguin_pal

ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +
  geom_point(aes(shape = species, color = species), size = 3) +
  scale_color_manual(values = penguin_pal) +
  theme_minimal()


Try the taylor package in your browser

Any scripts or data that you put into this service are public.

taylor documentation built on May 29, 2024, 10 a.m.