Mastering Color Scales in ggplot2

Using RColorBrewer and Manual Hex Customization

Author

Abdullah Al Shamim

Published

February 11, 2026

Introduction

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.

  • Systemic Benefit: Using 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().

  • Logic: You can map specific colors to specific data levels (e.g., “4” = “Blue”). Using Hex Codes (like #719AC9) provides millions of color possibilities, allowing for a highly specific and professional look.

1. Using Color Palettes (RColorBrewer)

The RColorBrewer library provides curated palettes that are scientifically designed for clarity.

Code
library(RColorBrewer)
library(tidyverse)

# Display colorblind-friendly palettes
display.brewer.all(colorblindFriendly = TRUE)

Type 1: Qualitative Palettes

Ideal for categorical data where there is no inherent ordering (e.g., drive types).

Code
mpg %>% 
  ggplot(aes(displ, hwy, color = drv)) +
  geom_jitter(size = 5) +
  scale_color_brewer(palette = "Set2") +
  theme_minimal() +
  labs(title = "Using RColorBrewer: Set2 Palette")


2. Taking Direct Control

Sometimes, you don’t need a scale; you just want a single, professional color for all your data points.

Code
mpg %>%
  ggplot(aes(displ, hwy)) +
  geom_jitter(color = "#97B3C6", size = 5) +
  theme_minimal() +
  labs(title = "Single Manual Color Mapping",
       subtitle = "Using Hex Code #97B3C6")


3. Specific Manual Mapping

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.

Code
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.

Code
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")


Systemic Summary Toolkit

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.