POL 051 F26 A04, A05, & A06 2026
Data carries weight in our society
Visualizing data is an effective way to convey information, convince, argue
Visualization can be used to tell The Truth™️ (or not)
RGraphs have an internal logic, or grammar that connects data to visuals
Data = variables in a dataset
Aesthetic = visual property of a graph (position, shape, color, etc.)
Geometry = representation of an aesthetic (point, line, text, etc.)
Take the data,
map it onto an aesthetic,
and visualize it with a geometry
ggplot(): our first function 😢don’t fret; You will not memorize these as they appear on screen
ggplot: specify the dataaes() to map variables to aestheticsImportant
Use the variable names exactly as they appear in the data, map them onto the exact function names in R
+aes()aes()labs()Notice that text is placed within quotation marks!
There are many more themes, here are a few
Tell ggplot() the data we want to plot
Map all variables onto aesthetics within aes()
Add layers like geom_point() and theme_bw() using +
What am I trying to show?
What kind of variables do I have?
What aesthetics and geometries do I need for this plot?
| id | age | degree | race | num_kids |
|---|---|---|---|---|
| 1 | 47 | Bachelor | White | 3 |
| 2 | 61 | High School | White | 0 |
| 3 | 72 | Bachelor | White | 2 |
| 4 | 43 | High School | White | 4 |
| 5 | 55 | Graduate | White | 2 |
| id | age | degree | race | num_kids |
|---|---|---|---|---|
| 1 | 47 | Bachelor | White | 3 |
| 2 | 61 | High School | White | 0 |
| 3 | 72 | Bachelor | White | 2 |
| 4 | 43 | High School | White | 4 |
| 5 | 55 | Graduate | White | 2 |
The scatterplot visualizes the relationship between two continuous variables
Shows every point in the data, reveals trends and outliers
| gdpPercap | lifeExp |
|---|---|
| 974.58 | 43.83 |
| 5937.03 | 76.42 |
| 6223.37 | 72.30 |
| 4797.23 | 42.73 |
| 12779.38 | 75.32 |
| Data | Aesthetic | Geometry |
|---|---|---|
| gdpPercap | x | geom_point() |
| lifeExp | y | geom_point() |
Plot is uninformative because continent is discrete (i.e., a category)
Make a plot of presidential election results using the elections_historic dataset
% of popular vote (x-axis, popular_pct) and % of electoral college vote (y-axis, ec_pct)
map the winner’s party to the color aesthetic, whether or not president served two terms to shape, and add labels to each point (use winner_label)
geom_text() to add text labels, make sure to put a label = in the aes() to tell how to label itWhat’s going on here?
The time series uses a line to show you how a variable (y-axis) moves over time (x-axis)
| year | avg_yrs |
|---|---|
| 1952 | 49.06 |
| 1957 | 51.51 |
| 1962 | 53.61 |
| 1967 | 55.68 |
| 1972 | 57.65 |
| Data | Aesthetic | Geometry |
|---|---|---|
| year | x | geom_line() |
| avg_yrs | y | geom_line() |
Notice the new geometry, geom_line()
Look at the economics dataset from the tidyverse package
Type and run economics to see the data
Type and run ?economics to read about the data
Make a time series of unemployment over time
Can you identify the recessions?
Sometimes we observe multiple units over time; how can we visualize these?
| country | continent | year | lifeExp | pop | gdpPercap |
|---|---|---|---|---|---|
| Ghana | Africa | 2007 | 60.022 | 22873338 | 1327.6089 |
| Ghana | Africa | 1992 | 57.501 | 16278738 | 925.0602 |
| China | Asia | 1997 | 70.426 | 1230075000 | 2289.2341 |
| United States | Americas | 1952 | 68.440 | 157553000 | 13990.4821 |
| Denmark | Europe | 1987 | 74.800 | 5127024 | 25116.1758 |
These are useful for comparing trends across units (countries, places, people, etc)
A histogram shows you how a continuous variable is distributed
| lifeExp |
|---|
| 43.83 |
| 76.42 |
| 72.30 |
| 42.73 |
| 75.32 |
| Data | Aesthetic | Geometry |
|---|---|---|
| lifeExp | x | geom_histogram() |
Notice the new geometry, geom_histogram(); and that a histogram only uses the x-axis!
In some countries, when you die it is assumed you want to donate your organs
To not donate, you have to opt out
In other countries, when you die it is assumed you do not want to donate your organs
To not donate, you have to opt in
| country | donors | opt |
|---|---|---|
| United Kingdom | 13.2 | In |
| Germany | 12.8 | In |
| Norway | 17.6 | Out |
| Spain | 32.5 | Out |
| Canada | 13.0 | In |
Using the organdata dataset in the package socviz:
Make a histogram of country’s organ donation rate (donors)
Then set the fill aesthetic to opt, whether donors have to opt in or opt out of donating. How does the graph change?
Barplots place a category (place, country, person, etc) on one axis and a quantity (amount, average, median, etc.) on another
Useful for making comparisons, highlighting differences
| marital | tv |
|---|---|
| No answer | 2.56 |
| Never married | 3.11 |
| Separated | 3.55 |
| Divorced | 3.09 |
| Widowed | 3.91 |
| Data | Aesthetic | Geometry |
|---|---|---|
| tv | x | geom_col() |
| marital | y | geom_col() |
Note
You could switch the x and y mapping around, but I think categories look better on the y-axis
Boxplots compare distributions of continuous variables across groups
Boxplots contain a lot of info 🥵:

| continent | lifeExp |
|---|---|
| Asia | 43.83 |
| Europe | 76.42 |
| Africa | 72.30 |
| Africa | 42.73 |
| Americas | 75.32 |
| Data | Aesthetic | Geometry |
|---|---|---|
| contient | y | geom_boxplot() |
| lifeExp | x | geom_boxplot() |
Note
You could switch the x and y mapping around, but I think categories look better on the y-axis
| Graph | aes() | geom_ | Purpose |
|---|---|---|---|
| Scatterplot | x = cause, y = effect | point() | Relationships |
| Time series | x = date, y = variable | line() | Trends |
| Histogram | x = cont. variable | histogram() | Distributions |
| Barplot | y = category, x = quantity | col() | Compare amounts |
| Boxplot | y = category, x = cont. variable | boxplot() | Compare distributions |
Know how and when to use which!
We’ve barely scratched the surface; there’s many more aesthetics, geometries, and layers in ggplot()
Here are some of my favorite ones
And some ideas for making graphs better
We can use panels to show movement of a variable across time, space, etc.
Note
Make sure the facetting variable is wrapped in vars()!
Take your aesthetics out of aes() and into geom() to make them static
scale_fill_brewer() for fill, scale_color_brewer for color
scale_fill_viridis_d for discrete variables, scale_fill_viridis_d for continuous
theme_spongeBob() from tvthemes package, many more online