Hands-on Exercise 1: A Layered Grammar of Graphics: ggplot2 methods

Author

Zou Jiaxun

Published

April 13, 2024

1 Learning Outcome

In this chapter, we will learn the basic principles and essential components of ggplot2. At the same time, we will gain hands-on experience on using these components to plot statistical graphics based on the principle of Layered Grammar of Graphics. By then end of this chapter we will be able to apply the essential graphical elements provided by ggplot2 to create elegant and yet functional statistical graphic

2 Getting Started

The code chunk below uses p-load() of pacman package to check if tidyverse packages are installed in the computer. If they are, then they will be launched into R.

Click to view code
pacman::p_load(tidyverse)
  • The code chunk below imports exam_data.csv into R environment by using read_csv()function of readr package.

  • readr is one of the tidyverse package.

Click to view code
exam_data <- read_csv("data/Exam_data.csv")
  • Year end examination grades of a cohort of primary 3 students from a local school.

  • There are a total of seven attributes. 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.

3 Introducing ggplot

Click to view code
ggplot(data = exam_data,
       aes(x = RACE)) + 
  geom_bar()

4 Plotting kinds of chart by geometric objects

4.1 Dotplot(with y-axis)

Click to view code
ggplot(data = exam_data,
       aes(x = ENGLISH)) +
  geom_dotplot(dotsize = 0.5)

4.2 Dotplot(turn-off y-axis)

Click to view code
ggplot(data = exam_data,
       aes(x = ENGLISH)) +
  geom_dotplot(binwidth = 2.5,
               dotsize = 0.5) +
  scale_y_continuous(NULL,
                     breaks = NULL)

4.3 Histogram (default bin is 30)

Click to view code
ggplot(data = exam_data,
       aes(x = ENGLISH)) +
  geom_histogram()

4.4 Histogram (changeing geom())

Click to view code
ggplot(data = exam_data,
       aes(x = ENGLISH)) +
  geom_histogram(bins = 20,
                 color = "black",
                 fill = "light blue")

4.5 Histogram (changeing aes())

Click to view code
ggplot(data = exam_data,
       aes(x = ENGLISH,
           fill = GENDER)) +
  geom_histogram(bins = 20,
                 color = "grey30")

4.6 Density

Click to view code
ggplot(data = exam_data,
       aes(x = ENGLISH,
           color = CLASS)) +
  geom_density()

4.7 Boxplot (with notch)

Click to view code
ggplot(data = exam_data,
       aes(y = ENGLISH,
           x = GENDER)) +
  geom_boxplot(notch = TRUE)

4.8 Violin (Comparing multiple data distribution)

Click to view code
ggplot(data = exam_data,
       aes(y = ENGLISH,
           x = GENDER)) +
  geom_violin()

4.9 Point

Click to view code
ggplot(data = exam_data,
       aes(x = ENGLISH,
           y = MATHS)) +
  geom_point()

4.10 Combine (“jitter” refers to a technique used to add small random bariation to data points)

Click to view code
ggplot(data = exam_data,
       aes(y = ENGLISH,
           x = GENDER)) +
  geom_boxplot() +
  geom_point(position = "jitter",
             size = 0.5)

5 Stat

5.1 Stat (Before)

Click to view code
ggplot(data = exam_data,
       aes(y = ENGLISH, x = GENDER)) +
  geom_boxplot()

5.2 Stat (After)

Click to view code
ggplot(data = exam_data,
       aes(y = ENGLISH,
           x = GENDER)) +
  geom_boxplot() +
  stat_summary(geom = "point",
               fun.y = "mean",
               colour = "red",
               size = 4)

5.3 Add curve on a scatterplot

Click to view code
ggplot(data = exam_data,
       aes(y = ENGLISH, x = MATHS)) +
  geom_point() +
  geom_smooth(size = 0.5)

5.4 Facet_wrap

Click to view code
ggplot(data = exam_data,
       aes(x = MATHS)) +
  geom_histogram(bins = 20) +
  facet_wrap(~ CLASS)

5.5 Facet_grid

Click to view code
ggplot(data = exam_data,
       aes(x = MATHS)) +
  geom_histogram(bins = 20) +
  facet_grid(~ CLASS)

6 Working with coordinate

6.1 (before)

Click to view code
ggplot(data=exam_data, 
       aes(x=RACE)) +
  geom_bar()

6.2 Working with coordinate (after)

Click to view code
ggplot(data=exam_data, 
       aes(x=RACE)) +
  geom_bar() +
  coord_flip()

6.3 Changing the y- and x-axis range (before)

Click to view code
ggplot(data = exam_data,
       aes(x = MATHS, y = ENGLISH)) +
  geom_point() +
  geom_smooth(method = lm, size=0.5)

6.4 Changing the y- and x-axis range (after)

Click to view code
ggplot(data = exam_data,
       aes(x = MATHS, y = ENGLISH)) +
  geom_point() +
  geom_smooth(method = lm, size=0.5) +
  coord_cartesian(xlim = c(0,100),
                  ylim = c(0,100))

7 Theme

Click to view code
ggplot(data = exam_data,
       aes(x = RACE)) +
  geom_bar() +
  coord_flip() +
  theme_gray()

7.1 Classic

Click to view code
ggplot(data = exam_data,
       aes(x = RACE)) +
  geom_bar() +
  coord_flip() +
  theme_classic()

7.2 Minimal

Click to view code
ggplot(data = exam_data,
       aes(x = RACE)) +
  geom_bar() +
  coord_flip() +
  theme_minimal()