Column

Stock Closing Prices

Stock Closing Prices Interactive

Column

AAPL

AMZN

GOOGL

MSFT

Column

Stock Closing Price Dotted

---
title: "DASHBOARDING LAB"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library("tidyquant")
```

```{r message = FALSE}
symbols <- c("MSFT","AAPL","GOOGL","AMZN")
data <- tq_get(symbols)

invisible(data %>% 
  select(symbol, date, close) %>% 
  spread(key = symbol, value = close))

data$day <- weekdays(as.Date(data$date,'%Y-%m-%d'))
data$weekNum <- strftime(data$date, format = "%V")
library("lubridate")
data$month <- month(as.POSIXlt(data$date, format="%Y/%m/%d"))
data$difference = data$close - data$open
```

```{r message = FALSE}
library(ggplot2)
library(xts)
```

```{r message = FALSE}
msft = data[data$symbol == "MSFT",]
aapl = data[data$symbol == "AAPL",]
googl = data[data$symbol == "GOOGL",]
amzn = data[data$symbol == "AMZN",]
```

Column {.tabset data-width=300}
-----------------------------------------------------------------------

### Stock Closing Prices

```{r}
ggplot(msft,aes(date,close)) + 
  geom_line(aes(color="msft")) +
  geom_line(data=aapl,aes(color="aapl")) +
  geom_line(data=googl,aes(color="googl")) +
  geom_line(data=amzn,aes(color="amzn")) +
  labs(color="Legend") +
  scale_colour_manual("", breaks = c("msft", "aapl","googl","amzn"),
                      values = c("blue", "yellow","red","green")) +
  ggtitle("Closing Stock Prices: MSFT, AAPL, GOOGL & AMZN") + 
  theme(plot.title = element_text(lineheight=.7, face="bold"))
```

```{r message = FALSE}
aapl_xts <- xts(aapl$close,order.by=aapl$date,frequency=365)
amzn_xts <- xts(amzn$close,order.by=amzn$date,frequency=365)
googl_xts <- xts(googl$close,order.by=googl$date,frequency=365)
msft_xts <- xts(msft$close,order.by=msft$date,frequency=365)

stocks <- cbind(aapl_xts,amzn_xts,googl_xts,msft_xts)
```

### Stock Closing Prices Interactive

```{r}
library(dygraphs)
dygraph(stocks,ylab="Close", 
        main="Closing Stock Prices") %>%
  dySeries("..1",label="AAPL") %>%
  dySeries("..2",label="AMZN") %>%
  dySeries("..3",label="GOOGL") %>%
  dySeries("..4",label="MSFT") %>%
  dyOptions(colors = c("yellow","blue","brown","green")) %>%
  dyRangeSelector()
```

```{r message = FALSE}
start <- as.Date("2014-09-01")
end <- as.Date("2017-09-01")
```


Column {.tabset-fade .tabset data-width=300}
-------------------------------------
   
### AAPL

```{r}
invisible(getSymbols("AAPL", src = "yahoo", from = start, to = end))
candleChart(AAPL, up.col = "black", dn.col = "red", theme = "white")
```   
 
### AMZN
    
```{r}
invisible(getSymbols("AMZN", src = "yahoo", from = start, to = end))
candleChart(AMZN, up.col = "black", dn.col = "red", theme = "white")
```

### GOOGL

```{r}
invisible(getSymbols("GOOGL", src = "yahoo", from = start, to = end))
candleChart(GOOGL, up.col = "black", dn.col = "red", theme = "white")
```

### MSFT

```{r}
invisible(getSymbols("MSFT", src = "yahoo", from = start, to = end))
candleChart(MSFT, up.col = "black", dn.col = "red", theme = "white")
```


Column {data-width=300}
-------------------------------------
### Stock Closing Price Dotted

```{r message = FALSE}
invisible(stocks <- as.xts(data.frame(AAPL = AAPL[, "AAPL.Close"], 
                            MSFT = MSFT[, "MSFT.Close"], 
                            GOOGL = GOOGL[, "GOOGL.Close"],
                            AMZN = AMZN[, "AMZN.Close"]
)))
```

```{r message = FALSE}
plot(as.zoo(stocks), screens = 1, lty = 1:4, xlab = "Date", ylab = "Price")
legend("right", c("AAPL", "MSFT", "GOOG","AMZN"), lty = 1:4, cex = 0.5)
```