2026-12-05
## Installing packages into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.5 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.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
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
library(knitr)
# Import the TidyTuesday datasets
daily_accidents <- read_csv(
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-04-22/daily_accidents.csv"
)## Rows: 9132 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (1): fatalities_count
## date (1): date
##
## ℹ 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.
daily_accidents_420 <- read_csv(
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-04-22/daily_accidents_420.csv"
)## Rows: 9170 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (1): fatalities_count
## lgl (1): e420
## date (1): date
##
## ℹ 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.
## # A tibble: 6 × 2
## date fatalities_count
## <date> <dbl>
## 1 1992-01-01 144
## 2 1992-01-02 111
## 3 1992-01-07 85
## 4 1992-01-12 127
## 5 1992-01-03 182
## 6 1992-01-17 130
## spc_tbl_ [9,132 × 2] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ date : Date[1:9132], format: "1992-01-01" "1992-01-02" ...
## $ fatalities_count: num [1:9132] 144 111 85 127 182 130 119 120 106 124 ...
## - attr(*, "spec")=
## .. cols(
## .. date = col_date(format = ""),
## .. fatalities_count = col_double()
## .. )
## - attr(*, "problems")=<pointer: 0x634e88692040>
## date fatalities_count
## Min. :1992-01-01 Min. : 47.0
## 1st Qu.:1998-04-01 1st Qu.:121.0
## Median :2004-07-01 Median :142.0
## Mean :2004-07-01 Mean :145.1
## 3rd Qu.:2010-10-01 3rd Qu.:166.0
## Max. :2016-12-31 Max. :299.0
## # A tibble: 6 × 3
## date e420 fatalities_count
## <date> <lgl> <dbl>
## 1 1992-01-01 FALSE 144
## 2 1992-01-02 FALSE 111
## 3 1992-01-07 FALSE 85
## 4 1992-01-12 FALSE 127
## 5 1992-01-03 FALSE 182
## 6 1992-01-17 FALSE 130
## spc_tbl_ [9,170 × 3] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ date : Date[1:9170], format: "1992-01-01" "1992-01-02" ...
## $ e420 : logi [1:9170] FALSE FALSE FALSE FALSE FALSE FALSE ...
## $ fatalities_count: num [1:9170] 144 111 85 127 182 130 119 120 106 124 ...
## - attr(*, "spec")=
## .. cols(
## .. date = col_date(format = ""),
## .. e420 = col_logical(),
## .. fatalities_count = col_double()
## .. )
## - attr(*, "problems")=<pointer: 0x634e8a66bf00>
## date e420 fatalities_count
## Min. :1992-01-01 Mode :logical Min. : 1.0
## 1st Qu.:1998-04-01 FALSE:9132 1st Qu.:120.0
## Median :2004-07-01 TRUE :25 Median :142.0
## Mean :2004-07-01 NAs :13 Mean :144.5
## 3rd Qu.:2010-09-30 3rd Qu.:166.0
## Max. :2016-12-31 Max. :299.0
## [1] "date" "fatalities_count"
## # A tibble: 6 × 2
## date fatalities_count
## <date> <dbl>
## 1 1992-01-01 144
## 2 1992-01-02 111
## 3 1992-01-07 85
## 4 1992-01-12 127
## 5 1992-01-03 182
## 6 1992-01-17 130
top_three_dates <- date_rankings %>%
arrange(desc(total_fatalities)) %>%
slice_head(n = 3)
ggplot(top_three_dates,
aes(x = reorder(calendar_day, average_fatalities),
y = average_fatalities,
fill = calendar_day)) +
geom_col(show.legend = FALSE, width = 0.7) +
geom_text(
aes(label = round(average_fatalities, 1)),
hjust = -0.2,
size = 5
) +
scale_fill_manual(
values = c(
"07-04" = "firebrick",
"07-03" = "steelblue",
"10-31" = "orange"
)
) +
coord_flip() +
labs(
title = "The Three Most Dangerous Days of the Year",
subtitle = "Average U.S. traffic fatalities, 1992–2016",
x = "Date",
y = "Average Number of Fatalities"
) +
scale_x_discrete(
labels = c(
"07-03" = "July 3",
"07-04" = "July 4",
"10-31" = "October 31 (Halloween)"
)
) +
theme_minimal(base_size = 13)## # A tibble: 10 × 6
## calendar_day month day average_fatalities total_fatalities years_recorded
## <chr> <dbl> <int> <dbl> <dbl> <int>
## 1 07-04 7 4 176 4400 25
## 2 07-03 7 3 174 4350 25
## 3 10-31 10 31 169. 4216 25
## 4 08-06 8 6 169. 4213 25
## 5 10-11 10 11 168. 4188 25
## 6 11-01 11 1 167. 4185 25
## 7 08-08 8 8 167. 4168 25
## 8 10-09 10 9 166. 4143 25
## 9 08-02 8 2 166. 4139 25
## 10 10-16 10 16 165. 4119 25
top_three_dates <- date_rankings %>%
slice_max(
average_fatalities,
n = 3,
with_ties = FALSE
)
print(top_three_dates)## # A tibble: 3 × 6
## calendar_day month day average_fatalities total_fatalities years_recorded
## <chr> <dbl> <int> <dbl> <dbl> <int>
## 1 07-04 7 4 176 4400 25
## 2 07-03 7 3 174 4350 25
## 3 10-31 10 31 169. 4216 25
holiday_dates <- date_averages %>%
filter(calendar_day %in% c(
"01-01",
"04-20",
"07-03",
"07-04",
"10-31",
"12-24",
"12-25"
)) %>%
mutate(
holiday = recode(
calendar_day,
"01-01" = "New Year's Day",
"04-20" = "April 20",
"07-03" = "July 3",
"07-04" = "July 4",
"10-31" = "Halloween",
"12-24" = "Christmas Eve",
"12-25" = "Christmas"
)
)
ggplot(
holiday_dates,
aes(
x = reorder(holiday, average_fatalities),
y = average_fatalities
)
) +
geom_col(fill = "steelblue") +
geom_text(
aes(label = round(average_fatalities, 1)),
hjust = -0.2
) +
coord_flip() +
labs(
title = "Fatalities on Selected Holidays and Celebrations",
subtitle = "Average daily fatalities, 1992–2016",
x = "Holiday or Celebration",
y = "Average Number of Fatalities"
) +
theme_minimal()april_20_comparison <- accidents_clean %>%
mutate(
date_group = if_else(
month == 4 & day == 20,
"April 20",
"All Other Dates"
)
) %>%
group_by(date_group) %>%
summarise(
average_fatalities = mean(fatalities_count, na.rm = TRUE),
total_fatalities = sum(fatalities_count, na.rm = TRUE),
number_of_days = n(),
.groups = "drop"
)## # A tibble: 2 × 4
## date_group average_fatalities total_fatalities number_of_days
## <chr> <dbl> <dbl> <int>
## 1 All Other Dates 145. 1321351 9107
## 2 April 20 139. 3483 25
ggplot(april_20_comparison,
aes(x = date_group,
y = average_fatalities,
fill = date_group)) +
geom_col(width = 0.6) +
geom_text(
aes(label = round(average_fatalities, 2)),
vjust = -0.5,
size = 5
) +
scale_fill_manual(
values = c(
"April 20" = "firebrick",
"All Other Dates" = "steelblue"
)
) +
labs(
title = "April 20 Compared to All Other Days",
subtitle = "Average U.S. traffic fatalities, 1992–2016",
x = "Date Category",
y = "Average Number of Fatalities"
) +
theme_minimal()