```{r setup, echo=FALSE, message=FALSE, warning=FALSE }
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE)
#knitr::opts_chunk$set(echo=TRUE, warning=TRUE, message=TRUE)
library(rmarkdown)
library(tidyverse)

#load libraries

library(ggrepel)
library(RColorBrewer)
library(ggplot2)
library(plotly)

# Load in the clean_data_all functions and establish the modified table for all countries
# This may soon be replaced with a line which loads a ready-made rdata file of these tables.
source("clean_data_all.R")

file_prefix <- params$file_prefix
written_label <- params$written_label
countries <- params$countries

# Filter by the country list
data_case <- data_case_all %>% filter(Country %in% countries)
data_death <- data_death_all %>% filter(Country %in% countries)

date_case <- get_date(data_case)
date_death <- get_date(data_death)
upper_case_limit <- get_rounded_upper(get_max(data_case))
upper_death_limit <- get_rounded_upper(get_max(data_death))

```

These graphs describe the development of COVID-19  by confirmed cases and deaths in `r written_label`, using data from the [Johns Hopkins coronavirus map](https://coronavirus.jhu.edu/map.html). The original model for these graphs was made by [\@ryancbriggs](https://twitter.com/ryancbriggs) to create graphs for Canada's provinces and territories; it has been adapted by [\@rmartinnielsen](https://twitter.com/rmartinnielsen) to do `r written_label` instead. You can also review the [Rmarkdown source for this page](index.Rmd).

# Graphs {.tabset}

Data current as of `r date_case`.

## Confirmed cases
```{r Confirmed cases}

p <- ggplot(data_case %>% filter(Count > 1),
  aes(x = Date, y = Count, color = Country, label = end_label, text = "")) + 
  geom_line(size = 1.25) + 
  scale_y_continuous(trans = "log10", breaks = sort(c(2*10^seq(0,6),5*10^seq(0,6),10^seq(0,6)))) + 
  annotation_logticks() +
  scale_x_date(date_breaks = "14 days", date_minor_breaks = "7 days", date_labels = "%b %d") +
  labs(x = "", 
       y = "Cumulative confirmed cases",
       title = paste("COVID-19 Confirmed Cases in", written_label),
       subtitle = paste("Current as of", date_case),
       caption = "Data from Johns Hopkins: https://coronavirus.jhu.edu/map.html") + 
  scale_color_brewer(name = "", guide = F, type = "qual") + 
  theme_minimal() +
  geom_text_repel()
# Make a png of the static file
ggsave(paste(file_prefix, "Dates_cases.png", sep="_"), width = 8, height = 5)

# Refigure for ggplotly use

fig <- ggplotly(p) 

text_y <- vector(length=length(countries))

for (i in 1:length(countries)) {
  text_y <- paste(
    "Cases: <b>", as.character(10^(fig$x$data[[i]]$y)),
    "</b><br>Date: ", format(as.Date(fig$x$data[[i]]$x, origin="1970-01-01"), "%B %d, %Y")
  )
  fig <- fig %>% style ( text = text_y, traces = i )
}

fig
```

## Confirmed cases since 10
```{r Cases since 10}
left_label_lim <- c(0, 8)
right_label_lim <- c(20,NA)
  
p <- ggplot(data_case %>%
    filter(Days_Count10 != 0 & Count > 0) %>% 
    mutate(slope_Day2 = if_else((slope_Day2<upper_case_limit), slope_Day2, NA_real_)) %>%
    mutate(slope_label2 = if_else((slope_Day2 == 640) & (Country == countries[1]), as.character("Doubles every 2 days"), NA_character_)) %>%
    mutate(slope_label3 = if_else((slope_Day3 == 320) & (Country == countries[1]), as.character("Doubles every 3 days"), NA_character_)) %>%
    mutate(slope_label7 = if_else((slope_Day7 == 80) & (Country == countries[1]), as.character("Doubles every 7 days"), NA_character_)),
    aes(x = Days_Count10-1, y = Count, color = Country, label = end_label)) + 
      geom_line( color = "gray", linetype = 2, aes(x = Days_Count10-1, y = slope_Day2)) +
      geom_line( color = "gray", linetype = 2, aes(x = Days_Count10-1, y = slope_Day3)) + 
      geom_line( color = "gray", linetype = 2, aes(x = Days_Count10-1, y = slope_Day7)) + 
      geom_line(size = 1.25) +
      geom_label_repel( color = "gray", aes(x = Days_Count10-1, y= slope_Day2, label =slope_label2),
                        arrow = arrow(length = unit(0.01, "npc"), type = "open", ends = "last"), box.padding = 1, xlim=left_label_lim ) +
      geom_label_repel( color = "gray", aes(x = Days_Count10-1, y= slope_Day3, label =slope_label3),
                        arrow = arrow(length = unit(0.01, "npc"), type = "open", ends = "last"), box.padding = 1, xlim=left_label_lim  ) +
      geom_label_repel( color = "gray", aes(x = Days_Count10-1, y= slope_Day7, label =slope_label7),
                        arrow = arrow(length = unit(0.01, "npc"), type = "open", ends = "last"), box.padding = 1, xlim=right_label_lim ) +
  geom_line(size = 1.25) +
  scale_y_continuous(trans = "log10", limits = c(10,upper_case_limit), breaks = sort(c(2*10^seq(1,6),5*10^seq(1,6),10^seq(1,6)))) + 
  annotation_logticks(sides="lr") +
  scale_x_continuous(breaks=seq(0,150, 10)) +
  labs(x = "Days since 10th case in Country", 
       y = "Cumulative confirmed cases",
       title = paste("COVID-19 Confirmed Cases in", written_label),
       subtitle = paste("Current as of", date_case),
       caption = #"Dashed lines indicate doubling in 2, 3, 4 and 6 days.\n"
       "Data from Johns Hopkins: https://coronavirus.jhu.edu/map.html") + 
  scale_color_brewer(name = "", guide = F, type = "qual") + 
  geom_text_repel(nudge_x = 1.1, nudge_y = 0.1) +
  theme_minimal() 

ggsave(paste(file_prefix, "Since10_cases.png", sep="_"), width = 8, height = 5)

fig <- ggplotly(p)

fig <- fig %>% 
  style(text = "Doubles every 2 days", traces = 1) %>%   
  style(text = "Doubles every 3 days", traces = 2) %>%   
  style(text = "Doubles every 7 days", traces = 3)

text_y <- vector(length=length(countries))
label_ranges <- seq(4,3+length(countries))

for (i in label_ranges) {
  text_y <- paste(
    "Cases: <b>", as.character(round(10^(fig$x$data[[i]]$y))),
    "</b><br>Days since 10: ", as.character(fig$x$data[[i]]$x)
  )
  fig <- fig %>% style ( text = text_y, traces = i )
}

fig

```

## Confirmed cases since 100
```{r Cases since 100}
p <- ggplot(data_case %>%
              filter(Days_Count100 > 0 & Count > 0) %>% 
              mutate(slope_Day3 = if_else((slope_Day3<upper_case_limit), slope_Day3, NA_real_)) %>%
              mutate(slope_Day7 = if_else((slope_Day7<upper_case_limit), slope_Day7, NA_real_)) %>%
              mutate(slope_label3 = if_else((slope_Day3_100 == 800) & (Country == countries[1]), as.character("Doubles every 3 days"), NA_character_)) %>%
              mutate(slope_label7 = if_else((slope_Day7_100 == 400) & (Country == countries[1]), as.character("Doubles every 7 days"), NA_character_)),
  aes(x = Days_Count100-1, y = Count, color = Country, label = end_label)) + 
  geom_line( color = "gray", linetype = 2, aes(x = Days_Count100-1, y = slope_Day3_100)) + 
  geom_line( color = "gray", linetype = 2, aes(x = Days_Count100-1, y = slope_Day7_100)) + 
  geom_line(size = 1.25) +
  geom_label_repel( color = "gray", aes(x = Days_Count100-1, y= slope_Day3_100, label =slope_label3),
                    arrow = arrow(length = unit(0.01, "npc"), type = "open", ends = "last"), box.padding = 1, xlim=left_label_lim  ) +
 geom_label_repel( color = "gray", aes(x = Days_Count100-1, y= slope_Day7_100, label =slope_label7),
                    arrow = arrow(length = unit(0.01, "npc"), type = "open", ends = "last"), box.padding = 1, xlim=right_label_lim ) +
  geom_line(size = 1.25) +
  scale_y_continuous(trans = "log10", limits = c(100,upper_case_limit), breaks = sort(c(2*10^seq(2,6),5*10^seq(2,6),10^seq(2,6)))) + 
  annotation_logticks(sides="lr") +
  scale_x_continuous(breaks=seq(0,150, 10)) +
  labs(x = "Days since 100th case in Country", 
       y = "Cumulative confirmed cases",
       title = paste("COVID-19 Confirmed Cases in",written_label),
       subtitle = paste("Current as of", date_case),
       caption = #"Dashed lines indicate doubling in 2, 3, 4 and 6 days.\n"
         "Data from Johns Hopkins: https://coronavirus.jhu.edu/map.html") + 
  scale_color_brewer(name = "", guide = F, type = "qual") + 
  geom_text_repel(nudge_x = 1.1, nudge_y = 0.1) +
  theme_minimal() 

ggsave(paste(file_prefix, "Since100_cases.png", sep="_"), width = 8, height = 5)

fig <- ggplotly(p) %>%
  style(text = "Doubles every 3 days", traces = 1) %>%   
  style(text = "Doubles every 7 days", traces = 2)

text_y <- vector(length=length(countries))
label_ranges <- seq(3,2+length(countries))

for (i in label_ranges) {
  text_y <- paste(
    "Cases: <b>", as.character(round(10^(fig$x$data[[i]]$y))),
    "</b><br>Days since 100: ", as.character(fig$x$data[[i]]$x)
  )
  fig <- fig %>% style ( text = text_y, traces = i )
}

fig

```

## Deaths
```{r Deaths}

p <- ggplot(data_death %>% filter(Count != 0) ,
       aes(x = Date, y = Count, color = Country, label = end_label)) + 
  geom_line(size = 1.25) + 
  
  scale_y_continuous(trans = "log10", breaks = sort(c(2*10^seq(0,4),5*10^seq(0,4),
                                                      10^seq(0,4)))) + 
  scale_x_date(date_breaks = "14 days", date_minor_breaks = "7 days", date_labels = "%b %d") +
  
  labs(x = "", 
       y = "Cumulative deaths per Country",
       title = paste("COVID-19 Deaths in", written_label),
       subtitle = paste("Current as of", date_death),
       caption = "Data from Johns Hopkins: https://coronavirus.jhu.edu/map.html") + 
  scale_color_brewer(name = "", guide = F, type = "qual") + 
  geom_text_repel() +
  theme_minimal() 

ggsave(paste(file_prefix, "Dates_deaths.png", sep="_"), width = 8, height = 5)

# Refigure for ggplotly use

fig <- ggplotly(p) 

text_y <- vector(length=length(countries))

for (i in 1:length(countries)) {
  text_y <- paste(
    "Deaths: <b>", as.character(round(10^(fig$x$data[[i]]$y))),
    "</b><br>Date: ", format(as.Date(fig$x$data[[i]]$x, origin="1970-01-01"), "%B %d, %Y")
  )
  fig <- fig %>% style ( text = text_y, traces = i )
}

fig
```

## Deaths since first death
```{r Death since first}

  p <- ggplot(data_death %>% filter(Days_Count1 != 0) %>%
              mutate(slope_Day3_1 = if_else((slope_Day3_1<upper_death_limit), slope_Day3_1, NA_real_)) %>%
              mutate(slope_Day7_1 = if_else((slope_Day7_1<upper_death_limit), slope_Day7_1, NA_real_)) %>%
              mutate(slope_label3 = if_else((slope_Day3_1 == 16) & (Country == countries[1]), as.character("Doubles every 3 days"), NA_character_)) %>%
              mutate(slope_label7 = if_else((slope_Day7_1 == 4) & (Country == countries[1]), as.character("Doubles every 7 days"), NA_character_)),
         aes(x = Days_Count1-1, y = Count, color = Country, label = end_label)) + 
  geom_line(size = 1.25) +
  geom_line( color = "gray", linetype = 2, aes(x = Days_Count1-1, y = slope_Day3_1)) + 
  geom_line( color = "gray", linetype = 2, aes(x = Days_Count1-1, y = slope_Day7_1)) + 
  geom_line(size = 1.25) +
  geom_label_repel( color = "gray", aes(x = Days_Count1-1, y= slope_Day3_1, label =slope_label3),
                    arrow = arrow(length = unit(0.01, "npc"), type = "open", ends = "last"), box.padding = 1, xlim=left_label_lim  ) +
 geom_label_repel( color = "gray", aes(x = Days_Count1-1, y= slope_Day7_1, label =slope_label7),
                    arrow = arrow(length = unit(0.01, "npc"), type = "open", ends = "last"), box.padding = 1, xlim=right_label_lim ) +
  #scale_y_continuous(trans = "log10", breaks = 10^seq(0, 4)) + 
  scale_y_continuous(trans = "log10", breaks = sort(c(2*10^seq(0,4),5*10^seq(0,4), 10^seq(0,4)))) + 
  scale_x_continuous(breaks=seq(0,150, 10)) +
  labs(x = "Days since first death in Country", 
       y = "Cumulative deaths per Country",
       title = paste("COVID-19 Deaths in", written_label),
       subtitle = paste("Current as of", date_death),
       caption = "Data from Johns Hopkins: https://coronavirus.jhu.edu/map.html") + 
  scale_color_brewer(name = "", guide = F, type = "qual") + 
  geom_text_repel() +
  theme_minimal()
ggsave(paste(file_prefix, "Since1_deaths.png", sep="_"), width = 8, height = 5)

# Refigure for ggplotly use

fig <- ggplotly(p) %>%
  style(text = "Doubles every 3 days", traces = length(countries)+1) %>%   
  style(text = "Doubles every 7 days", traces = length(countries)+2)
text_y <- vector(length=length(countries))

for (i in 1:length(countries)) {
  text_y <- paste(
    "Deaths: <b>", #as.character(fig$x$data[[i]]$y)," ", 
    as.character(round(10^(fig$x$data[[i]]$y))),
    "</b><br>Days since first death: ", as.character(fig$x$data[[i]]$x)
  )
  fig <- fig %>% style ( text = text_y, traces = i )
}

fig
```
