---
title: "Visualizing Time Series Data"
author: "Thiyanga S. Talagala"
date: ""
output:
  xaringan::moon_reader:
    chakra: libs/remark-latest.min.js
    lib_dir: libs
    css: 
      - default
      - default-fonts
      - duke-blue
      - hygge-duke
      - libs/cc-fonts.css
      - libs/figure-captions.css
      - xaringan-themer.css
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
      ratio: '16:9'
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
```

```{r xaringan-themer, include=FALSE, warning=FALSE}
library(xaringanthemer)
style_mono_accent(
  base_color = "#081d58",
  text_bold_color = "#ce1256",
  title_slide_text_color = "#edf8b1",
  header_font_google = google_font("Josefin Sans"),
  base_font_size = "20px",
  text_font_size = "1.5rem"
 #text_font_google   = google_font("Montserrat", "300", "300i")
 # code_font_google   = google_font("Fira Mono")
)
```


### R packages need for time series visualization

```{r, message=FALSE, warning=FALSE}
library(tsibble)
library(lubridate)
library(tidyverse)
```

---
# Data package

```{r}
# install.packages("devtools")
#devtools::install_github("thiyangt/denguedatahub")
library(denguedatahub)
```

More information: https://denguedatahub.netlify.app/
---

# Data sets in `denguedatahub`

```{r}
vcdExtra::datasets("denguedatahub")
```

---
class: inverse, center, middle

# Univariate Time Series Visualization

---
# `srilanka_weekly_data`

```{r}
srilanka_weekly_data
```

---

# Filter Colombo data

```{r}
colombodf <- srilanka_weekly_data |>
  filter(district == "Colombo")
colombodf
```

---
## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_point()
```

]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases))+geom_point()
```

]

---

## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_point()
```


What limitations or shortcomings do you observe in its current presentation?

]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases))+geom_point()
```

]

---


## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_point()
```

How can the graph be further improved?


]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases))+geom_point()
```

]

---



## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_point()
```


]

.pull-right[
```{r, echo=FALSE, fig.width=10}
ggplot(data=colombodf, aes(x=start.date, y=cases))+geom_point()
```

]

---


## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line()
```

We can connect neighbouring points with lines to emphasise the time dependent relationship.

]

.pull-right[
```{r, echo=FALSE, fig.width=10}
ggplot(data=colombodf, aes(x=start.date, y=cases))+geom_line()
```

]

---



## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line()
```

- Lines do not represent actual data. 

- Lines are used to guide the eye.

- It is not appropriate to connect points with lines if the observed values are far apart or unevenly distributed.

]

.pull-right[
```{r, echo=FALSE, fig.width=10}
ggplot(data=colombodf, aes(x=start.date, y=cases))+geom_line()
```

]

---


## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line() + 
  geom_point()
```


]

.pull-right[
```{r, echo=FALSE, fig.width=10}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + 
  geom_line() + 
  geom_point()
```

]


---

** Which is the best option? `Lines only` or `Points and Lines both`**

.pull-left[
```{r, echo=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line() 
```


]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + 
  geom_line() + 
  geom_point()
```

]
---
## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_area() 
```


]

.pull-right[
```{r, echo=FALSE, fig.width=10}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + 
  geom_area() 
```

]

---
## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_area() 
```

- Distinguishes the area above and below the curve visually.

-  More focus is given to the overarching trend in the series.  

- This visualisation works only if the y-axis is set to zero.

]

.pull-right[
```{r, echo=FALSE, fig.width=10}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + 
  geom_area() 
```

]

---

## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line()
```

How can the graph be further improved?


]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases))+geom_line()
```

]

---
## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line()
```



]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases))+geom_line()
```

]

---
## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line() +
*  scale_x_date(date_breaks = "1 year")
```



]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + geom_line() +  scale_x_date(date_breaks = "1 year")
```

]

---
## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line() +
  scale_x_date(date_breaks = "1 year") +
*  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
```



]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + geom_line() +  scale_x_date(date_breaks = "1 year") + 
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
```

]

---
## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line() +
*  scale_x_date(date_breaks = "1 month") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
```



]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + geom_line() +  scale_x_date(date_breaks = "1 month") + 
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
```

]

---
## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line() +
*  scale_x_date(date_breaks = "1 year") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
```



]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + geom_line() +  scale_x_date(date_breaks = "1 year") + 
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
```

]

