################################
##### Wand Exercise #####
################################
library(dygraphs)
library(utils)
library(tibble)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(xts)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
# read in Data
greg <- read.csv(file = "C:/Users/justi/GitHub/MSDS_6306_Doing-Data-Science/Unit 11/Unit11TimeSeries_Gregorovitch.csv")
olli <- read.csv(file = "C:/Users/justi/GitHub/MSDS_6306_Doing-Data-Science/Unit 11/Unit11TimeSeries_Ollivander.csv")
# peek at the data
#view(greg)
#view(olli)
# check for NAs
sapply(greg, function(x) sum(is.na(x)))
## X1.1.1970 X1268
## 0 0
sapply(olli, function(x) sum(is.na(x)))
## X1.1.1970 X1345
## 0 0
# convert to xts, order by date column, but don't add the date column to the xts object, we will do that later
greg <- xts(greg[,-1], order.by = as.Date(greg[,1], "%m/%d/%Y"), unique = FALSE)
olli <- xts(olli[,-1], order.by = as.Date(olli[,1], "%m/%d/%Y"), unique = FALSE)
# peek at the data
str(greg)
## An 'xts' object on 1971-01-01/2017-01-01 containing:
## Data: int [1:47, 1] 1295 1349 1298 1493 1432 1431 1291 1247 1403 1188 ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
str(olli)
## An 'xts' object on 1971-01-01/2017-01-01 containing:
## Data: int [1:47, 1] 1304 1168 1252 1296 1458 1443 1282 1450 1338 1063 ...
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
# bind time series together
Wands <- merge.xts(greg, olli)
# Look at Wands
str(Wands)
## An 'xts' object on 1971-01-01/2017-01-01 containing:
## Data: int [1:47, 1:2] 1295 1349 1298 1493 1432 1431 1291 1247 1403 1188 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:2] "greg" "olli"
## Indexed by objects of class: [Date] TZ: UTC
## xts Attributes:
## NULL
#view(Wands)
# Function to add annotation to the shadded region of the graph done with dyShading
presAnnotation <- function(dygraph, x, text) {
dygraph %>%
dyAnnotation(x, text, attachAtBottom = TRUE, width = 100, height = 32)
}
# plot dygraph
dygraph(Wands, main = "Wand Sales by Year: Gregovitch vs. Ollivander") %>%
# Change Label names
dySeries("greg", label = "Gregovitch") %>%
dySeries("olli", label = "Ollivander") %>%
# Change axis labels
dyAxis("y", label = "Wands Sold") %>%
dyAxis("x", label = "Years") %>%
# Add Stackgraph option and set custom colors
dyOptions(includeZero = TRUE, colors = c("blue", "red"),
stackedGraph = TRUE) %>%
dyRangeSelector() %>%
# Add a shaded region to show when Voldemort was revived
dyShading(from = "1995-1-1", to = "1999-1-1", color = "orange", axis = "x") %>%
# Add a label to the shaded region
presAnnotation("1997-1-1", text = "Voldemort Revived") %>%
# Add highlighting
dyHighlight(highlightCircleSize = 5,
highlightSeriesBackgroundAlpha = 0.2,
hideOnMouseOut= FALSE) %>%
# Modify the legend
dyLegend(width = 400)