Data Visualization

options("getSymbols.warning4.0"=FALSE)
options("getSymbols.yahoo.warning"=FALSE)

tickers = c("MSFT","SBUX","COST" )
for (i in tickers){
  getSymbols(i,
             from = "2012-10-01",
             to = "2022-12-01")}

x <- list(
  title = "date"
)
y <- list(
  title = "value"
)

stock <- data.frame(MSFT$MSFT.Adjusted,
                    SBUX$SBUX.Adjusted,
                    COST$COST.Adjusted)


stock <- data.frame(stock,rownames(stock))
colnames(stock) <- append(tickers,'Dates')

stock$date<-as.Date(stock$Dates,"%Y-%m-%d")
head(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")) 

g4<- 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")+
    guides(colour=guide_legend(title="Top Companies")) 


ggplotly(g4) %>%
  layout(hovermode = "x")