Gold’s Rise Over Silver Amid Trump Tariffs
[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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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")
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.