Load packages
library(tidyverse)
library(readxl)
library(janitor)
library(gridExtra)
library(kableExtra)
library(ggpp)
Get the data
trade_data <- read_excel("data/trade-data.xlsx",
col_types = c("text", "numeric", "numeric",
"text", "numeric", "numeric", "numeric"))
df <- trade_data
Data wrangling
df1 <- df |> clean_names() |>
rename(import_country = us_goods_exports_in_2022_top_5,
goods_exports = x2,
category = us_trade_in_billion_2021_2022,
year_2022 = x5,
year_2021 = x7) |>
select(-c(x3, x6)) |>
filter(!is.na(goods_exports))
#
df2 <- df1 |>
select(-c(1:2)) |>
na.omit() |>
pivot_longer(-category, names_to = c("aa", "year"), names_pattern = '([A-Za-z]+)_([0-9]+)',
values_to = "value")
Look at the data
df1 |>
select(1:2) |>
kable(caption = "<b>U.S. GOODS EXPORTS IN 2022: TOP 5 IMPORTING COUNTRIES<br> $ billion</b>") |>
kable_styling(bootstrap_options = c("striped"))
U.S. GOODS EXPORTS IN 2022: TOP 5 IMPORTING COUNTRIES
$
billion
import_country
|
goods_exports
|
Canada
|
356.5
|
Mexico
|
324.3
|
China
|
150.4
|
Japan
|
80.2
|
UK
|
76.2
|
df1 |>
select(-c(1:2)) |>
na.omit() |>
kable(caption = "<b>U.S. TRADE IN 2021-2022<br> $ billion</b>",
col.names = c("Category", "2022", "2021")) |>
kable_styling(bootstrap_options = c("striped"))
U.S. TRADE IN 2021-2022
$ billion
Category
|
2022
|
2021
|
Goods exports
|
2100.0
|
1792.7
|
Goods imports
|
3200.0
|
2786.3
|
Services exports
|
926.0
|
795.3
|
Services imports
|
680.3
|
550.0
|
The plots thicken!
df1 |>
ggplot() +
aes(x = reorder(import_country, -goods_exports), y = goods_exports) +
geom_col(width = 0.5, fill = "darkred") +
labs(x = "", y = "$, billions",
title = "US exports of goods in 2022: Top 5 destinations",
caption = "Data: USTR | Plot: SP")

df2 |>
ggplot() +
aes(x = category, y = value, fill = year, label = round(value, 0)) +
geom_col() +
geom_text(size = 3, position = position_stacknudge(y = -250)) +
labs(x = "", y = "$, billions",
title = "Stacked bar chart: U.S. trade in 2021-22",
caption = "Data: USTR | Plot: SP")

df2 |>
ggplot() +
aes(x = category, y = value, fill = year, label = round(value, 0)) +
geom_col(position = position_dodge()) +
geom_text(size = 3, vjust = 1.5, position = position_dodge(0.9)) +
labs(x = "", y = "$, billions",
title = "Side-by-side bar chart: U.S. trade in 2021-22",
caption = "Data: USTR | Plot: SP")

df2 |>
ggplot() +
aes(x = category, y = value, label = round(value, 0), fill = year) +
geom_col(width = 0.2) +
geom_text(size = 3, hjust = 1.2) +
coord_flip() +
facet_grid(.~year) +
labs(x = "", y = "$, billions",
title = "Facet grid: U.S. trade in 2021-22",
caption = "Data: USTR | Plot: SP") +
theme(
legend.position = "none",
strip.text = element_text(face = "bold", size=rel(0.8)))

df2 |>
ggplot() +
aes(x = year, y = value, label = round(value, 0), fill = year) +
geom_col(width = 0.5) +
geom_text(size = 2.5, vjust = 1.5) +
facet_grid(.~category) +
labs(x = "", y = "$, billions",
title = "Another facet grid: U.S. trade in 2021-22",
caption = "Data: USTR | Plot: SP") +
theme(
legend.position = "none",
strip.text = element_text(face = "bold", size=rel(0.8)))

Theend