---
title: "ghrexplore_laos"
output:
  html_document: default
  pdf_document:
    latex_engine: xelatex
date: "2025-02-20"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Load

```{r load packages, warning=FALSE, message=FALSE, echo=FALSE }
## Load packages ----
packages <- c("ggplot2", "dplyr", "tidyr", "lubridate", "sf","scales", 
              "cowplot",
              "grDevices",
              "magrittr",
              "rlang")

invisible(lapply(packages, require, character.only = TRUE))  # Suppress output

## Load GHRexplore package ----

path_to_package <- "/home/akawieck/Documents/projects/IDExtremes/GHRexplore/ghrexplore_0.0.0.9000.tar.gz"

install.packages(path_to_package , 
                 repos = NULL,
                 type = "source",
                 INSTALL_opts = c("--no-multiarch", "--no-test-load"))


library(ghrexplore)

```


```{r load data}
## Load Harmonized Data ----
data <- read.csv("harmonized_data.csv")
head(data)

## Load shapefile
laos<- st_read("gadm41_LAO1_cleaned.shp")

ggplot() +
  geom_sf(data = laos)


```


```{r calculate pseudo-anomaly}
# Calculate Pseudo-anomaly ----

data <- data %>%
  group_by(month, province) %>%
  mutate(tas.avg = mean(tas, na.rm = TRUE), 
         tasmax.avg = mean(tasmax, na.rm = TRUE),
         tasmin.avg = mean(tasmin, na.rm = TRUE),
         prlr.avg = mean(prlr, na.rm = TRUE)) %>%
  mutate(tas.diff = tas - tas.avg, 
         tasmax.diff = tasmax - tasmax.avg,
         tasmin.diff = tasmin - tasmin.avg,
         prlr.diff = prlr - prlr.avg) %>%
  ungroup() %>%
  # person time = 100000 person-month
  mutate(dengue_incidence=(dengue_cases / population) * 100000 ) # calculate incidence

  
```

# GHRexplore description

## GHRexplore plot functions: 
 
### plot_correlation
### plot_bivariate
### plot_timeseries
### plot_heatmap
### plot_seasonality
### plot_map

## GHRexplore plot management functions: 

### plot_multiple
Outputs a list with multiple plots, each representing one variable. Variable names and palette can be customized for each plot, otherwise parameters will be the same for each variable plotted (options depend on the plot type chosen). Returns a list of plots, each list element named after the variable plotted

 *plot_function* indicates which of the ghr plot types to use
Options are: 'plot_timeseries', 'plot_heatmap', 'plot_seasonality',
 'plot_bivariate', 'plot_map'.
 
 *data* an object of class "data.frame" containing covariates or cases 
 
 *var* the name of the variables identifying the covariates to be plotted. 
 Must be a character vector except if plot_function = plot_bivariate, 
 in which case it must be a list of 2 character vectors (a list of variable pairs). 

 
### plot_combine

Combines plots, each representing one variable, into a single plot.
This function takes any input from the cowplot::plot_grid function to customize
the organization of the plots. To customize organization of a common plot legend 
to the combined plots, use parameters with the suffix "_l".

 *plot_list* requires as input a list of plots
 
 *combine_legend* logical. If TRUE, assumes the legend of all plots is the same
 as the legend of the first plot in plot_list and final plot shows only one
 instance of the common legend. Default is FALSE.
 
 *combine_xaxis* logical. If TRUE, removes x axis labels from all but the last
 plot. Default is FALSE.
 
 *ncol* (from cowplot) number of colums in the plot grid. Default 1.
 
 *align* (from cowplot) specifies how plots should be aligned.
 Options are "none", "hv" (align in both directions), "h", and "v" (default). 
 
 *ncol_l* when combine_legend = TRUE. 
 Number of colums in which to align plots and the common legend. Default 2
 
 *nrow_l* when combine_legend = TRUE.
 Number of rows in which to align plots and the common legend. Default NULL.
 
 *rel_widths_l* when combine_legend = TRUE.
 Vector of widths in which to align plots and the common legend. Default = c(3, 1)
 
 *rel_heights_l* when combine_legend = TRUE.
 Vector of heights in which to align plots and the common legend. Default = c(1, 1)
 
 *ncol_legend* when combine_legend = TRUE
 number of columns the legend should be distributed in. Default 1 column 

### plot_compare

Combines multiple plots based on specified variables and plot type.
This function takes any input arguments from ghrexplore::plot_combine()
and from ghrexplore::plot_multiple() to customize the plots
and their organization in a grid. 

 *plot_function* indicates which of the ghr plot types to use
 Options are: 'plot_timeseries', 'plot_heatmap', 'plot_seasonality',
 'plot_bivariate', 'plot_map'.
 
 *data* an object of class "data.frame" containing covariates or cases 
 
 *var* the name of the variables identifying the covariates to be plotted. 
 Must be a character vector except if plot_function = plot_bivariate, 
 in which case it must be a list of 2 character vectors (a list of variable pairs). 


# Explore dengue in Laos

## Correlation