---
## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line() +
*  scale_x_date(date_breaks = "1 year") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
```



]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + geom_line() +  scale_x_date(date_breaks = "1 year",
                                                                                 date_labels = "%b-%y") + 
theme(axis.text.x = element_text(angle = 90, 
                                 vjust = 0.5, hjust=1))
```

]

---

## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line() +
*  scale_x_date(date_breaks = "1 year",  date_labels = "%b-%y") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + xlab("Time") + ylab("Dengue Cases")
```



]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + geom_line() +  scale_x_date(date_breaks = "1 year",
                                                                                 date_labels = "%b-%y") + 
theme(axis.text.x = element_text(angle = 90, 
                                 vjust = 0.5, hjust=1)) + xlab("Time") + ylab("Dengue Cases")
```

]


---

## start.data vs cases

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, 
       aes(x=start.date, y=cases)) +
  geom_line() +
*  scale_x_date(date_breaks = "1 year", date_labels = "%Y-%m-%d") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + xlab("Time") + ylab("Dengue Cases")
```



]

.pull-right[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + geom_line() +  scale_x_date(date_breaks = "1 year",
                                                                                 date_labels = "%Y-%m-%d") + 
theme(axis.text.x = element_text(angle = 90, 
                                 vjust = 0.5, hjust=1)) + xlab("Time") + ylab("Dengue Cases")
```

]

---

```{r, echo=FALSE, fig.width=15}
ggplot(data=colombodf, aes(x=start.date, y=cases)) + geom_line() +  scale_x_date(date_breaks = "1 year",
                                                                                 date_labels = "%Y-%m-%d") + 
theme(axis.text.x = element_text(angle = 90, 
                                 vjust = 0.5, hjust=1)) + xlab("Time") + ylab("Dengue Cases")
```

--

What are the key takeaways from this chart?

---
class: inverse, middle, center

# Formatting time axis

---

# Filter 2019 data for Colombo district

```{r}
colombo2019 <-  srilanka_weekly_data |>
  filter(district == "Colombo", year==2019)
colombo2019
```

---
class: middle, centre, inverse

# Your turn: Obtain the following chart

```{r, echo=FALSE, fig.width=15}
base <- ggplot(data=colombo2019,
               aes(x=start.date, y=cases)) + geom_line() + scale_x_date(date_breaks = "1 month") + 
   theme(axis.text.x = element_text(angle = 90,  vjust = 0.5, hjust=1)) +  xlab("Month") + ylab("Dengue Cases") 
base
```

---
.pull-left[
```{r}
base
```

]

.pull-right[

- How can the plot be improved?


]

---

```{r, fig.width=15}
base + geom_point()
```

---
class: middle, center

# Breaks: `date_breaks`

---

```{r, fig.width=15}
base + geom_point() +  scale_x_date(date_breaks = "2 months")
```

---
class: middle,  center

# Minor breaks: `date_minor_breaks`
---

.pull-left[
```{r, message=FALSE, warning=FALSE}
base + geom_point() +  scale_x_date(date_breaks = "1 month")
```
]

.pull-right[
```{r, message=FALSE, warning=FALSE}
base + geom_point() +  scale_x_date(date_breaks = "1 month", date_minor_breaks = "1 week")
```
]

---
class:  middle, center

# Labels: `date_labels`

---

```{r, message=FALSE, warning=FALSE, fig.width=15}
base + geom_point() +  scale_x_date(date_breaks = "1 month", date_minor_breaks = "1 week",
date_labels = "%b")
```

---

.pull-left[
|Label | Description|
|---|---|
|`%S` | seconds (00-59)|
|`%M`| minutes (00-59)|
|`%l`| hour in 12-hour clock, 1, 2, 3..12|
|`%I`| hour in 12-hour clock, 01, 02,...|
|`%p`|am\_pm|
|`%H`| hour in 24-hour clock|
|`%a`|day of week, Mon, Tue,..|
|`%A`|full name of day of week|

]

.pull-right[

|Label | Description|
|---|---|
|`%e` | day of month 1, 2, 3 |
|`%d` | day of month 01, 02, 03 |
|`%m`|month 1, 2, 3,..|
|`%b`|month, Jan, Feb..|
|`%B`|full name of month|
|`%y`|year, 01, 02,..|
|`%Y`|year, 2001, 2002,..|

]
---
class: middle,inverse, center

## Your turn: Modify the chart as follows

```{r, message=FALSE, warning=FALSE, fig.width=15, echo=FALSE}
base + geom_point() +  scale_x_date(date_breaks = "1 month", date_minor_breaks = "1 week",
date_labels = "%B")
```

