Hands-on Exercise 2: Beyond ggplot2 Fundamentals

Author

Zou Jiaxun

Published

April 13, 2024

1 Installing and Loading the required library

1.1 check the required packages: ggrepel, ggthemes, hrbrthemes, patchwork, ggplot2

Click to view code
pacman::p_load(ggrepel, patchwork, ggthemes,
               hrbrthemes, tidyverse, ggplot2)

2 Importing data

Click to view code
exam_data <- read.csv("data/Exam_data.csv")
reader is one of the tidyverse package

3 Beyond ggplot2 Annotation:ggrepel

Click to view code
ggplot(data = exam_data,
       aes(x = MATHS, y = ENGLISH)) +
  geom_point() +
  geom_smooth(method = lm, size = 0.5) +
  geom_label(aes(label = ID),
             hjust = 0.5,
             vjust = -.5) +
  coord_cartesian(xlim = c(0,100),
                  ylim = c(0,100)) +
  ggtitle("English scores versus Maths scores for Primary 3")

orignal plot
Click to view code
ggplot(data = exam_data,
       aes(x = MATHS, y = ENGLISH)) +
  geom_point() +
  geom_smooth(method = lm, size = 0.5) +
  geom_label_repel(aes(label = ID),
             hjust = 0.5,
             vjust = -.5) +
  coord_cartesian(xlim = c(0,100),
                  ylim = c(0,100)) +
  ggtitle("English scores versus Maths scores for Primary 3")

using ggrepel

4 Beyond ggplot2 Themes

4.1 Built_in themes

eight built_in themes: theme_gray(), theme_bw(), theme_classic(), theme_dark(), theme_light(), theme_linedraw(), theme_minimal(), and theme_void()

4.1.1 theme_gray

Click to view code
ggplot(data = exam_data, aes(x = MATHS)) +
  geom_histogram(bins = 20, boundary = 100, color = "grey25",
                 fill = "grey90") +
  theme_gray() +
  ggtitle("Distribution of Maths scores")

4.1.2 theme_bw

Click to view code
ggplot(data = exam_data, aes(x = MATHS)) +
  geom_histogram(bins = 20, boundary = 100, color = "grey25",
                 fill = "grey90") +
  theme_bw() +
  ggtitle("Distribution of Maths scores")

4.1.3 theme_classic

Click to view code
ggplot(data = exam_data, aes(x = MATHS)) +
  geom_histogram(bins = 20, boundary = 100, color = "grey25",
                 fill = "grey90") +
  theme_classic() +
  ggtitle("Distribution of Maths scores")

4.1.4 theme_dark

Click to view code
ggplot(data = exam_data, aes(x = MATHS)) +
  geom_histogram(bins = 20, boundary = 100, color = "grey25",
                 fill = "grey90") +
  theme_dark() +
  ggtitle("Distribution of Maths scores")

4.1.5 theme_light

Click to view code
ggplot(data = exam_data, aes(x = MATHS)) +
  geom_histogram(bins = 20, boundary = 100, color = "grey25",
                 fill = "grey90") +
  theme_light() +
  ggtitle("Distribution of Maths scores")

4.1.6 theme_linedraw

Click to view code
ggplot(data = exam_data, aes(x = MATHS)) +
  geom_histogram(bins = 20, boundary = 100, color = "grey25",
                 fill = "grey90") +
  theme_linedraw() +
  ggtitle("Distribution of Maths scores")

4.1.7 theme_minimal

Click to view code
ggplot(data = exam_data, aes(x = MATHS)) +
  geom_histogram(bins = 20, boundary = 100, color = "grey25",
                 fill = "grey90") +
  theme_minimal() +
  ggtitle("Distribution of Maths scores")

4.1.8 theme_void

Click to view code
ggplot(data = exam_data, aes(x = MATHS)) +
  geom_histogram(bins = 20, boundary = 100, color = "grey25",
                 fill = "grey90") +
  theme_void() +
  ggtitle("Distribution of Maths scores")

4.2 Working with ggtheme package

4.2.1 Economist theme

Click to view code
ggplot(data = exam_data, aes(x = MATHS)) +
  geom_histogram(bins = 20, boundary = 100, color = "grey25",
                 fill = "grey90") +
  ggtitle("Distribution of Maths scores") +
  theme_economist()

4.3 Working with hrbyhems package

4.3.1 hybrthmes (where lables are placed)

Click to view code
ggplot(data=exam_data, 
             aes(x = MATHS)) +
  geom_histogram(bins=20, 
                 boundary = 100,
                 color="grey25", 
                 fill="grey90") +
  ggtitle("Distribution of Maths scores") +
  theme_ipsum()

4.3.2 hybrthmes (add details)

Click to view code
ggplot(data=exam_data, 
             aes(x = MATHS)) +
  geom_histogram(bins=20, 
                 boundary = 100,
                 color="grey25", 
                 fill="grey90") +
  ggtitle("Distribution of Maths scores") +
  theme_ipsum(axis_title_size = 18,
              base_size = 15,
              grid = "Y")

5 Beyond Single Graph

5.1 p1

Click to view code
p1 <- ggplot(data = exam_data,
             aes(x = MATHS)) +
  geom_histogram(bins = 20,
                 boundary = 100,
                 color = "grey25",
                 fill = "grey90") +
  coord_cartesian(xlim = c(0,100)) +
  ggtitle("Distribution of Maths scores")

5.2 p2

Click to view code
p2 <- ggplot(data = exam_data,
             aes(x = MATHS)) +
  geom_histogram(bins = 20, boundary = 100,
                 color = "grey25", fill = "grey90") +
  coord_cartesian(xlim = c(0,100)) +
  ggtitle("Distribution of Maths scores")

5.3 p3

Click to view code
p3 <- ggplot(data = exam_data,
             aes(x = MATHS, y = ENGLISH)) +
  geom_point() +
  geom_smooth(method = lm, 
              size = .5) +
  coord_cartesian(xlim = c(0,100), ylim = c(0,100)) +
  ggtitle("English scores versus Maths scores for Primary 3")

5.4 show

Click to view code
p1

Click to view code
p2

Click to view code
p3

6 Creating Composite Graphics: pathwork mehods

6.1 Combining two ggplot2 graphs

Click to view code
p1 + p2

6.2 Combining three ggplot2 graphs

Click to view code
(p1/p2)|p3

6.3 Creating a composite figure with tag

Click to view code
(p1/p2)|p3 +
  plot_annotation(tag_levels = "I")

6.4 Creating figure with insert

Click to view code
p3 + inset_element(p2,
                   left = 0.02,
                   bottom = 0.7,
                   right = 0.5,
                   top = 1)

6.5 Creating acompostie figure by using patchwork and ggtheme

Click to view code
patchwork <- (p1/p2)|p3 
patchwork & theme_economist()