```{r p_correlation}
p_correlation <- plot_correlation(
  data= data,
  var = c("dengue_incidence", "tas", "tasmax", "tasmin", "prlr"),
  custom_label = c("Dengue Incidence",
                   "Average Temp.", "Max Temp.",
                   "Min Temp.", "Precipitation"),
  method = "pearson",
  palette = "BlueRed")

p_correlation

ggsave("p_correlation.jpg", plot = p_correlation, 
       width = 15, height = 10, units = "cm", dpi = 300)

```

The correlation analysis suggests that the most precipitation, minimum temperature
and average temperature might be the most relevant variables affecting dengue incidence. 


## Bivariate 

```{r p_bivariate_list}
# plot bivariate plots for all variables of interest into a list of plots

p_bivariate_list <- ghrexplore::plot_multiple(
 plot_function = plot_bivariate,
 data = data,
 var = list( c("tas", "dengue_incidence"),
             c("tasmin", "dengue_incidence"),
             c("tasmax", "dengue_incidence"),
             c("prlr", "dengue_incidence")),
 custom_labels = list(c("Average Temp.", "DENV incidence"),
                      c("Minimum Temp.", "DENV incidence"),
                      c("Maximum Temp.", "DENV incidence"),
                      c("Precipitation", "DENV incidence")),
 legend = "Province",
 group= "province",
 palette = c("Qualitative")
)


```
```{r p_bivariate_cases.vs.var, fig.width=15, fig.height=12, out.width="100%"}
# plot the difference from the mean plots 
p_bivariate_cases.vs.var<- ghrexplore::plot_combine(
  plot_list = p_bivariate_list[c("prlr_dengue_incidence",
                                 "tas_dengue_incidence",
                                 "tasmin_dengue_incidence",
                                 "tasmax_dengue_incidence")], 
  ncol=2,
  align = "hv",
  combine_legend = TRUE) 

p_bivariate_cases.vs.var

ggsave("p_bivariate_cases.vs.var.jpg", plot = p_bivariate_cases.vs.var,
       width = 30, height = 20, units = "cm", dpi = 300)

```

The bivariate analysis suggests that there might be non-linear effects between
precipitation, temperature and dengue incidence, and apparent temperatures at
which transmission is optimal.

## Time series

```{r p_timeseries_list, message=FALSE }
# plot timeseries for all variables of interest into a list of plots
p_timeseries_list <- ghrexplore::plot_multiple(
  plot_function = plot_timeseries,
  data = data,
  var = c("tas", "tasmax", "tasmin", "prlr", 
          "tas.diff", "tasmax.diff", "tasmin.diff", "prlr.diff"),
  cases = "dengue_cases",
  pop = "population",
  type = "incidence",
  custom_label = c("Dengue Incidence",
                   "Average Temp.", "Max Temp.",
                   "Min Temp.", "Precipitation", 
                   "Average Temp. Anomaly", "Max Temp. Anomaly",
                   "Min Temp. Anomaly", "Precipitation Anomaly"),
  palette = c("Red",
                   "Purp", "Purp",
                   "Purp","Blue", 
                   "Purp", "Purp",
                   "Purp","Blue"),
  time = "date",
  area = "province", 
  panel =TRUE)

```

```{r p_timeseries_cases.vs.var, fig.width=12, fig.height=10, out.width="100%", warning=FALSE}
# plot the difference from the mean plots 
p_timeseries_cases.vs.var<- ghrexplore::plot_combine(
  plot_list = 
    p_timeseries_list[c("dengue_cases",
                        "prlr",
                        "dengue_cases",
                        "tas")], 
  ncol=2,
  align = "hv") 

p_timeseries_cases.vs.var

ggsave("p_timeseries_cases.vs.var.jpg", plot = p_timeseries_cases.vs.var,
       width = 30, height = 20, units = "cm", dpi = 300)

```

```{r p_timeseries_cases.vs.diff, fig.width=12, fig.height=10, out.width="100%", warning=FALSE}
# plot the difference from the mean plots 
p_timeseries_cases.vs.diff<- ghrexplore::plot_combine(plot_list = p_timeseries_list[c("dengue_cases","prlr.diff", "dengue_cases","tas.diff")], 
                                                      ncol=2,
                                                      align = "hv") 

p_timeseries_cases.vs.diff

ggsave("p_timeseries_cases.vs.diff.jpg", plot = p_timeseries_cases.vs.diff,
       width = 30, height = 20, units = "cm", dpi = 300)

```

It is apparent from the seasonal anomaly of average temperature that something 
happened in the province of Khammouan during 2019 and 2020 
(there might have been a data collection error), as those 2 years present
monthly temperatures 3 degrees lower than the average during those months. 

## Heat map

```{r heat map, message=FALSE }
# plot heat map for all variables of interest into a list of plots
p_heatmap_list <- ghrexplore::plot_multiple(
  plot_function = plot_heatmap,
  data = data,
  var = c("tas", "tasmax", "tasmin", "prlr", 
          "tas.diff", "tasmax.diff", "tasmin.diff", "prlr.diff"),
  cases = "dengue_cases",
  pop = "population",
  type = "incidence",
  custom_label = c("Dengue Incidence",
                   "Average Temp.", "Max Temp.",
                   "Min Temp.", "Precipitation", 
                   "Average Temp. Anomaly", "Max Temp. Anomaly",
                   "Min Temp. Anomaly", "Precipitation Anomaly"),
  palette= c("Red",
                   "BlYlRd", "BlYlRd",
                   "BlYlRd", "Water", 
                   "BlueRed", "BlueRed",
                   "BlueRed", "BlueRed"),
  time = "date",
  area = "province")
```


