forked from jtr13/cc21fall2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intro_to_dygraph.Rmd
116 lines (88 loc) · 4.18 KB
/
intro_to_dygraph.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# An Introduction to Dygraph in R
Yanyun Chen
```{r, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```
### These are the codes demonstrated in the video tutorial. The video tutorial can be found at https://www.youtube.com/watch?v=ewQdzu47yZs
***Data Source: FRED, https://fred.stlouisfed.org/categories/9?t=cpi&ob=pv&od=desc#***
```{r library}
library(readxl)
library(dygraphs)
library(dplyr)
```
### Project Description
The main motivation of this project is the ubiquity of time series data. One thing I
noticed in my past internship and project experience is that the need for exploratory
analysis and visualization of such data is high, but people are less familiar with it than
other types of data. Moreover, the r package dygraphs provides much easier and
intuitive functionality to draw time series data when compared to ggplot or plotly (which
were introduced in class), and it additionally has interactive features that are useful for
long-term time series data. I personally think the video tutorial and example codes are
organized and rendered in a clean and friendly way that anyone with some r
background would understand and learn. During the making process, I’ve also
benefitted a lot from exploring this package, especially plotting prediction range and
actual data in such a quick and easy way. However, what I might do differently next
time is to pay more attention to the best practices of visualizing time series data, such
as not filling the area.
### The Basic
```{r intro}
cpi_data <- read_excel("resources/intro_to_dygraph/cpi.xlsx", col_types = c("date",
"numeric", "numeric"))
colnames(cpi_data) <- c("date","us","china")
us_cpi <- ts(cpi_data$us, frequency=12, start=c(1999,9))
dygraph(us_cpi)
```
**Note**: In the video, fill is demonstrated. But filling time series graph is generally not recommended.
```{r selector,title,label}
dygraph(us_cpi, main = "CPI, not seasonally adjusted: All Items for United States") %>%
dySeries("V1", label = "United States CPI",color='red') %>%
dyAxis("y", label = "Index (2015=100)", valueRange = c(50, 120)) %>%
dyOptions(axisLineWidth = 1.5,
drawGrid = FALSE) %>%
dyRangeSelector(height = 20) %>%
dyLegend(show = "follow")
```
### Multiple Series
```{r multilple}
cpi <- ts(cpi_data[,-1], frequency=12, start=c(1999,9))
dygraph(cpi)
```
```{r multilple/options}
dygraph(cpi,main="CPI, not Seasonally Adjusted: All Items") %>%
dySeries("us", label = "United States",color='red') %>%
dySeries("china", label = "China",color='blue') %>%
dyAxis("y", label = "Index (2015=100)") %>%
dyOptions(axisLineWidth = 1.5, drawGrid = TRUE) %>%
dyRangeSelector(height = 20)
```
### Events
**Note**: In the video, drawing event line along with shaded region is demonstrated. Another option is to draw annotation along with shaded region, which is demonstrated below.
```{r events}
dygraph(cpi, main="CPI, not Seasonally Adjusted: All Items") %>%
dySeries("us", label = "United States") %>%
dySeries("china", label = "China") %>%
dyRangeSelector(height = 20) %>%
dyEvent("2019-12-01", "COVID-19", labelLoc = "bottom") %>%
dyAnnotation("2008-05-01", "2008 Financial Crisis", attachAtBottom = TRUE, width = 150) %>%
#dyEvent("2008-05-01", "2008 Financial Crisis", labelLoc = "bottom") %>%
dyShading(from = "2008-01-01", to = "2008-12-01")
```
### Prediction
```{r upper/lower}
china_cpi <- ts(cpi_data$china, frequency=12, start=c(1999,9))
hw <- HoltWinters(china_cpi)
predicted <- predict(hw, n.ahead = 36, prediction.interval = TRUE)
all <- cbind(china_cpi, predicted)
dygraph(all, "CPI, not Seasonally Adjusted: All Items for China") %>%
dySeries("china_cpi", label = "Actual") %>%
dySeries(c("predicted.lwr", "predicted.fit", "predicted.upr"), label = "Predicted")
```
```{r upper/lower/more}
china_cpi_200 <- ts(cpi_data$china[1:200], frequency=12, start=c(1999,9))
hw <- HoltWinters(china_cpi_200)
predicted <- predict(hw, n.ahead = 64, prediction.interval = TRUE)
all <- cbind(china_cpi, predicted)
dygraph(all, "CPI, not Seasonally Adjusted: All Items for China") %>%
dySeries("china_cpi", label = "Actual") %>%
dySeries(c("predicted.lwr", "predicted.fit", "predicted.upr"), label = "Predicted")
```