ggplot2 Color Scales Exercises in R: 25 Practice Problems

Twenty-five practice problems on ggplot2 color and fill scales: manual, brewer, viridis, gradient, hue, custom palettes. Hidden solutions.

RRun this once before any exercise
library(ggplot2)

  

Section 1. Manual scales (5 problems)

Exercise 1.1: scale_color_manual

Difficulty: Beginner.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + scale_color_manual(values = c("red","blue","green"))

  

Exercise 1.2: Named colors

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + scale_color_manual(values = c(setosa = "red", versicolor = "blue", virginica = "green"))

  

Exercise 1.3: scale_fill_manual on bars

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(diamonds, aes(cut, fill = cut)) + geom_bar() + scale_fill_manual(values = c("#1f77b4","#ff7f0e","#2ca02c","#d62728","#9467bd"))

  

Exercise 1.4: Hex color values

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + scale_color_manual(values = c("#e41a1c","#377eb8","#4daf4a"))

  

Exercise 1.5: Reorder legend

Difficulty: Advanced.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + scale_color_manual(values = c("red","blue","green"), breaks = c("virginica","versicolor","setosa"))

  

Section 2. ColorBrewer (5 problems)

Exercise 2.1: scale_color_brewer Set1

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + scale_color_brewer(palette = "Set1")

  

Exercise 2.2: Set2 (qualitative)

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + scale_color_brewer(palette = "Set2")

  

Exercise 2.3: Sequential

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(diamonds, aes(cut, fill = cut)) + geom_bar() + scale_fill_brewer(palette = "Blues")

  

Exercise 2.4: Diverging RdBu

Difficulty: Advanced.

Show solution
RInteractive R
df <- expand.grid(x = 1:10, y = 1:10) df$z <- df$x - df$y ggplot(df, aes(x, y, fill = z)) + geom_tile() + scale_fill_distiller(palette = "RdBu")

  

Exercise 2.5: Reverse direction

Difficulty: Advanced.

Show solution
RInteractive R
ggplot(diamonds, aes(cut, fill = cut)) + geom_bar() + scale_fill_brewer(palette = "Blues", direction = -1)

  

Section 3. Viridis (5 problems)

Exercise 3.1: viridis_d

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + scale_color_viridis_d()

  

Exercise 3.2: viridis_c (continuous)

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(mtcars, aes(wt, mpg, color = hp)) + geom_point() + scale_color_viridis_c()

  

Exercise 3.3: option = "magma"

Difficulty: Advanced.

Show solution
RInteractive R
ggplot(mtcars, aes(wt, mpg, color = hp)) + geom_point() + scale_color_viridis_c(option = "magma")

  

Exercise 3.4: option = "inferno"

Difficulty: Advanced.

Show solution
RInteractive R
ggplot(mtcars, aes(wt, mpg, color = hp)) + geom_point() + scale_color_viridis_c(option = "inferno")

  

Exercise 3.5: viridis on a heatmap

Difficulty: Advanced.

Show solution
RInteractive R
df <- expand.grid(x = 1:10, y = 1:10); df$z <- runif(100) ggplot(df, aes(x, y, fill = z)) + geom_tile() + scale_fill_viridis_c()

  

Section 4. Gradient (5 problems)

Exercise 4.1: Two-color gradient

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(mtcars, aes(wt, mpg, color = hp)) + geom_point() + scale_color_gradient(low = "blue", high = "red")

  

Exercise 4.2: Three-color (diverging)

Difficulty: Intermediate.

Show solution
RInteractive R
df <- data.frame(x = -5:5, y = -5:5, z = -5:5) ggplot(df, aes(x, y, color = z)) + geom_point(size = 5) + scale_color_gradient2(low = "blue", mid = "white", high = "red", midpoint = 0)

  

Exercise 4.3: gradientn n-color

Difficulty: Advanced.

Show solution
RInteractive R
ggplot(mtcars, aes(wt, mpg, color = hp)) + geom_point() + scale_color_gradientn(colors = c("blue","yellow","red"))

  

Exercise 4.4: Limits on gradient

Difficulty: Advanced.

Show solution
RInteractive R
ggplot(mtcars, aes(wt, mpg, color = hp)) + geom_point() + scale_color_gradient(low = "blue", high = "red", limits = c(50, 250))

  

Exercise 4.5: Custom labels on legend

Difficulty: Advanced.

Show solution
RInteractive R
ggplot(mtcars, aes(wt, mpg, color = hp)) + geom_point() + scale_color_gradient(low = "blue", high = "red", breaks = c(100, 200), labels = c("low","high"))

  

Section 5. Hue and others (5 problems)

Exercise 5.1: scale_color_hue (default)

Difficulty: Beginner.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + scale_color_hue()

  

Exercise 5.2: Hue starting position

Difficulty: Advanced.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + scale_color_hue(h.start = 90)

  

Exercise 5.3: scale_color_grey

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + scale_color_grey()

  

Exercise 5.4: Reverse the legend keys

Difficulty: Advanced.

Show solution
RInteractive R
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point() + guides(color = guide_legend(reverse = TRUE))

  

Exercise 5.5: Show fewer breaks on continuous color

Difficulty: Advanced.

Show solution
RInteractive R
ggplot(mtcars, aes(wt, mpg, color = hp)) + geom_point() + scale_color_gradient(low = "blue", high = "red", breaks = c(100, 200))

  

What to do next

  • ggplot2-Exercises (shipped), broader practice.
  • ggplot2-Themes-Exercises (shipped), theme drilling.