# packages and global themelibrary(ggplot2)library(dplyr)theme_set(theme_bw(base_size = 14))
head(iris, 3)
Output:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
This tutorial explains all the essential techniques of brewer palettes:
brewer
, distiller
, and fermenter
We’ll use the iris
dataset for illustration.
# packages and global themelibrary(ggplot2)library(dplyr)theme_set(theme_bw(base_size = 14))
head(iris, 3)
Output:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
To apply the brewer color palettes, the basic formula is scale_color_*
(or scale_fill_*
), with *
representing three different suffix options: brewer, distiller, and fermenter, depending on the type of the variable that is mapped to the color
(or fill
) aesthetic.
brewer
When a categorical variable is mapped to the color
aesthetic, use suffix brewer, i.e., in the form of scale_color_brewer()
.
# create a scatterplot<- iris %>% ggplot(aes(Sepal.Length, Sepal.Width)) base
+ base geom_jitter(aes(color = Species), size = 3) + scale_color_brewer(palette = "Accent")
distiller
or fermenter
When a numeric variable is mapped to color
, there are two options:
# Petal.Length a numerical variable+ base geom_jitter(aes(color = Petal.Length), size = 3) + scale_color_distiller(palette = "Accent")
+ base geom_jitter(aes(color = Petal.Length), size = 3) + scale_color_fermenter(palette = "Accent")
When the type of the variable is mismatched with the color scale syntax, it’ll generate an error message typically like: Error: Continuous value supplied to discrete scale.
scale_color_brewer()
, scale_color_distiller()
, and scale_color_fermenter()
(and the scale_fill_*
functions) are all built in the ggplot2 package. However, in order to display the brewer’s available palette names and colors, or to extract colors hex codes from the brewer palettes, you need to additionally load the RColorBrewer
package.
The following code displays the brewer’s palette names and colors. It also shows the maximum number of colors available in the palette when a categorical variable is mapped to color
(or fill
) (see maximum color limit below).
library(RColorBrewer)display.brewer.all()
After loading the RColorBrewer
package, colors can be extracted from a palette and returned as hex codes.
.9 <- brewer.pal(n = 9, name = "Set3") colors.9 colors
Output:
[1] "#8DD3C7" "#FFFFB3" "#BEBADA" "#FB8072" "#80B1D3" "#FDB462" "#B3DE69"
[8] "#FCCDE5" "#D9D9D9"
The scales
package offers a solution to visualize the color hex codes.
library(scales)show_col(colors.9)
When a categorical (discrete) variable is mapped to color
and scale_color_brewer()
, the colors available in the plot is limited by the number of colors in the associated palette. For example, a maximum of 9 colors is available in palette Set 3
(use display.brewer.all()
to show available colors; see above). In like manner, when extracting colors using brewer.pal()
, no more than 9 colors can be extracted from Set 3
.
Note that this maximum color number limit does not apply to scale_color_fermenter()
or scale_color_distiller()
.
This limit in scale_color_brewer()
often makes sense, as too many discrete colors (typically used for categorical variables) are difficult to distinguish visually. However, if you do need more colors, you can use the base R function colorRampPalette()
to interpolate (generate) as many transitional colors as needed (see below).
The following code generates 20 colors with smoother color transition from 9 colors of the Set 3
palette.
.20 <- colorRampPalette(colors.9)(20) colors.20 colors
Output:
[1] "#8DD3C7" "#BDE5BE" "#EDF8B6" "#EDECBD" "#D2CFCD" "#C4B3CF" "#DE9BA3"
[8] "#F78377" "#CD9295" "#99A6BE" "#9AB1BB" "#CEB28B" "#F9B662" "#D9C765"
[15] "#BAD968" "#CAD890" "#E8D1C4" "#F6CEE3" "#E7D3DE" "#D9D9D9"
show_col(colors.20, cex_label = .6)
stat_density_2d_filled()
creates a density heatmap. It divides the continuous density scale into 13 discrete (categorical) intervals, with each interval mapped with a color, and corresponds to color scale scale_fill_brewer()
. The Spectral
palette, however, can only offer a maximum of 11 colors. This leads to incomplete color mapping in the heatmap and legend.
<- faithful %>% f ggplot(aes(waiting, eruptions)) + stat_density_2d_filled() + coord_cartesian(expand = 0)
+ scale_fill_brewer(palette = "Spectral") f
To generate all the needed 13 colors, we can employ color interpolation to the Spectral
palette. Note that the generated colors are supplied to the values
argument in the scale_color_manual()
function.
<- colorRampPalette(brewer.pal(11, "Spectral"))(13) myColors + scale_fill_manual(values = myColors) f
An alternative solution to color interpolation is to use the popular viridis or scico color palettes, which are never limited to the maximum color number limits.
# viridis palettes<- f + scale_fill_viridis_d(option = "A") p1
# scico palettes; require calling / loading the package<- f + scico::scale_fill_scico_d(palette = "batlowK") p2
library(patchwork)| p2 p1