Mastering Visualization Color with the RColorBrewer Palette! (The Complete Guide to Sequential, Divergent, and Categorical)

When visualizing data, we often find ourselves wondering, "What color should I use to make it look better?" Especially when it comes to Ryou'll feel this pain all the more acutely, but don't worry! Today we're going to show you how to use the RColorBrewer Paletteto bring your data to life.

We'll cover the differences and uses of the sequential, divergent, and categorical palettes. By the time you're done, data visualization will be more fun than ever!

What is the RColorBrewer Palette?

RColorBrewer is a color palette package provided by R that provides a wide range of color combinations for data visualization. In particular, Sequential, Diverging, Categorical (Qualitative) Palettes make it easy to choose colors that match the nature of your data.

Palette types and features

1. sequential palette

  • Features: Ideal for representing data that progresses from low to high values.
  • Example: temperature changes, sales increases, etc.
  • Palette list: There are 18 in total.
    • Blues, BuGn, BuPu, GnBu, Greens, Greys
    • Oranges, OrRd, PuBu, PuBuGn, PuRd, Purples
    • RdPu, Reds, YlGn, YlGnBu, YlOrBr, YlOrRd
  • How to use: You can see the colors in the sequential palette with the code below.
  library(RColorBrewer)
  display.brewer.pal(n = 9, name = "Blues")
RColorBrewer Palette - 순차적 팔레트 예시 이미지

2. the Diverging palette

  • Features: Used to emphasize the values at either extreme relative to the median.
  • Example: temperature change (up/down from average temperature), gains and losses
  • Palette list: There are nine in total.
    • BrBG, PiYG, PRGn, PuOr
    • RdBu, RdGy, RdYlBu, RdYlGn, Spectral
  • How to use: You can see the colors of the divergent palette in the code below
  display.brewer.pal(n = 11, name = "RdBu")
RColorBrewer Palette - 발산형 팔레트 예시 이미지

3. Categorical (Qualitative) palette

  • Features: Perfect for separating different categories.
  • Example: Sales by country, broken down by category
  • Palette list: There are eight in total.
    • Accent (8 colors), Dark2 (8 colors), Paired (12 colors)
    • Pastel1 (9 colors), Pastel2 (8 colors), Set1 (9 colors)
    • Set2 (8 colors), Set3 (12 colors)
  • How to useYou can see the colors in the categorical palette with the code below
  display.brewer.pal(n = 8, name = "Set1")
RColorBrewer Palette - 범주형 팔레트 예시 이미지

View and visualize your palette list

1. view the entire palette

Run the code below to see all the lists and colors from the palettes introduced above.

library(RColorBrewer)
display.brewer.all()
RColorBrewer Palette - 전체 팔레트 예시 이미지

2. check the pallet type

Run the code below to see a list of palettes and colors by type, respectively.

View only the # sequential palette
display.brewer.all(type = "seq")

Show only # divergent palettes
display.brewer.all(type = "div")

Show only # categorical palettes
display.brewer.all(type = "qual")
RColorBrewer Palette - 순차적 팔레트 이미지
(sequential palette list and colors)
RColorBrewer Palette - 발산형 팔레트 이미지
(Diverging palette lists and colors)
RColorBrewer Palette - 발산형 팔레트 이미지
(Categorical palette lists and colors)

Data visualization examples with palettes

1. Heatmaps using the Sequential palette

순차적(Sequential) 팔레트를 활용한 히트맵
Loading the # package
library(RColorBrewer)

Generate # data
set.seed(123)
data <- matrix(rnorm(100), nrow=10)
colnames(data) <- paste0("Col", 1:10)
rownames(data) <- paste0("Row", 1:10)

Draw a # heatmap
heatmap(data,
col = brewer.pal(9, "Blues"),
main = "
Using a Sequential Palette for a Heatmap",
margins = c(5,5))

Interpreting code

  • set.seed(123):
    • Set a seed value to ensure reproducibility of random number generation. Generate the same random number every time you run the same code.
  • matrix(rnorm(100), nrow=10):
    • Create a matrix of 10 rows by generating 100 random numbers from a normal distribution with a mean of 0 and a standard deviation of 1.
  • colnames(data) <- paste0("Col", 1:10):
    • Name the columns "Col1", "Col2", ..., "Col10".
  • rownames(data) <- paste0("Row", 1:10):
    • Name the rows "Row1", "Row2", ..., "Row10".
  • heatmap():
    • Visualize the given data as a heatmap.
  • col = brewer.pal(9, "Blues"):
    • RColorBrewer's sequential palette, the "Blues" Use the palette to set the color of the heatmap. brewer.pal(9, "Blues")The "Blues" Returns 9 colors from the palette.
  • main = "Using a Sequential Palette for a Heatmap":
    • Set a title for the heatmap.
  • margins = c(5,5):
    • Specify the margin size where the row and column names of the heatmap will appear.

