---
title: "Dygraphs of total all cause mortality by region from the ONS"
author: "Duncan Golicher"
date: "01/03/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,message=FALSE,warning = FALSE)
library(tidyverse)
library(xlsx)
library(dygraphs)
library(xts)
library(lubridate)

```


## Countries 1

https://www.mortality.org/Public/STMF/Outputs/stmf.csv

```{r}
#d<-read.csv("https://www.mortality.org/Public/STMF/Outputs/stmf.csv",skip=1)
#save(d, file="stmfdata2.rob")
load("stmfdata2.rob")
filter(d, Year >1995) -> d
d$date<-paste(d$Year,d$Week,1,sep="-")
d$date<-as.Date(d$date, "%Y-%U-%u")
d1<-d
d %>% filter(Sex=="b") %>% select("date", "RTotal", "CountryCode") ->d
d<-na.omit(d)
d %>% mutate(RTotal=RTotal*100)->d
```

```{r}
d %>% filter(CountryCode %in% c("SWE","GBRTENW", "GBR_SCO", "BEL", "ITA", "ESP" )) %>%   pivot_wider(names_from="CountryCode", values_from = "RTotal") ->dd
dd<-xts(x = dd[,-1], order.by = dd$date)
dygraph(dd,group="deaths",main = "Age adjusted mortality rate", ylab = "Age adjusted mortality rate" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

## Countries2

https://www.mortality.org/Public/STMF/Outputs/stmf.csv

```{r}
d %>% filter(CountryCode %in% c("DEUTNP","DNK", "NOR", "FIN", "NLD", "LUX", "EST" )) %>%   pivot_wider(names_from="CountryCode", values_from = "RTotal") ->dd
dd<-xts(x = dd[,-1], order.by = dd$date)
dygraph(dd,group="deaths",main = "Age adjusted mortality rate", ylab = "Age adjusted mortality rate" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

## Countries3

https://www.mortality.org/Public/STMF/Outputs/stmf.csv

```{r}
d %>% filter(CountryCode %in% c("KOR","FRATNP", "CAN", "USA", "NZL_NP", "POL", "ISL" )) %>%   pivot_wider(names_from="CountryCode", values_from = "RTotal") ->dd
dd<-xts(x = dd[,-1], order.by = dd$date)
dygraph(dd,group="deaths",main = "Age adjusted mortality rate", ylab = "Age adjusted mortality rate" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

## Deaths  by age group {.tabset}

### USA

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="USA") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(6:9)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths by age group", ylab = "Deaths by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### England and Wales

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="GBRTENW") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(6:9)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths by age group", ylab = "Deaths by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Scotland

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="GBR_SCO") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(6:9)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths by age group", ylab = "Deaths by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```



### Sweden

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="SWE") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(6:9)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths by age group", ylab = "Deaths by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Poland

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="POL") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(6:9)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths by age group", ylab = "Deaths by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Germany

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="DEUTNP") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(6:9)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths by age group", ylab = "Deaths by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Bulgaria

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="BGR") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(6:9)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths by age group", ylab = "Deaths by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```


### Italy

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="ITA") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(6:9)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths by age group", ylab = "Deaths by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Spain

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="ESP") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(6:9)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths by age group", ylab = "Deaths by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Israel

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="ISR") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(6:9)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths by age group", ylab = "Deaths by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

## Death rate by age group {.tabset}

### USA

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="USA") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(12:15)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Deaths rate by age group", ylab = "Deathrate by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### England and Wales

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="GBRTENW") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(12:15)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Death rate by age group", ylab = "Death rate by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Scotland

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="GBR_SCO") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(12:15)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Death rate by age group", ylab = "Death rate by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```



### Sweden

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="SWE") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(12:15)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Death rate by age group", ylab = "Death rate by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Poland

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="POL") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(12:15)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Death rate by age group", ylab = "Death rate by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Germany

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="DEUTNP") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(12:15)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Death rate by age group", ylab = "Death rate by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Bulgaria

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="BGR") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(12:15)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Death rate by age group", ylab = "Death rate by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```


### Italy

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="ITA") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(12:15)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Death rate by age group", ylab = "Death rate by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Spain

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="ESP") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(12:15)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Death rate by age group", ylab = "Death rate by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```

### Israel

```{r}

d1 %>% filter(Sex=="b") %>% filter(CountryCode =="ISR") %>% filter(!is.na(date))->dd
dd<-xts(x = dd[,c(12:15)], order.by = dd$date)
dygraph(dd,group="deaths",main = "Death rate by age group", ylab = "Death rate by age group" ) %>% dyLegend(width = 600) %>% dyRoller(rollPeriod = 4) %>% dyRangeSelector(dateWindow = c("2010-01-01", "2021-03-01"))
```



