---
title: "Interactive plots & maps with rMarkdown and flexdashboards"
author: "Anto Aasa"
date: "december 03, 2021"
output: html_document
---

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


```

```{r echo = F}
setwd("/ANTO/loengud/geopythonR/rspatial_Git/rspatial/")
```


libraries:
```{r warning=FALSE, message=FALSE, error=FALSE}
library(tidyverse)
library(ggthemes) 
library(plotly)
library(sf)
library(leaflet)
library(gganimate)
library(tmap)
```

Today's world is the world of the Internet. Data and information are everywhere. Our society is getting more and more data-driven. Good decisions and governance are supported by data, analysis, knowledge, and wisdom. And for that, we need excellent communication. Static visualizations are informative and unavoidable in the printed world. But the Internet offers us the platform for interactivity. Interactive documents and dashboards may help to communicate the message in a more attractive and catchy way. We cannot afford to miss this train.    
In the current session, we learn how to create an (interactive) web page (*.html) in RStudio, and we try to create interactive dashboards and briefly look at the nature of shiny applications.    

> **According to the Wikipedia**: "Across the many fields concerned with interactivity, including information science, computer science, human-computer interaction, communication, and industrial design, there is little agreement over the meaning of the term "interactivity", although all are related to interaction with computers and other machines with a user interface.   
Multiple views on interactivity exist. In the "contingency view" of interactivity, there are three levels:   
- Not interactive, when a message is not related to previous messages;   
- Reactive, when a message is related only to one immediately previous message; and   
- Interactive, when a message is related to a number of previous messages and to the relationship between them."   


Creating the interactive and especially animated visualizations means very often that you have to wait. Time of waiting depends on the efficiency of the algorithm and computing power. Waiting is good time to read and learn more about possibilities to create interactive maps and visualizations in R:

- [Interactive maps in R (with leaflet). Hansen Johnson](https://hansenjohnson.org/post/interactive-maps-in-r/)
- [Creating Interactive Spatial Maps in R Using Leaflet. Carson Farmer & Leah Wasser](https://www.earthdatascience.org/courses/earth-analytics/get-data-using-apis/leaflet-r/)
- [Interactive maps with linked data and R. by Julija Hansen](https://medium.swirrl.com/interactive-maps-with-linked-data-and-r-e977ea441cb)
- [Interactive Maps in R](https://bhaskarvk.github.io/user2017.geodataviz/notebooks/03-Interactive-Maps.nb.html)
- [Interactive and animated maps](https://geocompr.robinlovelace.net/adv-map.html#animated-maps)

## Interactive time series
First, we try to create some simple interactive time serries plots. Interactivity means that we can communicate with the object. For example, we can zoom in/out and get additional information (select, mouseover).

### COVID-19 infections in Estonia

Estonian COVID-19 information is available from web page of [Health Board Estonia](https://www.terviseamet.ee/en/coronavirus/coronavirus-dataset). There are available many different interactive visualizations about the situation. From the same location we can download also the raw data (updated on daily bases 10:35). Unfortunately the data description is only in Estonian but at least the variable names are in English [link](https://www.terviseamet.ee/et/koroonaviirus/avaandmed). From this page we choose data set about the infections in counties. Link to the data set: https://opendata.digilugu.ee/opendata_covid19_test_county_all.csv    

Import the data into R workspace! Name the imported dataset : `covid19_test_county_all`. Be carful with dates!!! By default the date variables are recognized as text!!!
```{r echo=F, warning=FALSE, message=F}
covid19_test_county_all <- read_csv("https://opendata.digilugu.ee/opendata_covid19_test_county_all.csv", 
    col_types = cols(LastStatisticsDate = col_date(format = "%Y-%m-%d"), 
        StatisticsDate = col_date(format = "%Y-%m-%d")))
```

If the import was correct and successful, the structure of data should look like this:
```{r}
glimpse(covid19_test_county_all)
```

In current example the interesting fields in dataset are:

 - `StatisticsDate`: the date for which the result is valid;
 - `County`: name of the county (place of residence);
 - `CountyEHAK`: EHAK code of the county (we need it for mapping!);
 - `ResultValue`: test result (**n**egative or **p**ositive);
 - `DailyCases`: number of the new cases.
 
Keep only positive tests!    
```{r echo=FALSE}
covid19_test_county_all <- covid19_test_county_all %>% 
  filter(ResultValue ==  "P")
```

If you managed to complete it correctly, then you see that `ResultValue` is `P`:
```{r}
covid19_test_county_all %>% 
  distinct(ResultValue, .keep_all = FALSE)
```
Now plot it as a static plot:
```{r}
ggplot()+
  geom_line(data = covid19_test_county_all, aes(x= StatisticsDate, y = DailyCases), size= .25)+
  facet_wrap(vars(County), scale = "free_y") # separate plot window for every County. What happends without: scale = "free_y"?
```

In column `County` after every county name is " maakond". Delete it! (Hint: [Session 1](https://aasa.ut.ee/Rspatial/01_session.html)).
```{r echo = F}
covid19_test_county_all <- covid19_test_county_all %>% 
  mutate(County = str_replace(County, " maakond", ""))
