Exploratory Data Analysis: U.S. Crude Oil Production Surge on Prices

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%.

Source code:

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")

Leave a comment

I’m Selcuk Disci

Welcome to DataGeeek.com, dedicated to data science and machine learning with R, mostly based on financial data.

Let’s connect