---
title: "ANLY 512: Data Visualization Dashboard Laboratory"
author: Karoli Sakwa
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
social: menu
source_code: embed
---
```{r setup, include=FALSE}
library(flexdashboard)
```
```{r message=FALSE}
library(PerformanceAnalytics)
library(quantmod)
library(dygraphs)
library(lubridate)
daily_stock_returns <- function(ticker, start_year){
symbol <- getSymbols(ticker, src = 'yahoo', auto.assign = FALSE, warnings = FALSE)
data <- periodReturn(symbol, period='monthly', subset=paste(start_year, "::", sep = ""), type ='log')
colnames(data) <- as.character(ticker)
assign(ticker, data, .GlobalEnv)
}
```
Column {.tabset data-width=550}
-----------------------------------------------------------------------
### Monthly Return
```{r message = FALSE}
year <- ymd("2016-08-02")
duration <- ymd("2019-01-02")
daily_stock_returns('AMZN', year)
daily_stock_returns('GOOGL', year)
daily_stock_returns('JPM', year)
merged_returns <- merge.xts(AMZN, GOOGL, JPM)
merged_returns_percent <- merged_returns * 100
dygraph(merged_returns_percent, main = "Monthly Return") %>%
dyAxis("y", label = "Return(%)") %>%
dyOptions(colors = RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightSeriesBackgroundAlpha = 0.2,
highlightSeriesOpts = list(strokeWidth = 2)) %>%
dyRangeSelector(height = 65)
```
Column {.tabset data-width=450}
-----------------------------------------------------------------------
### AMAZON
```{r}
invisible(getSymbols("AMZN", src = "yahoo", from=duration))
AMZN_xts <- AMZN
dygraph(AMZN_xts[, -5], main = "Amazon") %>%
dyCandlestick() %>%
dyAxis("y", label="Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```
### GOOGLE
```{r}
invisible(getSymbols("GOOGL", src = "yahoo", from=duration))
GOOGL_xts <- GOOGL
dygraph(GOOGL_xts[, -5], main = "Google") %>%
dyCandlestick() %>%
dyAxis("y", label="Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```
### JP MORGAN
```{r}
invisible(getSymbols("JPM", src = "yahoo", from=duration))
JPM_xts <- JPM
dygraph(JPM_xts[, -5], main = "Twitter") %>%
dyCandlestick() %>%
dyAxis("y", label="Price") %>%
dyOptions(colors= RColorBrewer::brewer.pal(5, "Set1")) %>%
dyHighlight(highlightCircleSize = 4,
highlightSeriesOpts = list(strokeWidth = 3),
highlightSeriesBackgroundAlpha = 1) %>%
dyRangeSelector(height = 65)
```