```

For some cases the place of residence is unknown (`NA`; foreigners usually). Delete those rows as well! [Hint](https://aasa.ut.ee/Rspatial/02_session.html).
```{r}
covid19_test_county_all <- covid19_test_county_all %>% 
  filter(!is.na(County))

gg_cov_cases <- ggplot()+
  geom_line(data = covid19_test_county_all, aes(x= StatisticsDate, y = DailyCases), size= .25)+
  facet_wrap(vars(County), scale = "free_y", ncol = 3) 

plotly::ggplotly(gg_cov_cases)
```

```{r warning=F, message=FALSE}
covid19_test_county_all_2 <- covid19_test_county_all %>% 
  filter(County == "Tartu" | County =="Ida-Viru")

gg_cov_cases_2 <- ggplot()+
  geom_line(data = covid19_test_county_all_2, aes(x= StatisticsDate, y = DailyCases, color = County), size= .25, alpha = .5)+
  stat_smooth(data = covid19_test_county_all_2, aes(x= StatisticsDate, y = DailyCases, color = County), size= .25, se = F)+
  scale_colour_manual(values = c("blue", "red"))
  
plotly::ggplotly(gg_cov_cases_2)
```

```{r warning=F, message=FALSE}
library(dygraphs)

covid19_test_county_all_2a <- covid19_test_county_all_2 %>% 
  select(StatisticsDate, County, DailyCases)

covid19_test_county_all_2a <- covid19_test_county_all_2a %>% 
  pivot_wider(names_from = County, values_from = DailyCases, id_cols = StatisticsDate)


covid19_test_county_all_2b <- xts::xts(covid19_test_county_all_2a, order.by = covid19_test_county_all_2a$StatisticsDate) 

covid19_test_county_all_2b$StatisticsDate <- NULL
  
dygraph(covid19_test_county_all_2b) %>% 
  dySeries() %>% 
  dyRangeSelector() 
```

## Interactive maps
As usual, there are several options to draw interactive plots. you can use `plotly`, `leaflet`, `tmap` or something else.

### Prepare the data
As you already saw, plotly is very simple. Just make the map in `ggplot` and wrap it with `plotly::ggplotly()`. In next example we create interactive plot of COVID-19 cases in Estonia.    
Firstly prepare the attributes: number of positive cases for the last available date by county:
```{r}
covid19_test_county_all_latest <- covid19_test_county_all %>% 
  select(CountyEHAK, DailyCases, StatisticsDate) %>% 
  filter(StatisticsDate == max(StatisticsDate))
```

Now refresh you memory and import again the county polygons!
```{r echo= F}
counties <- st_read("maakond_20211101.shp", quiet = T)
```


```{r}
# data structure:
glimpse(counties)

# map:
ggplot()+
  geom_sf(data = counties, fill = "slategray2", col = "blue", size= .25)
```

```{r}
counties <- counties %>% 
  st_simplify(preserveTopology = TRUE, dTolerance = 200) %>% 
  st_cast("MULTIPOLYGON") # defines the type of geometry after simplification

glimpse(counties)
```

Join the attributes with polygons!
```{r}
covid19_test_county_all_latest_sf <- left_join(counties, covid19_test_county_all_latest, by = c("MKOOD" = "CountyEHAK"))
```

```{r}
gg_covid19_map <- ggplot()+
  geom_sf(data = covid19_test_county_all_latest_sf, aes(fill = DailyCases))+
  scale_fill_gradientn(colours = c("forestgreen", "grey70", "orange", "red"))

gg_covid19_map
```

Add values to map as labels!   
```{r}
covid19_test_county_all_latest_sf_cntr <- covid19_test_county_all_latest_sf %>% 
  st_centroid()

gg_covid19_map <- ggplot()+
  theme_void()+
  geom_sf(data = covid19_test_county_all_latest_sf, aes(fill = DailyCases))+
  geom_sf_text(data = covid19_test_county_all_latest_sf_cntr, aes(label = DailyCases))+
  scale_fill_gradientn(colours = c("forestgreen", "grey70", "orange", "red"))

gg_covid19_map
```


### plotly
And now interactive map:
```{r}
plotly::ggplotly(gg_covid19_map)
```


### Leaflet
Leaflet is an open source JavaScript library used to build web mapping applications. Leaflet allows developers to very easily display tiled web maps hosted on a public server, with optional tiled overlays.   

Leaflet package is avialable also for R: [link](https://rstudio.github.io/leaflet/).   
Firstly you have to install it!   


```{r message=F, warning=F}
library(leaflet)
```

```{r}
covid19_test_county_all_latest_sf_4326 <- covid19_test_county_all_latest_sf %>% 
  st_transform(4326)

covid19_test_county_all_latest_sf_cntr_4326 <- covid19_test_county_all_latest_sf_cntr %>% 
  st_transform(4326)
```


```{r}
leaflet() %>% 
  addTiles() %>% 
  addPolygons(data = covid19_test_county_all_latest_sf_4326) 
