# Libraries
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.5.2
## Warning: package 'readr' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.2.0     
## ── 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
# Load Data
load("~/Downloads/Jobs_Goods_Exports.RData")
attach(Jobs_Supported_by_Goods_Exports)

# Create G7 Dataframe (Excluding U.S.)
G7_df <- Jobs_Supported_by_Goods_Exports |>
  filter(Partner %in% c("Canada", "France", "Germany", "Italy", "Japan", "United Kingdom"))

# Make Dataframe Long
G7_long <- G7_df |>
  pivot_longer(
    cols = c("2018", "2019", "2020", "2021", "2022"),
    names_to = "Year",
    values_to = "Jobs_Supported"
  ) |>
  mutate(Year = as.Date(paste0(Year, "-01-01")))

# Check Datatypes
str(G7_long)
## tibble [30 × 3] (S3: tbl_df/tbl/data.frame)
##  $ Partner       : chr [1:30] "Canada" "Canada" "Canada" "Canada" ...
##  $ Year          : Date[1:30], format: "2018-01-01" "2019-01-01" ...
##  $ Jobs_Supported: num [1:30] 1127402 1101059 934748 993759 1096836 ...
# Plot
ggplot(data = G7_long, mapping = aes(x = Year, y = Jobs_Supported, colour = Partner)) +
  geom_line(linewidth = 1.2) +
  scale_y_continuous(labels = scales::comma) +
  scale_color_discrete(name = "Country") +
  labs(
    title = "U.S. Jobs Supported by Goods Exports to G7 Countries, 2018–2022",
    subtitle = "Source: https://www.trade.gov/data-visualization/country-reports-ita-industry-analysis",
    x = "Year",
    y = "Jobs Supported"
  ) +
  theme_bw() +
  theme(legend.position = "bottom")