Hands-on Exercise 02

Author

Ong Chae Hui

1. Getting Started

1.1. Install and launch R packages

In this exercise, beside tidyverse, four R packages will be used. They are:

  • ggrepel: an R package provides geoms for ggplot2 to repel overlapping text labels.
  • ggthemes: an R package provides some extra themes, geoms, and scales for ‘ggplot2’.
  • hrbrthemes: an R package provides typography-centric themes and theme components for ggplot2.
  • patchwork: an R package for preparing composite figure created using ggplot2.
Code
pacman::p_load(tidyverse, ggrepel, patchwork, ggthemes, hrbrthemes)

1.2. Importing the data

It consists of year end examination grades of a cohort of primary 3 students from a local school. It is in csv file format.

There are a total of seven attributes in the exam_data tibble data frame. Four of them are categorical data type and the other three are in continuous data type.

  • The categorical attributes are: ID, CLASS, GENDER and RACE.
  • The continuous attributes are: MATHS, ENGLISH and SCIENCE.
Code
exam_data <- read_csv("data/Exam_data.csv")

2. Beyond ggplot2 Annotation: ggrepel

One of the challenge in plotting statistical graph is annotation, especially with large number of data points.

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

2.1. Working with ggrepel

ggrepel is an extension of ggplot2 package which provides geoms for ggplot2 to repel overlapping text as in the above example.

To do so, we simply replace geom_text() by geom_text_repel() and geom_label() by geom_label_repel.

Note: We will get the warning message: “ggrepel: 318 unlabeled data points (too many overlaps). Consider increasing max.overlaps” when we run the code snippet. However, in order to eliminate the warning message, we will need to set max.overlaps=Inf. This, however, makes the entire chart cluttered with labels (which seemed worse that the original chart above, without using ggrepel).

Alternatively, we can just set warning=FALSE to suppress the warning message and let ggrepel does its work of suggesting the ‘best’ number of labels.

To show the comparison, the left tab code snippet will present the warning message, while the right tab code snippet will show the chart is cluttered with labels when we set max.overlaps=Inf.

Code
ggplot(data=exam_data, 
       aes(x= MATHS, y=ENGLISH)) +
  geom_point() +
  geom_smooth(formula = y ~ x, method=lm, 
              linewidth=0.5) +  
  geom_label_repel(aes(label = ID), 
                   fontface = "bold") +
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100)) +
  ggtitle("English scores versus Maths scores for Primary 3")

Code
ggplot(data=exam_data, 
       aes(x= MATHS, y=ENGLISH)) +
  geom_point() +
  geom_smooth(formula = y ~ x, method=lm, 
              linewidth=0.5) +  
  geom_label_repel(aes(label = ID), 
                   fontface = "bold", max.overlaps=Inf) +
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100)) +
  ggtitle("English scores versus Maths scores for Primary 3")

3. Beyond ggplot2 Themes

ggplot2 comes with eight built-in themes, they are: theme_gray(), theme_bw(), theme_classic(), theme_dark(), theme_light(), theme_linedraw(), theme_minimal(), and theme_void().

Refer to this link to learn more about ggplot2 Themes

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") 

3.1. Working with ggtheme package

ggthemes provides ‘ggplot2’ themes that replicate the look of plots by Edward Tufte, Stephen Few, Fivethirtyeight, The Economist, ‘Stata’, ‘Excel’, and The Wall Street Journal, among others.

In the example below, The Economist theme is used.

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

It also provides some extra geoms and scales for ‘ggplot2’. Consult this vignette to learn more.

3.2. Working with hrbthemes package

hrbrthemes package provides a base theme that focuses on typographic elements, including where various labels are placed as well as the fonts that are used.

Code
#pacman::p_load(extrafont)
#font_import()
#loadfonts(device = "win")

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

The second goal centers around productivity for a production workflow. In fact, this “production workflow” is the context for where the elements of hrbrthemes should be used. Consult this vignette to learn more.

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")

From the example above, we learnt that

  • axis_title_size argument is used to increase the font size of the axis title to 18
  • base_size argument is used to increase the default axis label to 15, and
  • grid argument is used to remove the x-axis grid lines.

4. Beyond Single Graph

It is not unusual that multiple graphs are required to tell a compelling visual story. There are several ggplot2 extensions provide functions to compose figure with multiple graphs. In this section, we will create composite plot by combining multiple graphs. First, let us create three statistical graphics by using the code chunk below.

Firstly, the histogram for MATHS scores

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")

p1

Next, the histogram for ENGLISH scores

Code
p2 <- ggplot(data=exam_data,
             aes(x = ENGLISH)) +
  geom_histogram(bins=20, 
                 boundary = 100,
                 color="grey25", 
                 fill="grey90") +
  coord_cartesian(xlim=c(0,100)) +
  ggtitle("Distribution of English scores")

p2

Lastly, we will draw a scatterplot for English score versus Maths score by as shown below

Code
p3 <- ggplot(data=exam_data,
             aes(x= MATHS, 
                 y=ENGLISH)) +
  geom_point() +
  geom_smooth(formula = y ~ x, method=lm, 
              linewidth=0.5) +  
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100)) +
  ggtitle("English scores versus Maths scores for Primary 3")

p3

4.1. Creating Composite Graphics: pathwork methods

There are several ggplot2 extension’s functions support the needs to prepare composite figure by combining several graphs such as grid.arrange() of gridExtra package and plot_grid() of cowplot package.

In this section, We will use a ggplot2 extension called patchwork which is specially designed for combining separate ggplot2 graphs into a single figure.

Patchwork package has a very simple syntax where we can create layouts super easily. Here’s the general syntax that combines:

  • Two-Column Layout using the Plus Sign +.
  • Parenthesis () to create a subplot group.
  • Two-Row Layout using the Division Sign /

4.2. Combining 2 ggplot2 graphs

Figure below shows a composite of two histograms created using patchwork. Note how simple the syntax used to create the plot!

Code
p1 + p2

4.3. Combining 3 ggplot2 graphs

We can plot more complex composite by using appropriate operators. For example, the composite figure below is plotted by using:

  • “|” operator to stack two ggplot2 graphs,
  • “/” operator to place the plots beside each other,
  • “()” operator the define the sequence of the plotting.
Code
(p1 / p2) | p3

To learn more about, refer to Plot Assembly.

4.4. Creating a composite figure with tag

In order to identify subplots in text, patchwork also provides auto-tagging capabilities as shown in the figure below. For tagging,

  • ‘1’ for Arabic numerals,
  • ‘A’ for uppercase Latin letters,
  • ‘a’ for lowercase Latin letters,
  • ‘I’ for uppercase Roman numerals, and
  • ‘i’ for lowercase Roman numerals.
Code
((p1 / p2) | p3) + 
  plot_annotation(tag_levels = 'I')

4.5. Creating figure with insert

Beside providing functions to place plots next to each other based on the provided layout. With inset_element() of patchwork, we can place one or several plots or graphic elements freely on top or below another plot.

Code
p12 <- p1|p2
p3 + inset_element(p2, 
                   left = 0.02, 
                   bottom = 0.7, 
                   right = 0.5, 
                   top = 1)

4.6. Creating a composite figure by using patchwork and ggtheme

Figure below is created by combining patchwork and theme_economist() of ggthemes package discussed earlier.

Code
patchwork <- (p1 / p2) | p3
patchwork & theme_economist()