```{r p_heatmap_cases.vs.var, fig.width=15, fig.height=12, out.width="100%"}
# plot the difference from the mean plots 
p_heatmap_cases.vs.var<- ghrexplore::plot_combine(
  plot_list = p_heatmap_list[c("dengue_cases",
                               "prlr",
                               "dengue_cases",
                               "tas"
                               )], 
  ncol=2,
  align = "hv") 

p_heatmap_cases.vs.var

ggsave("p_heatmap_cases.vs.var.jpg", plot = p_heatmap_cases.vs.var,
       width = 30, height = 20, units = "cm", dpi = 300)

```

```{r p_heatmap_cases.vs.diff, fig.width=15, fig.height=12, out.width="100%"}
# plot the difference from the mean plots 
p_heatmap_cases.vs.diff<- ghrexplore::plot_combine(
  plot_list = p_heatmap_list[c("dengue_cases",
                               "prlr.diff",
                               "dengue_cases",
                               "tas.diff")], 
  ncol=2,
  align = "hv") 

p_heatmap_cases.vs.diff

ggsave("p_heatmap_cases.vs.diff.jpg", plot = p_heatmap_cases.vs.diff,
       width = 30, height = 20, units = "cm", dpi = 300)

```

These plots alos highlight the temperature anomaly in Khammouan province during 2019 and 2020.

## Seasonality 

```{r p_seasonality_list}
# plot heat map for all variables of interest into a list of plots
p_seasonality_list <- ghrexplore::plot_multiple(
  plot_function = plot_seasonality,
  data = data,
  var = c("tas", "tasmax", "tasmin", "prlr", 
          "tas.diff", "tasmax.diff", "tasmin.diff", "prlr.diff"),
  cases = "dengue_cases",
  pop = "population",
  type = "incidence",
  custom_label = c("Dengue Incidence",
                   "Average Temp.", "Max Temp.",
                   "Min Temp.", "Precipitation", 
                   "Average Temp. Anomaly", "Max Temp. Anomaly",
                   "Min Temp. Anomaly", "Precipitation Anomaly"),
  palette= c("BlYlRd"),
  time = "date",
  area = "province")
```

```{r p_seasonality_cases.vs.var, fig.width=15, fig.height=12, out.width="100%"}
# plot the difference from the mean plots 
p_seasonality_cases.vs.var<- ghrexplore::plot_combine(
  plot_list = p_seasonality_list[c("dengue_cases",
                                   "prlr",
                                   "dengue_cases",
                                   "tas")], 
  ncol=2,
  align = "hv", 
  combine_legend = TRUE) 

p_seasonality_cases.vs.var

ggsave("p_seasonality_cases.vs.var.jpg", plot = p_seasonality_cases.vs.var,
       width = 30, height = 20, units = "cm", dpi = 300)

```
Seasonality plots suggest that rainfall and temperature is highly seasonal,
following similar patterns in the different provinces of Laos.
Dengue incidence also presents seasonal patterns, with increased number of 
cases in the more recent years (recent years represented in colors toward the
red spectrum. 

## Map 

```{r p_map_list}
# plot heat map for all variables of interest into a list of plots
p_map_list <- ghrexplore::plot_multiple(
  plot_function = plot_map,
  data = data,
  var = c("tas", "tasmax", "tasmin", "prlr"),
  cases = "dengue_cases",
  pop = "population",
  time = "date",
  area = "province", 
  custom_label = c("Dengue Incidence",
                   "Average Temp.", "Max Temp.",
                   "Min Temp.", "Precipitation"),
  palette= c("BlYlRd"),
  map = laos, 
  map_area = "NAME_1")
```


```{r p_map_cases.vs.var, fig.width=15, fig.height=12, out.width="100%"}
# plot the difference from the mean plots 
p_map_cases.vs.var<- ghrexplore::plot_combine(
  plot_list = p_map_list[c("dengue_cases",
                           "prlr",
                           "dengue_cases",
                           "tas"
                           )],
  ncol=2,
  align = "hv") 

p_map_cases.vs.var

ggsave("p_map_cases.vs.var.jpg", plot = p_map_cases.vs.var,
       width = 30, height = 20, units = "cm", dpi = 300)

```


## Compare 

```{r p_heatmap_compare, fig.width=15, fig.height=7, out.width="100%", out.height= "100%"}

plot_compare(
   plot_function = plot_heatmap,
   data = data,
   var = c("prlr"),
   cases = "dengue_cases",
   type= "incidence",
   pop = "population",
   time = "date",
   area = "province",
   custom_label = c("Dengue Cases", "Precipitation"), 
   palette = c("Red", "BlYlRd"),
   ncol = 2,
   ncol_legend = 1)

```

