Data Visualization
options("getSymbols.warning4.0"=FALSE)
options("getSymbols.yahoo.warning"=FALSE)
= c("MSFT","SBUX","COST" )
tickers for (i in tickers){
getSymbols(i,
from = "2012-10-01",
to = "2022-12-01")}
<- list(
x title = "date"
)<- list(
y title = "value"
)
<- data.frame(MSFT$MSFT.Adjusted,
stock $SBUX.Adjusted,
SBUX$COST.Adjusted)
COST
<- data.frame(stock,rownames(stock))
stock colnames(stock) <- append(tickers,'Dates')
$date<-as.Date(stock$Dates,"%Y-%m-%d")
stockhead(stock)
MSFT SBUX COST Dates date
2012-10-01 24.11845 20.94163 76.89466 2012-10-01 2012-10-01
2012-10-02 24.25748 20.58669 76.11429 2012-10-02 2012-10-02
2012-10-03 24.42105 20.66603 76.21375 2012-10-03 2012-10-03
2012-10-04 24.56009 20.50317 77.63676 2012-10-04 2012-10-04
2012-10-05 24.41287 20.35284 77.87390 2012-10-05 2012-10-05
2012-10-08 24.35562 20.36119 77.53726 2012-10-08 2012-10-08
ggplot(stock, aes(x=date)) +
geom_line(aes(y=MSFT, colour="MSFT"))+
geom_line(aes(y=SBUX, colour="SBUX"))+
geom_line(aes(y=COST, colour="COST"))+
labs(
title = "Stock Prices for the Top Companies",
subtitle = "From 2013-2022",
x = "Date",
y = "Adjusted Closing Prices")+
theme(panel.background = element_rect(fill = "white", colour = "grey50"))+
guides(colour=guide_legend(title="Tech Companies"))
<- ggplot(stock, aes(x=date)) +
g4geom_line(aes(y=MSFT, colour="MSFT"))+
geom_line(aes(y=SBUX, colour="SBUX"))+
geom_line(aes(y=COST, colour="COST"))+
labs(
title = "Stock Prices for the Top Companies",
subtitle = "From 2013-2022",
x = "Date",
y = "Adjusted Closing Prices")+
guides(colour=guide_legend(title="Top Companies"))
ggplotly(g4) %>%
layout(hovermode = "x")