Visualizing the Impact of U.S. Crude Oil Production Surge on Prices

[This article was first published on DataGeeek, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

U.S. Crude Oil Inventories increased more than expected, but this didn’t cause oil prices to decline amid FED rate cut expectations. Crude oil production increased by 68% since 2014, while prices fell by 20%.

library(tidyverse)
library(tidyquant)

#Crude Oil Futures(USD) (Index 2014 = 100)
df_crude_oil <- 
  tq_get("CL=F") %>% 
  tq_transmute(select = close,
               mutate_fun = to.monthly,
               col_rename = "crude_oil") %>% 
  mutate(date = as.Date(date))

#Industrial Production: Mining: Crude Oil (NAICS = 21112) (Index 2014 = 100)
df_crude_oil_production <- 
  tq_get("IPG21112S", get = "economic.data") %>% 
  select(date, crude_oil_production = price) 

#Merging all the data sets
df_merged <- 
  df_crude_oil %>%
  left_join(df_crude_oil_production) %>% 
  drop_na()

#Index based on benchmark date (2014 = 100)
df_index <- 
  df_merged %>% 
  pivot_longer(cols = -date, names_to = "vars") %>% 
  mutate(vars = case_when(
    vars == "crude_oil" ~ "Crude Oil Futures",
    vars == "crude_oil_production" ~ "Crude Oil Production")) %>% 
  group_by(vars) %>% 
  mutate(value = (value / first(value)) * 100) %>% 
  ungroup()

#Dataset for text line
df_index_wider <- 
  df_index %>% 
  pivot_wider(names_from = "vars",
              values_from = "value")


#Comparison plot
df_index %>% 
  ggplot(aes(date, value, col = vars)) +
  ggbraid::geom_braid(
    data = df_index_wider, 
    aes(
      y = NULL, # Overwrite the inherited aes from ggplot()
      col = NULL, 
      ymin = `Crude Oil Production`, 
      ymax = `Crude Oil Futures`, 
      fill = `Crude Oil Production` < `Crude Oil Futures`
    ), 
    alpha = 0.6
  ) +
  geom_line(linewidth = 1.25) +
  geomtextpath::geom_textline(
    data = df_index %>% filter(vars == "Crude Oil Production"),
    aes(label = vars),
    hjust = 0,
    vjust = 0,
    family = "Bricolage Grotesque",
    text_smoothing = 40,
    size = 8) +
  geomtextpath::geom_textline(
    data = df_index %>% filter(vars == "Crude Oil Futures"),
    aes(label = vars),
    hjust = 1,
    vjust = 2.2,
    family = "Bricolage Grotesque",
    size = 8,
    text_smoothing = 60) +
  scale_color_manual(
    values = c("steelblue", "orangered")) +
  scale_fill_manual(
    values = c("TRUE" = "steelblue", 
               "FALSE" = "orangered")) +
  scale_x_date(expand = expansion(mult = c(.05, .1))) +
  labs(
    x = element_blank(), 
    y = element_blank(),
    subtitle = "Change of % (Index 2014 = 100)") +
  theme_minimal(base_size = 20, 
                base_family = "Bricolage Grotesque") +
  theme(panel.grid.minor = element_blank(),
        legend.position = "none")
To leave a comment for the author, please follow the link and comment on their blog: DataGeeek.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)