| marital | tv |
|---|---|
| No answer | 2.56 |
| Never married | 3.11 |
| Separated | 3.55 |
| Divorced | 3.09 |
| Widowed | 3.91 |
POL51
University of California, Davis
October 21, 2026
Review Data Visualization
Wrangling and pipes
Subsetting data
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
Why would there be differences across TV consumption?
| 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() |
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
they show us more than the barplot which only gives us a quantity for each group
Boxplots contain a lot of info 🥵:

interpret
| 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() |
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.
What is this graph showing us?
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
Scatterplots?
geom_point(): two continuous variables (x and y)
Time series?
geom_line(): one time/date variable (x) + one continuous variable (y)
Histograms?
geom_histogram(): one continuous variable (x)
Barplots?
geom_col(): one categorical variable (x) + one continuous variable (y, already summarized)
Boxplots?
geom_boxplot(): one continuous variable (y), often split by one categorical variable (x)
…the process of transforming and mapping data from one “raw” data form into another format with the intent of making it more appropriate and valuable for a variety of downstream purposes such as analytics… Data analysts typically spend the majority of their time in the process of data wrangling compared to the actual analysis of the data. – Wikipedia
Most of your time working with data will be spent wrangling it into a usable form for analysis
You’ve seen these before…
Pipes link data to functions
They look like this %>%, or |>
Definitely use keyboard shortcuts

With pipes: 😍
Without pipes: 🤢
Both produce the same output, but pipes make code more legible
You can read the pipe as if it said “and then”…
Take the data object movies, AND THEN
filter so genre1, genre2, or genre3 equal HORROR, AND THEN
mutate so that…
filter()filter() subsets data objects based on rules

Lots of real-world applications: finding flights, addresses, IDs, etc.
Sometimes we want to focus on a specific subset of data: the South, Latin America, etc.
Useful to deal with common problems: outliers, missing data, strange responses
filter()To use filter(), we need to tell R which observations we want to include (or exclude) using rules
Rules filter data based on whether variables meet certain criteria
Rules rely on logical operators:
Equal to, not equal to, less than, more than, included in, etc.
Observations that meet the rule are returned; those that are not are dropped

| Operator | meaning |
|---|---|
| == | equal to |
| != | not equal to |
| > | greater than |
| < | less than |
| >= | greater than or equal to |
| <= | less than or equal to |
| & | AND (both conditions true) |
| | | OR (either condition is true) |
| %in% | IN (in the set of) |
filter()Say we have some data on 🍎
| name | color | pounds | sweet |
|---|---|---|---|
| Fuji | red | 2 | TRUE |
| Gala | green | 4 | TRUE |
| Macintosh | green | 8 | FALSE |
| Granny Smith | red | 3 | FALSE |
# A tibble: 4 × 4
name color pounds sweet
<chr> <chr> <dbl> <lgl>
1 Fuji red 2 TRUE
2 Gala green 4 TRUE
3 Macintosh green 8 FALSE
4 Granny Smith red 3 FALSE
Note
The output reports how many rows and columns our dataset has (4 rows x 4 columns)
# A tibble: 2 × 4
name color pounds sweet
<chr> <chr> <dbl> <lgl>
1 Gala green 4 TRUE
2 Macintosh green 8 FALSE
Notice words are in quotations!
Notice that the number of rows has decreased: 2 x 4
# A tibble: 1 × 4
name color pounds sweet
<chr> <chr> <dbl> <lgl>
1 Macintosh green 8 FALSE
Notice TRUE/FALSE are all-caps!
# A tibble: 2 × 4
name color pounds sweet
<chr> <chr> <dbl> <lgl>
1 Fuji red 2 TRUE
2 Granny Smith red 3 FALSE
The ! symbol negates: not equal to
# A tibble: 1 × 4
name color pounds sweet
<chr> <chr> <dbl> <lgl>
1 Gala green 4 TRUE
Notice: at least implies greater than or equal to
“Observations where either this is true OR that is true”
The & operator can be used to combine rules
Returns observations where both rules are true
“Apples that are red AND sweet or green AND sour”:
The %in% operator is super powerful
It returns observations that belong to a set
Make a list of countries and return observations that match any of them
Note
To make a “list” of items (a vector), use c()
# A tibble: 1 × 16
country gwcode leader gender year yr_office age edu mil_service combat
<chr> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <fct> <dbl> <dbl>
1 VNM 815 Thanh Th… M 1889 1 11 Seco… 0 0
# ℹ 6 more variables: rebel <dbl>, yrs_exp <dbl>, phys_health <dbl>,
# mental_health <dbl>, will_force <dbl>, will_force_sd <dbl>
Open an r script, open the code book to find the variable names, and filter to find the car model.
Next Using the mtcars dataset, identify:
Note
?mtcars to see the codebook.
mpg cyl disp hp drat wt qsec vs am gear carb
Ferrari Dino 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6