[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.
Donald Trump’s recent election win has sparked a rapid surge in sectors like Financials. Barclays calls this a clear reflection of the “Trump playbook,” trends seen in 2016. According to this, the iShares Global Financials ETF seems to have room to continue the uptrend.
Source code:
library(tidyverse) library(tidyquant) library(timetk) #iShares Global Financials ETF (IXG) Stock Price df_ixg <- tq_get("IXG") %>% select(date, ixg = close) #2016 term df_ixg_2016 <- df_ixg %>% filter(date >= as.Date("2016-10-29"), date <= as.Date("2016-12-08")) %>% mutate(ixg = round(ixg / subset(., date=="2016-11-08")[["ixg"]] * 100), symbol = "2016") %>% mutate( days_from_election = as.integer(date - as.Date("2016-11-08")), t_label = ifelse( days_from_election == 0, "t", ifelse( days_from_election > 0, paste0("t+", days_from_election), paste0("t", days_from_election) ) ) ) %>% select(t_label, symbol, ixg) %>% mutate(t_label = factor(t_label, levels = .$t_label)) #2024 term df_ixg_2024 <- df_ixg %>% filter(date >= as.Date("2024-10-26"), date <= as.Date("2024-11-13")) %>% mutate(ixg = round(ixg / subset(., date=="2024-11-05")[["ixg"]] * 100), symbol = "2024") %>% mutate( days_from_election = as.integer(date - as.Date("2024-11-05")), t_label = ifelse( days_from_election == 0, "t", ifelse( days_from_election > 0, paste0("t+", days_from_election), paste0("t", days_from_election) ) ) ) %>% select(t_label, symbol, ixg) %>% mutate(t_label = factor(t_label, levels = .$t_label)) #Merging the datasets df_merged <- bind_rows( df_ixg_2024, df_ixg_2016 ) #Plot df_merged %>% ggplot(aes(t_label, ixg, col = symbol)) + geom_line(linewidth = 1.5, aes(group = symbol)) + ggrepel::geom_text_repel( data = . %>% slice_tail(n = 1, by = symbol), aes(label = ixg), hjust = 1, vjust = 1, nudge_x = 0.5, size = 8, face = "bold", family = "Roboto Slab" ) + scale_color_manual( values = c("2024" ="darkorange","2016" = "navyblue") ) + scale_x_discrete(expand = expansion(mult = c(.1, .1)), breaks = c(as.factor("t-8"), as.factor("t"), as.factor("t+8"), as.factor("t+30"))) + geom_vline(xintercept = "t", size = 1.5, linetype= "dashed", color = "red") + labs( x = element_blank(), y = element_blank(), subtitle = "<span style = 'color:red;'>US Election Date</span><br>Daily Index: (t = 100)", title = "iShares Global Financials ETF<br><span style = 'color:darkorange;'>2024</span> vs. <span style = 'color:navyblue'>2016</span>" ) + theme_minimal( base_family = "Roboto Slab" ) + 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",size = 18), plot.title = ggtext::element_markdown(face = "bold", size = 20), axis.text = element_text(face = "bold", size = 18), 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.