close
close

first Drop

Com TW NOw News 2024

Visualizing the Impact of Rising US Crude Oil Production on Prices
news

Visualizing the Impact of Rising US Crude Oil Production on Prices

(This article was first published on DataGeeekand kindly contributed to R-bloggers). (You can report problems with the content of 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 inventories rose more than expected, but that did not cause oil prices to fall amid expectations of a Fed rate cut. Crude oil production has risen 68% since 2014, while prices have fallen 20%.

library(tidyverse)
library(tidyquant)

#Crude Oil Futures(USD) (Index 2014 = 100)
df_crude_oil % 
  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 % 
  select(date, crude_oil_production = price) 

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

#Index based on benchmark date (2014 = 100)
df_index % 
  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 % 
  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` % 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")