2. Tilemap using the Diverging palette

발산형(Diverging) 팔레트를 활용한 타일맵
Loading the # package
library(RColorBrewer)

Generate # data
set.seed(123)
df <- data.frame(
x = rep(1:10, each=10),
y = rep(1:10, times=10),
value = rnorm(100)
)

# Plotting a tilemap with ggplot
library(ggplot2)
ggplot(df, aes(x=x, y=y, fill=value)) +
geom_tile() +
scale_fill_distiller(palette = "RdBu", direction = 1) +
theme_minimal() +
labs(title = "
Using a Diverging Palette for a Tile Map",
x = "X-axis", y = "Y-axis")

Interpreting code

RColorBrewer: A package that provides color palettes for data visualization.

ggplot2: A powerful graphing package for data visualization that allows you to create many different types of graphs.

  • set.seed(123)Sets the seed value to ensure reproducibility of the random number generation. You'll get the same result every time you run the same code.
  • data.frame(): Divide the data into an x-axis, a y-axis, and a value Create it as a data frame organized by columns.
    • x: Iterate over values from 1 to 10, each value 10 times.
    • y: Repeat values from 1 to 10 a total of 10 times.
    • value: Generate random numbers from a normal distribution with mean 0 and standard deviation 1.
  • ggplot(df, aes(x=x, y=y, fill=value)):
    • ggplot() function is a data frame (df) to create a visualization based on the data.
    • aes()defines the axes and color mapping of the visualization:
      • x=x: on the x-axis x Column mappings.
      • y=y: on the y-axis y Column mappings.
      • fill=value: set tile color to value Set based on the value.
  • geom_tile():
    • function that generates a tilemap, where each (x, y) Draw a rectangle corresponding to the coordinates.
  • scale_fill_distiller(palette = "RdBu", direction = 1):
    • RColorBrewer's Divergent Palette "RdBu"to set the color.
      • "RdBu": Contrasting colors from the middle to the two extremes using Red and Blue.
      • direction = 1: Applies the palette in the default direction.
  • theme_minimal():
    • Remove unnecessary elements of the graph and apply a cleaner style.
  • labs():
    • Set the graph title and axis names:
      • Subject: "Using a Diverging Palette for a Tile Map".
      • X-axis name: "X-axis".
      • Y-axis name: "Y-Axis".

3. Bar graph using the Qualitative palette

3. 범주형(Qualitative) 팔레트를 활용한 막대 그래프
Loading the # package
library(RColorBrewer)

Generate # data
categories <- c("A", "B", "C", "D", "E")
values <- c(23, 45, 32, 58, 29)
df <- data.frame(categories, values)

Draw a # bar graph
ggplot(df, aes(x=categories, y=values, fill=categories)) +
geom_bar(stat="identity") +
scale_fill_brewer(palette="Set3") +
theme_minimal() +
labs(title = "Using a Qualitative Palette for a Bar Chart",
x = "Category", y = "Value")

Interpreting code

RColorBrewer: A package that provides color palettes for data visualization.

ggplot2: A powerful graphing package for data visualization that allows you to create many different types of graphs.

  • categoriesCategorical data that will be displayed on the x-axis of the bar graph. Here we define the categories "A", "B", "C", "D", and "E".
  • values: The value corresponding to each category, displayed on the y-axis.
  • data.frame(): categoriesand valuesto combine data frames (df).
  • ggplot(df, aes(x=categories, y=values, fill=categories)):
    • ggplot() function generates a graph based on your data.
    • aes()maps the axes and colors of the visualization:
      • x=categories: on the x-axis categories Column mappings.
      • y=values: on the y-axis values Column mappings.
      • fill=categories: Set the color of the bar to be different for each category.
  • geom_bar(stat="identity"):
    • Generates a bar graph, where the height of each bar is based on the data (values) value, which is set accordingly.
  • scale_fill_brewer(palette="Set3"):
    • Categorical Palettes in RColorBrewer "Set3"to set the color of the bar. "Set3"provides different colors to distinguish between different categories.
  • theme_minimal():
    • Remove unnecessary elements of the graph and apply a cleaner style.
  • labs():
    • Set the graph title and axis names:
      • Subject: "Using a Qualitative Palette for a Bar Chart".
      • X-axis name: "Category".
      • Y-axis name: "Value".

Finalize

RColorBrewer is a powerful tool for data visualization. You can use it to create heatmaps, tilemaps, and more by palette type, Bar graphs Let's look at an example.

Use the Sequential palette for sequential data, which progresses from light to dark, and choose the Divergent palette to highlight median and extreme values. When you need to distinguish between categories, use the categorical palette.

Your data will be more vibrant and easier to understand. Now go ahead and create stunning visualizations with RColorBrewer!

테리 이모티콘
(Happy coding!)

Similar Posts