---
title: "Using and creating color palettes"
description: |
  The plots were in screaming color.
vignette: >
  %\VignetteIndexEntry{Using and creating color palettes}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, child="children/chunk-options.txt"}
```

The taylor package comes with it's own class of color palettes, inspired by the work of [Josiah Parry](https://josiah.rs/) in the [cpcinema](https://github.com/JosiahParry/cpcinema) package.

## Creating palettes

taylor uses [vctrs](https://vctrs.r-lib.org/) 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.

```{r}
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.

```{r}
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.

```{r}
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 `?album_palettes`.

```{r}
album_palettes
```

Or we can access a single palette.

```{r}
album_palettes$fearless_tv
```

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

```{r}
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")`.

```{r message = FALSE, fig.alt = "A heatmap showing a positive relationship between the waiting time between eruptions and the length of eruptions at the Old Faithful geyser. The heat map is colored using the palette based on Fearless (Taylor's Version), which moves from a dark golden brown for low density combinations up to bright gold for high density combinations."}

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.

```{r fig.alt = "The same heatmap as the previous figure, but instead of the fill using a palette based on Fearless (Taylor's Version), the color palette goes from light green to dark green."}

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 `?penguins` data to map our palette to the species of penguin.

```{r message = FALSE, warning = FALSE, fig.alt = "A scatter plot with bill length on the x-axis and bill depth on the y-axis. The shape and color of the points correspond to the species of penguin, with colors derived from our custom color palette. Adelie penguins are shown in red circles, Chinstrap penguins in yellow triangles, and Gentoo penguins in blue squares."}

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

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