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$yggplot(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))