JHU-D9-Developing Data Products-W3-Assignment-Plotly2

"Spark-lin"
"11/27/2016"

Generally idea of the assignment

I want to use plotly to create an interactive chart of three selected Internet Companies' stock price.

  • My first screened stock pool: GOOGL(Google), AAPL(Apple), FB(Facebook), yet I encounted some errors.

    • Error in data.frame(Date = index(AAPL), AAPL[, 6], FB[, 6], GOOGL[, 6]) : arguments imply differing number of rows: 2494, 1139
    • Also I could not proceed the plot_ly to plot the interactive chart. This is due to the fact that Facebook's initial public offering (IPO) on Friday, May 18, 2012. Therefore, Facebook has different data row with other selected companied.
  • Thus, I modified my stock pool into : GOOGL(Google), AAPL(Apple), YHOO(Yahoo)

Steps to create interactive chart

1-Get StockExchange symbol of selected Stocks
2-Create dataframe with selected stock
3-Plot interactive chart with selected stock

Slide With Code

library(plotly)
library(tidyr)
library(dplyr)
library(quantmod)
library(ggplot2)
library(webshot)
library(Cairo)

getSymbols(Symbols = c("AAPL", "MSFT", "YHOO"))

ds <- data.frame(Date = index(AAPL), AAPL[, 6], MSFT[, 6], YHOO[, 6])
p <- plot_ly(ds, x = ~Date) %>%
  add_lines(y = ~AAPL.Adjusted, name = "Apple") %>%
  add_lines(y = ~MSFT.Adjusted, name = "Microsoft") %>%
  add_lines(y = ~YHOO.Adjusted, name = "YAHOO") %>%
  layout(
    title = "",
    xaxis = list(title = "Year 2008-2016" ,
      rangeselector = list(
        buttons = list(
          list(
            count = 1,
            label = "1 M",
            step = "month",
            stepmode = "backward"),
          list(
            count = 3,
            label = "3 M",
            step = "month",
            stepmode = "backward"),
          list(
            count = 6,
            label = "6 M",
            step = "month",
            stepmode = "backward"),
          list(
            count = 1,
            label = "1 Y",
            step = "year",
            stepmode = "backward"),
          list(
            count = 1,
            label = "YTD",
            step = "year",
            stepmode = "todate"),
          list(step = "all"))),
      rangeslider = list(type = "Year")),
    yaxis = list(title = "Stock Price"))

Slide With Plot