Code
library(RColorBrewer)
library(tidyverse)
# Display colorblind-friendly palettes
display.brewer.all(colorblindFriendly = TRUE)Choosing the right color scheme is vital for effective storytelling. This guide demonstrates four systemic ways to manage color in your ggplot2 visualizations, ranging from automated palettes to manual Hex code mapping.
The Art of Color in ggplot2: From Palettes to Hex Codes
Introduction Color is one of the most powerful tools in data visualization. It can be used to highlight patterns, group data, or evoke a specific professional tone. In this guide, we explore how to move from default colors to custom palettes using RColorBrewer and manual Hex codes.
1. RColorBrewer (Professional Palettes) The RColorBrewer package offers pre-designed, colorblind-friendly palettes.
scale_color_brewer() ensures that your colors are aesthetically harmonious and accessible to a wider audience.2. Manual Control (Hex Codes & Named Colors) For maximum precision—such as matching a brand’s identity—we use scale_color_manual().
#719AC9) provides millions of color possibilities, allowing for a highly specific and professional look.The RColorBrewer library provides curated palettes that are scientifically designed for clarity.
library(RColorBrewer)
library(tidyverse)
# Display colorblind-friendly palettes
display.brewer.all(colorblindFriendly = TRUE)Ideal for categorical data where there is no inherent ordering (e.g., drive types).
mpg %>%
ggplot(aes(displ, hwy, color = drv)) +
geom_jitter(size = 5) +
scale_color_brewer(palette = "Set2") +
theme_minimal() +
labs(title = "Using RColorBrewer: Set2 Palette")Sometimes, you don’t need a scale; you just want a single, professional color for all your data points.
mpg %>%
ggplot(aes(displ, hwy)) +
geom_jitter(color = "#97B3C6", size = 5) +
theme_minimal() +
labs(title = "Single Manual Color Mapping",
subtitle = "Using Hex Code #97B3C6")scale_color_manual() allows you to assign specific colors to specific levels of your data. This is crucial for consistency across multiple charts in a report.
mpg %>%
ggplot(aes(displ, hwy, color = drv)) +
geom_jitter(size = 3) +
scale_color_manual(values = c("4" = "blue",
"f" = "red",
"r" = "green")) +
theme_minimal() +
labs(title = "Manual Mapping: Basic Names")Hex codes are the industry standard for professional design. They ensure your “blue” is exactly the shade you intended.
mpg %>%
ggplot(aes(displ, hwy, color = drv)) +
geom_jitter(size = 5, alpha = 0.5) +
scale_color_manual(values = c("4" = "#719AC9",
"f" = "#75B99C",
"r" = "#C98D71")) +
theme_minimal() +
labs(title = "Manual Mapping: Professional Hex Codes",
subtitle = "Specific shades mapped to Drive Types")| Method | Function | Best Use Case |
|---|---|---|
| RColorBrewer | scale_color_brewer() |
Quick, professional, and accessible palettes. |
| Manual Hex | scale_color_manual() |
Brand-specific colors or custom designs. |
| Direct Color | geom_point(color = "...") |
When grouping is not required. |
| Transparency | alpha |
Managing overlapping points in dense data. |