Week 8
Introduction to ggplot2

Soci—269

Sakeef M. Karim
Amherst College

AN INTRODUCTION TO QUANTITATIVE SOCIOLOGY—CULTURE & POWER

Visualizing Data in
October 20th

Reminders

Coding Assignment in

Coding Assignment Deadline

Your first coding assignment is due by 8:00 PM on Tuesday, November 4th.

Reminders

Coding Assignment in

Assignment instructions are available online.

Using ggplot2

Getting Started

Launch RStudio and run the following code:

week8 <- c("systemfonts", "tidyverse", "palmerpenguins", "gapminder",
           "demography",  "ggthemes", "hrbrthemes", "see",
           "paletteer", "gglgbtq", "colorspace", "lemon", "summarytools",
           "skimr", "lattice", "tinyplot", "ggtext", "ggdist", "ggraph")

install.packages(week8)

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/soci-269-f25/raw/refs/heads/main/data/week%208/week8.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—
October 22nd

An Invitation

The Cummings Lecture

Title

Something Between Us
The Everyday Walls of American Life, and How to Take Them Down

Description

Click to Expand or Close

Scroll to access the entire description.

In 2016, Anand Pandian was alarmed by Donald Trump’s harsh attacks on immigrants to the United States, the appeal of that politics of anger and fear. In the years that followed, he crisscrossed the country seeking out fellow Americans with markedly different social and political commitments, trying to understand the forces that have hardened our suspicions of others. The result is Something Between Us, a groundbreaking and ultimately hopeful exploration of the ruptures in our social fabric, and courageous efforts to rebuild a collective life beyond them. Through vivid encounters with Americans of many kinds, Pandian shares tools to think beyond the twists and turns of our bracing present.

Yet Another Reminder

Coding Assignment in

Coding Assignment Deadline

Your first coding assignment is due by 8:00 PM on Tuesday, November 4th.

Yet Another Reminder

Coding Assignment in

Assignment instructions are available online.

Today’s Preliminaries

The Data

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

load(url("https://github.com/sakeefkarim/soci-269-f25/raw/refs/heads/main/data/week%208/week8.RData"))

Today’s Script File

You can access today’s script file here.

Download Some Fonts

Download

Here’s how to install fonts via Windows and Mac operating systems.

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!

Some “Homework”

Access Census APIs

You’ll need to obtain valid API keys to use the
tidycensus and cancensus libraries next week.

  • Instructions for obtaining a CensusMapper API key (to access cancensus data)—and storing it on your system—can be found here.

  • Instructions for obtaining an API key from the US Census Bureau—and storing it on your system—can be found here.

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, Mine Çetinkaya-Rundel, and Garrett Grolemund. 2023. R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. 2nd edition. Sebastopol, CA: O’Reilly.
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.