---

### Axis label modifications

.pull-left[
```{r, message=FALSE, warning=FALSE, eval=FALSE}
base + 
  geom_point() +  
  scale_x_date(
    date_breaks = "1 month",
    date_minor_breaks = "1 week",
    date_labels = "%b %y")
```
]

.pull-right[

```{r, message=FALSE, warning=FALSE, echo=FALSE}
base + geom_point() +  scale_x_date(date_breaks = "1 month", date_minor_breaks = "1 week",
date_labels = "%b %y")
```

]

---
### Axis limits

.pull-left[
```{r, message=FALSE, warning=FALSE, eval=FALSE}
lim <- as.Date(c("2019-01-01", "2019-12-31"))
base + 
  geom_point() + 
  scale_x_date(
    limits=lim,
    date_breaks = "1 month",
    date_minor_breaks = "1 week",
    date_labels = "%b %y")
```

]



.pull-right[
```{r, message=FALSE, warning=FALSE, echo=FALSE}
lim <- as.Date(c("2019-01-01", "2019-12-30"))
base + geom_point() +  scale_x_date(limits=lim, date_breaks = "1 month", date_minor_breaks = "1 week",
date_labels = "%b %y")
```

]

---

## Modify axis labels

.pull-left[
```{r, message=FALSE, warning=FALSE, eval=FALSE}
base + 
  geom_point() + 
  scale_x_date(
    limits=lim,
    date_breaks = "1 month",
    date_minor_breaks = "1 week",
    date_labels = "%B\n%Y")
```

]

.pull-right[

```{r, message=FALSE, warning=FALSE, echo=FALSE}
base + 
  geom_point() + 
  scale_x_date(limits=lim,
               date_breaks = "1 month", date_minor_breaks = "1 week",
date_labels = "%B\n%Y")
```


]

---

## Modify axis labels

.pull-left[
```{r, message=FALSE, warning=FALSE, eval=FALSE}
base + 
  geom_point() + 
  scale_x_date(
    limits=lim,
    date_breaks = "1 month",
    date_minor_breaks = "1 week",
    labels = scales::label_date("%B\n%Y"))
```

]

.pull-right[
```{r, message=FALSE, warning=FALSE, echo=FALSE}
base + 
  geom_point() + 
  scale_x_date(
    limits=lim,
    date_breaks = "1 month",
    date_minor_breaks = "1 week",
    labels = scales::label_date("%B\n%Y"))
```

]

---

## Modify axis labels

.pull-left[
```{r, message=FALSE, warning=FALSE, eval=FALSE}
base + 
  geom_point() + 
  scale_x_date(
    limits=lim,
    date_breaks = "1 month",
    date_minor_breaks = "1 week",
    labels = scales::label_date_short())
```

]

.pull-right[
```{r, message=FALSE, warning=FALSE, echo=FALSE}
base + 
  geom_point() + 
  scale_x_date(
    limits=lim,
    date_breaks = "1 month",
    date_minor_breaks = "1 week",
    labels = scales::label_date_short())
```

]

---
class: inverse, middle, center

# Grouping

---
# Seasonal plots

.pull-left[
```{r, eval=FALSE}
ggplot(data=colombodf, aes(x=as.factor(week),
y=cases, group=year)) 
+ geom_line()
```

]

.pull-left[
```{r, echo=FALSE}
ggplot(data=colombodf, aes(x=as.factor(week), y=cases, group=year)) + geom_line()
```

]

---
# Seasonal plots

.pull-left[
```{r, eval=FALSE}
library(viridis)
ggplot(data=colombodf, aes(x=as.factor(week),
y=cases, group=year, col=year)) 
+ geom_line() + 
  scale_color_viridis()
```

]

.pull-left[
```{r, echo=FALSE, message=FALSE, warning=FALSE}
library(viridis)
ggplot(data=colombodf, aes(x=as.factor(week), y=cases, group=year, col=year)) + geom_line() + scale_color_viridis()
```

]

---
class: inverse, center, middle

.pull-left[
```{r, echo=FALSE, message=FALSE, warning=FALSE}
ggplot(data=colombodf, aes(x=as.factor(week), y=cases, group=year, col=year)) + geom_line() + scale_color_viridis(option="C")
```
]

.pull-right[

Task 1: Level up the plot.

Task 2: Draw some other alternatives to show the seasonal behaviour.

]
