12.4 Using a Different Palette for a Discrete Variable

12.4.1 Problem

You want to use different colors for a discrete mapped variable.

12.4.2 Solution

Use one of the scales listed in Table 12.1.

Table 12.1: Discrete fill and color scales
Fill scale Color scale Description
`scale_fill_discrete()` `scale_colour_discrete()` Colors evenly spaced around the color wheel (same as `hue`)
`scale_fill_hue()` `scale_colour_hue()` Colors evenly spaced around the color wheel (same as `discrete`)
`scale_fill_grey()` `scale_colour_grey()` Greyscale palette
`scale_fill_viridis_d()` `scale_colour_viridis_d()`
Viridis palettes
`scale_fill_brewer()` `scale_colour_brewer()` ColorBrewer palettes
`scale_fill_manual()` `scale_colour_manual()` Manually specified colors

In the example here we’ll use the default palette (hue), a viridis palette, and a ColorBrewer palette (Figure 12.6):

``````library(gcookbook)  # Load gcookbook for the uspopage data set
library(viridis)  # Load viridis for the viridis palette

# Create the base plot
uspopage_plot <- ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) +
geom_area()

# These four specifications all have the same effect
uspopage_plot
# uspopage_plot + scale_fill_discrete()
# uspopage_plot + scale_fill_hue()
# uspopage_plot + scale_color_viridis()

# Viridis palette
uspopage_plot +
scale_fill_viridis(discrete = TRUE)

# ColorBrewer palette
uspopage_plot +
scale_fill_brewer()``````

12.4.3 Discussion

Changing a palette is a modification of the color (or fill) scale: it involves a change in the mapping from numeric or categorical values to aesthetic attributes. There are two types of scales that use colors: fill scales and color scales.

With `scale_fill_hue()`, the colors are taken from around the color wheel in the HCL (hue-chroma-lightness) color space. The default lightness value is 65 on a scale from 0–100. This is good for filled areas, but it’s a bit light for points and lines. To make the colors darker for points and lines, as in Figure 12.7 (right), set the value of `l` (luminance/lightness):

``````# Create the base scatter plot
hw_splot <- ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) +
geom_point()

# Default lightness = 65
hw_splot

# Slightly darker, set lightness = 45
hw_splot +
scale_colour_hue(l = 45)``````

The viridis package provides a number of color scales that make it easy to see differences across your data. See Recipe 12.3 for more details and examples.

The ColorBrewer package provides a number of palettes. You can generate a graphic showing all of them, as shown in Figure 12.8:

``````library(RColorBrewer)
display.brewer.all()``````

The ColorBrewer palettes can be selected by name. For example, this will use the “Oranges” palette (Figure 12.9):

``````hw_splot +
scale_colour_brewer(palette = "Oranges") +
theme_bw()``````

You can also use a palette of greys. This is useful for print when the output is in black and white. The default is to start at 0.2 and end at 0.8, on a scale from 0 (black) to 1 (white), but you can change the range, as shown in Figure 12.10.

``````hw_splot +
scale_colour_grey()

# Reverse the direction and use a different range of greys
hw_splot +
scale_colour_grey(start = 0.7, end = 0)``````

To select colors manually, see Recipe 12.5.

For more about viridis, see https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html. For more about ColorBrewer, see http://colorbrewer2.org.