Module 3-1-Principle - Data Visualization with ggplot2 in R

Author

Maurice Morgan

Published

February 25, 2026

0.1 Expected Learning Outcomes

After taking this workshop, participants should be able to do following:

  1. Explain the concept of the grammar of graphics when visualizing data with the ggplot2 package.

  2. Be familiar with various types of charts.

  3. Visualize data in counts and proportions.

  4. Select appropriate charts based on strategic considerations (e.g., the characteristics of the data and audience).

  5. Create a chart that involves one or two variables with either categorical or continuous data.

  6. Create a chart by adding a categorical moderator (3rd variable) to the chart involving two or three variables.

  7. Create correlation charts.

  8. Read charts and generate insights.

  9. Describe three popular packages that allow one to visualize data.

  10. Explain the concept of the grammar of graphics when visualizing data with the ggplot2 package.

0.2 Loading Packages

#install.packages('ggrepel')
#install.packages('ggthemes')
#install.packages('scales')
#install.packages('plotly')
#install.packages('lattice')
#install.packages('GGally')
#install.packages("dplyr")
#install.packages("tidyverse")
#install.packages('ggtext')
#install.packages("glue")
#install.packages("tibble")
library(ggplot2) #visualization
library(ggrepel) #labels for data
library(ggthemes) #collections of themes
library(scales) # scale
library(plotly) # interactive chart
library(GGally) # correlation
library(dplyr) # data transformation
library(tidyverse) # mega package containing 8 packages
library(ggtext) # for text visualization
library(glue) # combining multiple component
library(gapminder)
Error in `library()`:
! there is no package called 'gapminder'

1 1. Understand mtcars data

1.1 1.1 Using Help

?mtcars
help(mtcars)
data()

A data frame with 32 observations on 11 (numeric) variables.

[, 1] mpg Miles/(US) gallon
[, 2] cyl Number of cylinders
[, 3] disp Displacement (cu.in.)
[, 4] hp Gross horsepower
[, 5] drat Rear axle ratio
[, 6] wt Weight (1000 lbs)
[, 7] qsec 1/4 mile time
[, 8] vs Engine (0 = V-shaped, 1 = straight)
[, 9] am Transmission (0 = automatic, 1 = manual)
[,10] gear Number of forward gears
[,11] carb Number of carburetors

1.2 1.2 Reading data and converting to a tibble (cars)

head(mtcars)
class(mtcars)

# short cut for pipe operator: ctl + shift + M
cars <- mtcars |> 
  rownames_to_column() |> 
  as_tibble()
  rename(model = rowname) |>
  print(n = 20, width = Inf
Error in parse(text = input): <text>:10:0: unexpected end of input
8:   rename(model = rowname) |>
9:   print(n = 20, width = Inf
  ^

1.3 1.3 Simple Descriptive Statistics

Note

shortcut for code chunk: ctl + alt + i

summary(cars)
     speed           dist       
 Min.   : 4.0   Min.   :  2.00  
 1st Qu.:12.0   1st Qu.: 26.00  
 Median :15.0   Median : 36.00  
 Mean   :15.4   Mean   : 42.98  
 3rd Qu.:19.0   3rd Qu.: 56.00  
 Max.   :25.0   Max.   :120.00  
glimpse(cars)
Rows: 50
Columns: 2
$ speed <dbl> 4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13…
$ dist  <dbl> 2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34…
skimr::skim(cars)
Data summary
Name cars
Number of rows 50
Number of columns 2
_______________________
Column type frequency:
numeric 2
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
speed 0 1 15.40 5.29 4 12 15 19 25 ▂▅▇▇▃
dist 0 1 42.98 25.77 2 26 36 56 120 ▅▇▅▂▁

2 2. Basic Plotting Methods in Base R

# Using a built in plotting function
hist(cars$disp, breaks = 10)
Error in `hist.default()`:
! 'x' must be numeric

3 Lattice package

#install.packages("lattice")
library(lattice)
xyplot(mpg - wt, cars)
Error:
! object 'wt' not found
histogram(cars$disp, breaks = 10)
Error in `UseMethod()`:
! no applicable method for 'histogram' applied to an object of class "NULL"

4 ggplot2

  • We will use ggplot2 – the best tool in the market for data visualization – from now on.

4.1 4.1 Elaborate Examples

4.1.1 4.1.1 x & y are both continuous with moderator & labeller()

mtcars |> 
  count(cyl)

4.1.2 Plotting

mtcars |> 
  mutate(cyl = factor(cyl))
  ggplot(aes(x = mpg, y = disp))
Error in `fortify()`:
! `data` must be a <data.frame>, or an object coercible by `fortify()`,
  or a valid <data.frame>-like object coercible by `as.data.frame()`, not a
  <ggplot2::mapping> object.
ℹ Did you accidentally pass `aes()` to the `data` argument?
  geom_point()
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 
  geom_smooth(method = "lm", se = FALSE) +
  facet_wrap(~ cyl, ncol = 1) +
  theme_bw()
Error:
! Cannot add <ggproto> objects together.
ℹ Did you forget to add this object to a <ggplot> object?