ggplot2 scale_color_brewer() in R: ColorBrewer Palettes
The scale_color_brewer() function in ggplot2 applies named ColorBrewer palettes to discrete groups. ColorBrewer palettes are designed for cartography but work great for any categorical visualization.
+ scale_color_brewer(palette = "Set1") + scale_color_brewer(palette = "Dark2", direction = -1) + scale_fill_brewer(palette = "Spectral") + RColorBrewer::display.brewer.all() # see all palettes + scale_color_manual(values = ...) # custom alternative
Need explanation? Read on for examples and pitfalls.
What scale_color_brewer() does in one sentence
scale_color_brewer(palette = "Set1") colors discrete groups using a named ColorBrewer palette. ColorBrewer (cynthia.bristows.com/colorbrewer) is a standard set of perceptually balanced palettes.
Syntax
scale_color_brewer(palette = "Set1", direction = 1, type = "seq", ...). type is "seq" (sequential), "qual" (qualitative), or "div" (diverging).
Five common patterns
1. Set1 (default qualitative)
2. Dark2 (more saturated)
3. Sequential ordered
4. Reverse direction
5. fill version
scale_color_brewer() vs scale_color_viridis_d() vs scale_color_manual
| Function | Source | Color count |
|---|---|---|
scale_color_brewer() |
ColorBrewer | 3-12 |
scale_color_viridis_d() |
Viridis | Any |
scale_color_manual() |
Custom | Any |
scale_color_grey() |
Greyscale | 2-9 |
When to use which:
- brewer for 3-9 categorical levels.
- viridis for many levels or perceptual uniformity.
- manual for custom / brand colors.
A practical workflow
Browse all ColorBrewer palettes with RColorBrewer::display.brewer.all() then pick one.
Common pitfalls
Pitfall 1: too many categories. ColorBrewer maxes at 8-12. With more, ggplot warns and recycles or interpolates.
Pitfall 2: wrong palette type. Don't use sequential palettes for unordered categories; use qualitative.
scale_color_brewer requires RColorBrewer package. ggplot installs it as a dependency, but in stripped environments you may need to install separately.Try it yourself
Try it: Color mtcars points by cyl using "Dark2" palette. Save to ex_plot.
Click to reveal solution
Explanation: Dark2 is a saturated qualitative palette good on white backgrounds.
Related ggplot2 / RColorBrewer functions
After mastering scale_color_brewer, look at:
scale_fill_brewer(): fill versionscale_color_distiller(): continuous version of brewerscale_color_viridis_d(): viridis alternativescale_color_manual(): custom valuesRColorBrewer::display.brewer.all(): visualize all palettes
FAQ
What does scale_color_brewer do in ggplot2?
scale_color_brewer(palette = "Set1") assigns colors from a named ColorBrewer palette to discrete factor levels.
What is the difference between scale_color_brewer and scale_color_viridis_d?
brewer uses ColorBrewer's curated palettes (small N). viridis is perceptually uniform and works for any number of levels.
How many categories can scale_color_brewer handle?
Most ColorBrewer palettes max at 8-12 colors. For more, use viridis or manual colors.
What is the difference between qualitative, sequential, and diverging palettes?
qualitative for unordered groups (Set1). sequential for ordered (Blues). diverging for data centered on a midpoint (RdBu, Spectral).
Can I see all ColorBrewer palettes in R?
Yes. RColorBrewer::display.brewer.all() shows every palette in a grid.