Putting Visual Analytics into Practical Use
This take-home exercise requires to apply appropriate data visualization techniques learned in Lesson 6 to accomplish the following tasks:
Script stock prices of top 40 companies in Singapore by market capitalisation between 1st January 2020 - 31st December 2021 by using tidyquant R package.
Using horizon graph to plot historical stock prices by the top 40 companies by market capitalisation.
In this exercise, tidyquant, tidyverse, ggplot2, dyplyr, and Knitr will be used.
readr for importing csv into R.tidyquant to return objects in the tidy ‘tibble’ format and use quantitative functions together with the tidyverse functions.knitr for building static html table.ggHoriPlot for building horizon plots in ggplot2.packages = c('tidyverse', 'tidyquant', 'ggplot2','dplyr','tidyr','ggthemes','ggHoriPlot',
'knitr')
for(p in packages){
if(!require(p,character.only = T)){
install.packages(p)
}
library(p,character.only = T)
}
Script data from top 40 companies in Singapore by market capitalisation
Reference: scraping of fiancial dataset
Companies<-read.csv('data/companiesmarketcap.csv')
Only the list of symbols was retained for the subsequent use. slice_max() is used to get the top 40 stocks in SGX by marketcap.
top40<-Companies%>%
slice_max(`marketcap`,n=40)%>%
select(Symbol,Name)
The following code is used to manipulate and select the stock symbols within 1 January 2020 to 31 December 2021 from Yahoo Finance as required. The data was returned in daily intervals. tq_get() function in tidyquant R packages is used to retrieve date, volume, opening, highest, lowest, closing, and adjusted price. group_by() function is used to select() function is used to retain the symbol, name, data and close price column.
from_date = "2020-01-01"
to_date = "2021-12-31"
period_type = "days" # "days"/ "weeks"/ "months"/ "years"
stock_data_daily<-top40 %>%
tq_get(get = "stock.prices",
from = from_date,
to = to_date) %>%
group_by(Symbol,Name)%>%
select(Symbol,Name,date,close)%>%
tq_transmute(select = NULL,
mutate_fun = to.period,
period = period_type)
rmarkdown::paged_table(stock_data_daily)
Symbol <chr> | Name <chr> | date <date> | close <dbl> | |
|---|---|---|---|---|
| SE | Sea (Garena) | 2020-01-02 | 40.040001 | |
| SE | Sea (Garena) | 2020-01-03 | 40.490002 | |
| SE | Sea (Garena) | 2020-01-06 | 40.480000 | |
| SE | Sea (Garena) | 2020-01-07 | 41.009998 | |
| SE | Sea (Garena) | 2020-01-08 | 40.160000 | |
| SE | Sea (Garena) | 2020-01-09 | 40.529999 | |
| SE | Sea (Garena) | 2020-01-10 | 39.580002 | |
| SE | Sea (Garena) | 2020-01-13 | 40.020000 | |
| SE | Sea (Garena) | 2020-01-14 | 39.939999 | |
| SE | Sea (Garena) | 2020-01-15 | 40.419998 |
Build the horizon plots in ggplot2 using geom_horizon().
geom_horizon() is used to plot the graph.scale_fill_hcl()is used to adjust the color scheme of graph.facet_grid()is used to parse the name variable to Y axis.stock_data_daily %>% ggplot() +
geom_horizon(aes(date, close))+
scale_fill_hcl(palette = 'RdBu', reverse = F) + #set the color palette
facet_grid(Name~.) +
theme_few() +
theme(
panel.spacing.y=unit(0, "lines"),
strip.text.y = element_text(size = 7, angle = 0, hjust = 0),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
panel.border = element_blank()
) +
scale_x_date(expand=c(0,0),
date_breaks = "1 month",
date_labels = "%b%Y") +
xlab('Date') +
ggtitle('Close price of SGX top 40 by marketcap',
'from 1/1/2020 to 31/12/2021')
Adjust the size of fond, set the x-axis interval to 2 months.
stock_data_daily %>% ggplot() +
geom_horizon(aes(date, close))+
scale_fill_hcl(palette = 'RdBu', reverse = F) + #set the color palette
facet_grid(Name~.) +
theme_few() +
theme(
panel.spacing.y=unit(0, "lines"),
strip.text.y = element_text(size = 16, angle = 0, hjust = 0),
axis.text.x = element_text(size = 14),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
panel.border = element_blank(),
axis.title=element_text(size=20,face="bold"),
plot.title = element_text(size=28, face = "bold"),
plot.subtitle = element_text(size=20, face = "bold")
)+
scale_x_date(expand=c(0,0),
date_breaks = "2 month",
date_labels = "%b") +
xlab('Date') +
ggtitle('Close price of SGX top 40 by marketcap',
'from 1/1/2020 to 31/12/2021')+
guides(fill= guide_legend(title="stock price +(blue) or -(red)",
title.position = "top")
)
Inspired by visualizations shown in the class, consider adopting x-axis intersection Line to highlight the timeline regarding covid-19 related events in Singapore and worldwide. To examine the impact of COVID-19 pandemic and companies’ stock price listed in the SGX.
geom_vline() is used to mark the time line.
stock_data_daily %>% ggplot() +
geom_horizon(aes(date, close))+
scale_fill_hcl(palette = 'RdBu', reverse = F) + #set the color palette
geom_vline(xintercept = as.Date("2020-03-19"), linetype = "dotdash", color = "green", size = 0.5) +
geom_vline(xintercept = as.Date("2020-03-23"), linetype = "dotdash", color = "green", size = 0.5) +
geom_vline(xintercept = as.Date("2020-04-07"), linetype = "dotted", size = 0.5) +
geom_vline(xintercept = as.Date("2020-06-01"), linetype = "dotted", size = 0.5) +
geom_vline(xintercept = as.Date("2020-12-14"), linetype = "dotted", size = 0.5) +
facet_grid(Name~.) +
theme_few() +
theme(
panel.spacing.y=unit(0, "lines"),
strip.text.y = element_text(size = 16, angle = 0, hjust = 0),
axis.text.x = element_text(size = 14),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
panel.border = element_blank(),
axis.title=element_text(size=20),
plot.title = element_text(size=28, face = "bold"),
plot.subtitle = element_text(size=20, face = "bold")
)+
scale_x_date(expand=c(0,0),
date_breaks = "3 month",
date_labels = "%b%Y") +
xlab('Date') +
ggtitle('Close price of SGX top 40 by marketcap',
'from 1/1/2020 to 31/12/2021')+
guides(fill= guide_legend(title="stock price +(blue) or -(red)",
title.position = "top")
)
The covid-19 timeline in Singapore/the world according to straitstimes:
19 March 2020:
23 March 2020:
7 April 2020:
1 Jun 2020:
14 Dec 2020:
PM’s deliver the speech which marks the begin of Singapore’s attempt to enter into phase 3 and its confidence in reopen.
The releasing of good news encouraged the public and revitalized confidence in the stock market. Around this time all 40 stocks started to show signs of rebound.
From December 2020 till December 2021, even during the wave Delta and Omicron, SGX market shown a good resilience to pressure and the patter of first 6 months of 2020 has not reappeared.
Companies’ recovery speed from the hit of COVID-19 vary due to the different traits of their industrie background. Some took little impact from COVID-19, for example, the real estate sector (Keppel), fibre network infrastructure(Netline Trust), even during the first six months in 2020, those two companies still showed great resilience. On the other hand, companies with strong links to the development of cities(dependent on foreign workforce) and tourism suffer the greatest impact. For example, Singapore Airlines and City development have continued to fall and no positive signals have been seen so far.