Gold’s Rise Over Silver Amid Trump Tariffs

The impact of Trump tariffs has boosted gold to outpost silver.

Source code:

library(tidyverse)
library(tidyquant)

#XAU/USD | Gold Spot US Dollar Price
df_xau <- 
  df_xau <- 
  read_csv("data/xau.csv") %>% 
  janitor::clean_names() %>% 
  mutate(date = parse_date(date, "%m/%d/%Y")) %>% 
  select(date, xau = price)

#XAG/USD | Silver Spot US Dollar Price
df_xag <- 
  df_xag <- 
  read_csv("data/xag.csv") %>% 
  janitor::clean_names() %>% 
  mutate(date = parse_date(date, "%m/%d/%Y")) %>% 
  select(date, xag = price)

#Merging the datasets
df_merged <- 
  df_xau %>% 
  left_join(df_xag) %>% 
  drop_na()


#Index data (Range:0-100)
library(recipes)

df_index <- 
  df_merged %>% 
  recipe(~ .) %>% 
  step_range(all_numeric(), 
             min = 0, 
             max = 100) %>% 
  prep() %>% 
  bake(new_data = NULL) %>% 
  pivot_longer(-date) 

#Plot
df_index %>% 
  ggplot(aes(date, value, col = name)) +
  geom_line(linewidth = 2, aes(group = name)) +
  geom_point(aes(group = name), size = 5) +
  ggrepel::geom_text_repel(
    data = df_index %>% group_by(name) %>% slice_head(n = 1),
    aes(label = round(value,0)),
    hjust = 1, 
    vjust = 1,
    nudge_x = 0.5,
    fontface = "bold", 
    family = "Roboto Slab",
    size = 6,
    segment.color = NA
  ) +
  ggbraid::geom_braid(
    data = df_index %>% pivot_wider(), 
    aes(
      y = NULL, ## Overwrite the inherited aes from ggplot()
      col = NULL, 
      ymin = xag, 
      ymax = xau, 
      fill = xag < xau
    ), 
    alpha = 0.3
  ) +
  scale_color_manual(values = c("xau" = "gold", 
                                "xag" = "darkgray")) +
  scale_fill_manual(values = c("TRUE" = "gold", 
                               "FALSE" = "darkgray")) +
  scale_x_date(expand = expansion(mult = c(.1, .15))) +
  labs(
    x = element_blank(), 
    y = element_blank(),
    subtitle = "Normalized to 1-100",
    title = "<span style = 'color:gold;'>XAU/USD | Gold Spot US Dollar Price</span><br><span style = 'color:darkgray;'>XAG/USD | Silver Spot US Dollar Price</span>"
  ) +
  theme_minimal(base_family = "Roboto Slab", base_size = 18) +
  theme(
    panel.grid = element_blank(),
    panel.grid.major.x = element_line(linetype = "dashed", color = "gray"),
    panel.grid.major.y = element_line(linetype = "dashed", color = "gray"),
    plot.subtitle = ggtext::element_markdown(face = "bold"),
    plot.title = ggtext::element_markdown(face = "bold"),
    axis.text = element_text(face = "bold"),
    plot.background = element_rect(fill = "azure", color = "azure"),
    legend.position = "none")

One response to “Gold’s Rise Over Silver Amid Trump Tariffs”

  1. Samohyl Avatar
    Samohyl

    Here in Brazil we have seen an unnecessay divergence for the price of Brazilian Reals in US Dollars by as much as 20% reporting a devaluation resulting in some financial and concurrent political turmoil. Which is your prefered data source for foreign exchange series?

    Like

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