```
```{r}
library(RColorBrewer)
pal <- colorBin(palette = "Purples", 
                domain = covid19_test_county_all_latest_sf_4326$DailyCases, n = 5) # split colors from white to red into 9 even bins


leaflet() %>% 
  addTiles() %>% 
  addPolygons(data = covid19_test_county_all_latest_sf_4326, 
              label= ~DailyCases, # when you hover over the polygon, it will label the svi
              color = "gray", # the color of the border
              fillColor = ~pal(covid19_test_county_all_latest_sf_4326$DailyCases), # the colors inside the polygons
              weight = 1.0, # the thickness of the border lines
              opacity = 1.0, # the transparency of the border lines
              fillOpacity = 0.8) %>%  # the transparency inside the polygons
  addLabelOnlyMarkers(data = covid19_test_county_all_latest_sf_cntr_4326,
                      label = ~covid19_test_county_all_latest_sf_cntr_4326$DailyCases,
                      labelOptions = labelOptions(noHide = T))

```


### tmaps
```{r}
tmap_mode("view")
```

```{r}
tm_shape(covid19_test_county_all_latest_sf_4326)+
  tm_polygons(col = "DailyCases", 
              style = "pretty",
              palette = "Reds",
              alpha = .7)+
tm_shape(covid19_test_county_all_latest_sf_cntr_4326)+
  tm_text(text = "DailyCases",
          bg.color = "grey",
          shadow = T)
```

Interactive plots and maps are nice and informative. But they cannot be independent object, they must be part of something else. Very often they are embedded to web page. In next example we look how to create html directly from RStudio.For that we use `rMarkdown`.

# rMarkdown
## html
According to the RStudio: "R Markdown documents are fully reproducible. Use a productive notebook interface to weave together narrative text and code to produce elegantly formatted output. Use multiple languages including R, Python, and" SQL. More: [link](https://rmarkdown.rstudio.com).     
It is true, at least partially. For example all the R related tutorials of current course are produced with rMarkdown.    
Let's try!   

Before the next steps you should install the library:
```{r eval=F}
install.packages("rmarkdown")
```

Create the markdown file (*.Rmd):    
File -> New File -> R Markdown    

![](http://aasa.ut.ee/Rspatial/rmarkdown_01.png)

In next window you can enter the title and author of the document. Other options are ok by default. 

![](http://aasa.ut.ee/Rspatial/rmarkdown_02.png)

Short tutorial of rMarkdown is available from here: [link](https://rmarkdown.rstudio.com/lesson-1.html)    
You explore the script of current page as well: [link](00_interactive_session.Rmd). To see the highlighted and formatted script you shoukld download it and open it in RStudio.     

Try to create your own rMarkdown html! Fill it for example with content from your previous assignments!     

To process the Rmd-file to html-file, you have to "knit" it:

![](http://aasa.ut.ee/Rspatial/rmarkdown_05.png)

In case you have your own webpage you can upload the created html directly to there. But you can use also the RPubs platform: [link](https://rpubs.com). For RPubs you have to create user account.   

![](http://aasa.ut.ee/Rspatial/rmarkdown_03.png)    



## Dashboards
> **According to Wikipedia**: A dashboard is a type of graphical user interface which often provides at-a-glance views of key performance indicators (KPIs) relevant to a particular objective or business process. In other usage, "dashboard" is another name for "progress report" or "report" and considered a form of data visualization.     
The “dashboard” is often accessible by a web browser and is usually linked to regularly updating data sources.

List of the impressive dashboards created in R is very long:

 - https://rpubs.com/joaosilva/covid19-dash
 - https://calcat.covid19.ca.gov/cacovidmodels/
 - https://pkgs.rstudio.com/flexdashboard/articles/examples.html
 
 - https://aasa.ut.ee/tmp/covidEst.html


Good introduction how to start using the flexdashboard is available from here:
https://pkgs.rstudio.com/flexdashboard/ 

To start with flexdashboard you should select:    

File -> New File -> R Markdown -> Flex Dashboard       

![](http://aasa.ut.ee/Rspatial/rmarkdown_04.png)   

For the beginners the biggest problem is probably the layout of the dashboard. Some optional storyboard layouts are described in here: [link](https://pkgs.rstudio.com/flexdashboard/articles/using.html#storyboards-1).        
The individual assignment after this session for you is to create your own interactive dashboard! Fill the dashboard with you own content. Dashboard must contain at least 4 different interactive visualizations (at least 2 maps)

![](http://aasa.ut.ee/Rspatial/flexdashboard_empty_01.png)
You can use this file as the possible template for your assignment: [link](final_dashboard.Rmd)


***

```{r echo=FALSE, message=FALSE, warning=FALSE}
time <- Sys.time()
```

<small> Author: Anto Aasa </small>  
<span style="color:grey">LTOM.02.041</span>  
<span style="color:grey">Last update: `r time` </span>
  
  
![](C:/ANTO/loengud/GIS Maps and Spatial Analyses for Urban Planning/2018/praktikum/logo.jpg "logo")


.

