When working with colors in R, the
RColorBrewer
package is indispensable. It provides a
variety of color palettes, showcased in the attached
figure. This page is designed solely to display the composition of each
palette, while detailed usage
instructions are available on other pages.
{RColorBrewer}
The RColorBrewer
package in R is an extension of the ggplot2
package, designed to simplify the process of combining multiple
plots into a single layout.
The library provides a variety of color palettes that can be used to customize the appearance of your plots. The figure below showcases all the palettes available in the package.
✍️ author → Erich Neuwirth
📘 documentation → CRAN
To get started with RColorBrewer
, you can install it
directly from CRAN using the install.packages
function:
There are 3 types of palettes :
Sequential palettes are suited to ordered data that progress from low to high. Lightness steps dominate the look of these schemes, with light colors for low data values to dark colors for high data values.
Diverging palettes put equal emphasis on mid-range critical values and extremes at both ends of the data range. The critical class or break in the middle of the legend is emphasized with light colors and low and high extremes are emphasized with dark colors that have contrasting hues.
Qualitative palettes do not imply magnitude differences between legend classes, and hues are used to create the primary visual differences between classes. Qualitative schemes are best suited to representing nominal or categorical data.
Thanks to the brewer.pal()
function, you can easily get
the hex codes of a palette.
Example:
## [1] "#D53E4F" "#F46D43" "#FDAE61" "#FEE08B" "#E6F598" "#ABDDA4" "#66C2A5"
## [8] "#3288BD"
The display.brewer.pal()
function allows you to
visualize a palette.
Example:
Since RColorBrewer
is an extension of
ggplot2
, you can use it to customize the color of your
plots.
Here are 3 examples that use 1 sequential palette, 1 diverging palette, and 1 qualitative palette:
library(RColorBrewer)
colors_seq = brewer.pal(n = 8, name = "Blues")
colors_div = brewer.pal(n = 8, name = "RdYlBu")
colors_qual = brewer.pal(n = 8, name = "Set3")
# basic plot
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point(size = 3) +
scale_color_manual(values = colors_seq) +
theme_minimal()
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point(size = 3) +
scale_color_manual(values = colors_div) +
theme_minimal()
ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
geom_point(size = 3) +
scale_color_manual(values = colors_qual) +
theme_minimal()
The gallery is filled with examples that showcase
the versatility of the RColorBrewer
package. Each example
is designed to help you understand how to use custom
colors in your plots.
👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