require(pacman)
p_load(tidytuesdayR, tidyverse, trashpanda, magick, sf, scales)
options(scipen = 999)US Agriculture Tariffs
Load Packages
Load Data
tuesdata <- tidytuesdayR::tt_load('2026-04-28')
agreements <- tuesdata$agreements
quantity_codes <- tuesdata$quantity_codes
tariff_agricultural <- tuesdata$tariff_agricultural
tariff_codes <- tuesdata$tariff_codesPlot
df <- tariff_agricultural |>
mutate(days = end_effective_date - begin_effective_date)
# Annual snapshot dates
snapshot_dates <- tibble(
year = 1989:2026,
snapshot_date = as.Date(paste0(year, "-07-01"))
)
# Find the tariff rate applicable on July 1 of each year
tariff_annual <- tariff_agricultural |>
filter(
str_to_lower(agreement) == "mfn"
) |>
crossing(snapshot_dates) |>
filter(
begin_effective_date <= snapshot_date,
end_effective_date >= snapshot_date
) |>
group_by(hts8, agreement, year) |>
slice_max(
begin_effective_date,
n = 1,
with_ties = FALSE
) |>
ungroup()
plot <- tariff_annual |>
group_by(year) |>
summarise(
n_products = n_distinct(hts8),
n_lines = n(),
.groups = "drop"
) |>
ggplot(aes(x = year, y = n_products)) +
geom_line(linewidth = 1.5, colour = "#287C8E") +
geom_point(size = 3, colour = "#287C8E") +
scale_x_continuous(breaks = seq(1990, 2025, 5)) +
scale_y_continuous(labels = scales::label_comma()) +
labs(
title = "Historical Coverage of the U.S. Agricultural Tariff Schedule",
x = "Year",
y = "Number of distinct HTS8 products"
) +
theme_bw(base_size = 17) +
theme(
plot.margin = margin(b = 10),
plot.title = element_text(face = "bold", size = 25, hjust = 0)) +
add_caption_cwb()
# Save and display images
current_dir <- dirname(knitr::current_input())
plot_name <- "ag_tarriffs.png"
ggsave(plot = plot,
dpi = "screen",
width = 16,
height = 10,
device = ragg::agg_png,
filename = file.path(current_dir, plot_name))
# Read the big plot
img <- image_read(file.path(current_dir, plot_name))
# Force 16:9 aspect ratio with minimal padding
# Target size: 1200x675 px (16:9)
img_card <- image_scale(img, "1200x675") # scale to fit inside 16:9
img_card <- image_extent(
img_card,
geometry = "1200x675",
gravity = "center"
)
# Save as card preview
image_write(img_card, path = file.path(current_dir, "preview.png"))
knitr::include_graphics(
file.path(current_dir, plot_name)
)
References
cite_packages(format = "rmd")Wickham H, Pedersen T, Seidel D (2025). scales: Scale Functions for Visualization. doi:10.32614/CRAN.package.scales https://doi.org/10.32614/CRAN.package.scales. R package version 1.4.0, https://CRAN.R-project.org/package=scales.
Pebesma E, Bivand R (2023). Spatial Data Science: With applications in R. Chapman and Hall/CRC. doi:10.1201/9780429459016 https://doi.org/10.1201/9780429459016. https://r-spatial.org/book/.
Pebesma E (2018). “Simple Features for R: Standardized Support for Spatial Vector Data.” The R Journal, 10(1), 439-446. doi:10.32614/RJ-2018-009 https://doi.org/10.32614/RJ-2018-009. https://doi.org/10.32614/RJ-2018-009.
Ooms J (2026). magick: Advanced Graphics and Image-Processing in R. doi:10.32614/CRAN.package.magick https://doi.org/10.32614/CRAN.package.magick. R package version 2.9.1, https://CRAN.R-project.org/package=magick.
Baril C (2026). trashpanda: Cole’s Personal Collection of R Functions, Themes, and Palettes. R package version 0.0.1, commit d16de29303b66b1398238885a35c8a8b1aaf077f, https://github.com/colebaril/trashpanda.
Grolemund G, Wickham H (2011). “Dates and Times Made Easy with lubridate.” Journal of Statistical Software, 40(3), 1-25. https://www.jstatsoft.org/v40/i03/.
Wickham H (2025). forcats: Tools for Working with Categorical Variables (Factors). doi:10.32614/CRAN.package.forcats https://doi.org/10.32614/CRAN.package.forcats. R package version 1.0.1, https://CRAN.R-project.org/package=forcats.
Wickham H (2025). stringr: Simple, Consistent Wrappers for Common String Operations. doi:10.32614/CRAN.package.stringr https://doi.org/10.32614/CRAN.package.stringr. R package version 1.6.0, https://CRAN.R-project.org/package=stringr.
Wickham H, François R, Henry L, Müller K, Vaughan D (2026). dplyr: A Grammar of Data Manipulation. doi:10.32614/CRAN.package.dplyr https://doi.org/10.32614/CRAN.package.dplyr. R package version 1.2.1, https://CRAN.R-project.org/package=dplyr.
Wickham H, Henry L (2026). purrr: Functional Programming Tools. doi:10.32614/CRAN.package.purrr https://doi.org/10.32614/CRAN.package.purrr. R package version 1.2.2, https://CRAN.R-project.org/package=purrr.
Wickham H, Hester J, Bryan J (2026). readr: Read Rectangular Text Data. doi:10.32614/CRAN.package.readr https://doi.org/10.32614/CRAN.package.readr. R package version 2.2.0, https://CRAN.R-project.org/package=readr.
Wickham H, Vaughan D, Girlich M (2025). tidyr: Tidy Messy Data. doi:10.32614/CRAN.package.tidyr https://doi.org/10.32614/CRAN.package.tidyr. R package version 1.3.2, https://CRAN.R-project.org/package=tidyr.
Müller K, Wickham H (2026). tibble: Simple Data Frames. doi:10.32614/CRAN.package.tibble https://doi.org/10.32614/CRAN.package.tibble. R package version 3.3.1, https://CRAN.R-project.org/package=tibble.
Wickham H (2016). ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. ISBN 978-3-319-24277-4. https://ggplot2.tidyverse.org.
Wickham H, Averick M, Bryan J, Chang W, McGowan LD, François R, Grolemund G, Hayes A, Henry L, Hester J, Kuhn M, Pedersen TL, Miller E, Bache SM, Müller K, Ooms J, Robinson D, Seidel DP, Spinu V, Takahashi K, Vaughan D, Wilke C, Woo K, Yutani H (2019). “Welcome to the tidyverse.” Journal of Open Source Software, 4(43), 1686. doi:10.21105/joss.01686 https://doi.org/10.21105/joss.01686.
Harmon J, Hughes E (2026). tidytuesdayR: Access the Weekly ‘TidyTuesday’ Project Dataset. doi:10.32614/CRAN.package.tidytuesdayR https://doi.org/10.32614/CRAN.package.tidytuesdayR. R package version 1.3.3, https://CRAN.R-project.org/package=tidytuesdayR.
Rinker TW, Kurkiewicz D (2018). pacman: Package Management for R. version 0.5.0, http://github.com/trinker/pacman.