tompkins <- read_csv("data/tompkins-home-sales.csv") |>
mutate(decade_built = (year_built %/% 10) * 10) |>
mutate(
decade_built_cat = case_when(
decade_built <= 1940 ~ "1940 or before",
decade_built >= 1990 ~ "1990 or after",
.default = as.character(decade_built)
)
)
mean_price_decade <- tompkins |>
group_by(decade_built_cat) |>
summarize(mean_price = mean(price))Deep dive: layers (II)
Lecture 4
Announcements
Announcements
- Waitlist update
- 15 pins distributed so far
- INFO 3312: 1 seats available and 17 on the waitlist (8 IS majors)
- INFO 5312: 0 seats available and 2 on the waitlist
- Homework 01 due yesterday
- Homework 02
- Post questions on the discussion forum
Learning objectives
- Identify common geom types for single and multiple variable charts
- Generate charts using the same variables and different geoms
- Utilize position adjustments
- Evaluate the effectiveness of geom type choice for specific combinations of variables
Setup
From last time
Geoms
Geoms
Geometric objects, or geoms for short, perform the actual rendering of the layer, controlling the type of plot that you create
You can think of them as “the geometric shape used to represent the data”
One variable
One variable
-
Discrete:
-
geom_bar(): display distribution of discrete variable.
-
-
Continuous
geom_histogram(): bin and count continuous variable, display with barsgeom_density(): smoothed density estimategeom_dotplot(): stack individual points into a dot plotgeom_freqpoly(): bin and count continuous variable, display with lines
Comparing across groups
Application exercise
ae-03
Create and compare different types of bar charts.
10:00
Two variables
Two variables - both continuous
geom_point(): scatterplotgeom_quantile(): smoothed quantile regressiongeom_rug(): marginal rug plotsgeom_smooth(): smoothed line of best fitgeom_text(): text labels
Two variables - show density
geom_bin2d(): bin into rectangles and countgeom_density2d(): smoothed 2d density estimategeom_hex(): bin into hexagons and count
geom_hex()
Not so helpful for 38 observations:
tompkins |>
filter(decade_built == 1940) |>
ggplot(aes(x = area, y = price)) +
geom_hex()geom_hex()
More helpful for 1270 observations:
geom_hex()
Even more helpful for 53940 observations:
geom_hex()
(Maybe) even more helpful on the log scale:
Two variables
- At least one discrete
-
geom_count(): count number of point at distinct locations -
geom_jitter(): randomly jitter overlapping points
-
- One continuous, one discrete
-
geom_col(): a bar chart of pre-computed summaries -
geom_boxplot(): boxplots -
geom_violin(): show density of values in each group
-
geom_jitter()
How are the following three plots different?
geom_jitter() and set.seed()
Two variables
- One time, one continuous
-
geom_area(): area plot -
geom_line(): line plot -
geom_step(): step plot
-
- Display uncertainty:
-
geom_crossbar(): vertical bar with center -
geom_errorbar(): error bars -
geom_linerange(): vertical line -
geom_pointrange(): vertical line with center
-
- Spatial
-
geom_sf(): for map data
-
Average price per year built
# A tibble: 23 × 4
decade_built n mean_price sd_price
<dbl> <int> <dbl> <dbl>
1 1800 2 262500 95459.
2 1810 2 435000 233345.
3 1820 6 382083. 161852.
4 1830 5 330400 80114.
5 1840 8 510700 196711.
6 1850 22 258136. 138885.
7 1860 44 271182. 124217.
8 1870 29 381904. 221011.
9 1880 41 327278. 221005.
10 1890 37 364171. 192543.
# ℹ 13 more rows
Not geom_point()
geom_line()
geom_area()
geom_step()
Application exercise
ae-03
Create and compare different types of charts for comparing a categorical and continuous variable.
10:00
Let’s clean things up a bit!
ggplot(tompkins, aes(x = area, y = price)) +
geom_point(alpha = 0.2, size = 2, color = "#B31B1B") +
scale_x_continuous(labels = label_comma()) +
scale_y_continuous(labels = label_currency(scale_cut = cut_short_scale())) +
labs(
x = "Area (square feet)",
y = "Sale price (USD)",
title = "Sale prices of homes in Tompkins County, NY",
subtitle = "2022-24",
caption = "Source: Redfin.com"
)Wrap up
Recap
- {ggplot2} uses
geom_*()functions to define types of plots - Select appropriate
geom_*()functions based on the number and types of variables you wish to visualize - Consider the number of observations to determine an appropriate chart type and/or adjustments to the chart
Acknowledgements
- Slides derived in part from STA 313: Advanced Data Visualization
















