Week 4
Introduction to ggplot2

SOCI 269

Sakeef M. Karim
Amherst College

AN INTRODUCTION TO QUANTITATIVE SOCIOLOGY—CULTURE AND POWER

Visualizing Data in
February 18th

Some Updates

Our Syllabus Has Been Updated

Once again, the syllabus for this class
can be accessed here.

Some Updates

Coding Assignment Deadline

Your coding assignments are now due by 8:00 PM on Monday, March 10th.

Some Updates

Assignment instructions will be available
online by next Tuesday.

Some Updates

Midterm Assignment Deadline

Your midterm assignments are now due by 8:00 PM on Friday, April 4th.

Using ggplot2

Getting Started

Launch RStudio and run the following code:

week4 <- c("ggdist", "ggtext", "ggridges", "ggrepel", 
           "lattice", "skimr", "summarytools", "tinyplot", 
           "tidyplot", "ggraph", "lemon", "colorspace",
           "gglgbtq", "paletteer", "see", "hrbrthemes", "pacman",
           "ggthemes", "forecast","gapminder",  "palmerpenguins", "tidyverse",
           "systemfonts")

install.packages(week4)

Give me a when all the packages have been installed.

Getting Started

You can access data for today’s session by
executing the following code:

load(url("https://github.com/sakeefkarim/intro_quantitative_sociology/raw/refs/heads/main/data/week%204/week4.RData"))

Getting Started

Today, we’re going to be working with the following script file:

A Quick Example

Comparing Histograms

hist(penguins$bill_depth_mm)

histogram(~bill_depth_mm, data = penguins)

ggplot(data = penguins, mapping = aes(x = bill_depth_mm)) + 
geom_histogram()

The Grammar of Graphics

What Is a Graphic?

Wilkinson (2005) created the grammar of graphics to describe the fundamental features that underlie all statistical graphics. The grammar of graphics is an answer to the question of what is a statistical graphic? ggplot2 (Wickham 2009) builds on Wilkinson’s grammar by focusing on the primacy of layers and adapting it for use in . In brief, the grammar tells us that a graphic maps the data to the aesthetic attributes (colour, shape, size) of geometric objects (points, lines, bars). The plot may also include statistical transformations of the data and information about the plot’s coordinate system. Facetting can be used to plot for different subsets of the data. The combination of these independent components are what make up a graphic.

(Wickham, Navarro, and Pedersen 2025, EMPHASIS ADDED)

Building Blocks

Step 1:
Data, Aesthetics, Layers

Our Data

Let’s Move to RStudio

Note

The rest of today’s session will take place in RStudio!

Visualizing Data in II—
February 20th

Updated Script File

Here’s an updated version of week4.R:

Download Some Fonts

Download

Steps 2 & 3:
Scales, Coordinates, Facets; Themes, Labels and Guides

A Quick Refresher

Let’s Launch RStudio

Note

The rest of today’s session will take place in RStudio!

Enjoy the Weekend

References

Wickham, Hadley. 2009. “A Layered Grammar of Graphics.” Journal of Computational and Graphical Statistics 19(1):3–28. doi: 10.1198/jcgs.2009.07098.
Wickham, Hadley, Danielle Navarro, and Thomas Lin Pedersen. 2025. ggplot2: Elegant Graphics for Data Analysis. 3rd Edition. New York: Springer.
Wilkinson, Leland. 2005. The Grammar of Graphics. Springer-Verlag.