Colo(u)rs & Themes

ggplot2
tidyverse
statsglobe
R
colour
Author

Colin Madland

Published

February 8, 2025

Data Visualization in R Using ggplot2 - Module 6

library(ggplot2)  
library(tidyverse)                       # Install & load ggthemes
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ lubridate 1.9.3     ✔ tibble    3.2.1
✔ purrr     1.0.2     ✔ tidyr     1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggthemes)
my_data <- data.frame(x = 1:100,                          # Create example data
                      y = c(cumsum(rnorm(100, 0, 0.5)),
                            cumsum(rnorm(100, 0.1, 0.6)),
                            cumsum(rnorm(100, 0.3, 0.2)),
                            cumsum(rnorm(100, 0.2, 0.8)),
                            cumsum(rnorm(100, 0.2, 1.5))),
                      group = rep(LETTERS[1:5], each = 100))
view(my_data)
ggplot(data = my_data,                                    # Plot without color
       aes(x = x,
           y = y,
           group = group)) +
  geom_line(linewidth = 2)

my_ggp1 <- ggplot(data = my_data,                         # Default colors
                  aes(x = x,
                      y = y,
                      color = group)) +
  geom_line(linewidth = 2)
my_ggp1

my_ggp1 +                                                 # Change colors
  scale_color_manual(values = c("A" = "orange", 
                                "B" = "dodgerblue", 
                                "C" = "#009E73", 
                                "D" = "#F0E442", 
                                "E" = "#D55E00"))

my_ggp1 +                                                 # Discrete color palettes
  scale_color_brewer(palette = "Set1")

my_ggp1 +
  scale_color_brewer(palette = "Set2")

my_ggp1 +
  scale_color_brewer(palette = "Set3")

my_ggp2 <- ggplot(data = my_data,                         # Default scatter plot
                  aes(x = x,
                      y = y,
                      color = x)) +
  geom_point(size = 3)
my_ggp2 

my_ggp2 +                                                 # Continuous color palettes
  scale_color_viridis_c(option = "A")

my_ggp2 +
  scale_color_viridis_c(option = "D")

my_ggp2 +
  scale_color_viridis_c(option = "E")

my_ggp2 +                                                 # Custom color ranges
  scale_color_gradientn(colors = c("#009E73", 
                                   "#D55E00"))

my_ggp2 +
  scale_color_gradientn(colors = c("#009E73", 
                                   "#F0E442", 
                                   "#D55E00"))

my_ggp2 +                                                 # Change theme colors
  theme(panel.background = element_rect(fill = "lightgray"), # Panel background
        plot.background = element_rect(fill = "aliceblue"))  # Plot background

my_ggp2 +                                                 # Border color
  theme(plot.background = element_rect(color = "deepskyblue3",
                                       linewidth = 5))

my_ggp2 +                                                 # ggplot2 themes
  theme_bw()

my_ggp2 +
  theme_void()

my_ggp2 +
  theme_dark()

my_ggp2 +                                                 # Themes by ggthemes
  theme_calc()

my_ggp2 +
  theme_gdocs()

my_ggp2 +
  theme_economist()

Exercises In this module, we will work with the CO2 data set, which contains measurements of carbon dioxide uptake in grass plants under different conditions. To prepare, we will install and load the ggplot2 package and load the CO2 data set. This data set provides both numeric and categorical variables, making it ideal for exploring different colors and themes.

data(CO2)             # Load example data

Now, let’s move on to the exercises for this module: