── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
AE 10: More Practice
Application exercise
Durham Climate Data
We will use the tidyverse
library today.
As a refresher, is the code from AE-08:
Read in data:
durham_climate <- read_csv("data/durham-climate.csv")
Rows: 12 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): month
dbl (3): avg_high_f, avg_low_f, precipitation_in
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Relevel factors:
durham_climate <- durham_climate |>
mutate(month = fct_relevel(month, c("January", "February", "March",
"April", "May", "June", "July", "August",
"September", "October", "November", "December")))
Plot:
ggplot(
durham_climate,
aes(x = month, y = avg_high_f, group = 1)
) +
geom_line() +
geom_point(
shape = "circle filled", size = 2,
color = "black", fill = "white", stroke = 1
) +
labs(
x = "Month",
y = "Average high temperature (F)",
title = "Durham climate"
)
Goal : Beautify the plot
durham_climate <- durham_climate |>
mutate(month = fct_relevel(month, c("January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December")))
durham_climate <- durham_climate |>
mutate( season = case_when(
month %in% c("December", "January", "February") ~ "Winter",
month %in% c("March", "April", "May") ~ "Spring",
month %in% c("June", "July", "August") ~ "Summer",
month %in% c("September", "October", "November") ~ "Fall",)
) |>
mutate(season = fct_relevel(season, "Winter", "Spring", "Summer", "Fall"))
durham_climate |> ggplot(aes(x = month, y = avg_high_f,
group = 1, fill = season)) +
geom_line() +
geom_point(shape = "circle filled", size = 3, stroke = 1) +
scale_fill_manual(
values = c(
"Winter" = "lightskyblue1",
"Spring" = "chartreuse3",
"Summer" = "gold2",
"Fall" = "lightsalmon4")) +
labs(
x = "Month",
y = "Average high temperature (F)",
title = "Durham climate",
fill = "Season") +
theme_minimal() +
theme(legend.position = "top")
Goal : Pivot + Multiple Lines!
Add your pivot code here:
durham_climate <- durham_climate |>
pivot_longer(cols = c(avg_high_f, avg_low_f),
names_to = "temp_type",
values_to = "temp")
Check out the plot!
durham_climate |>
ggplot(aes(x = month, y = temp, group = temp_type, color = temp_type, fill = season)) +
geom_line() +
geom_point(shape = "circle filled", size = 3, stroke = 1) +
scale_fill_manual(
values = c(
"Winter" = "lightskyblue1",
"Spring" = "chartreuse3",
"Summer" = "gold2",
"Fall" = "lightsalmon4"
)
) +
scale_color_manual(
values = c(
"avg_high_f" = "gray20",
"avg_low_f" = "gray70"
)
) +
labs(
x = "Month",
y = "Average temperature (F)",
title = "Durham climate",
fill = "Season",
color = "Type"
) +
theme_minimal() +
theme(legend.position = "top")