---
title: "mapping using aggregated units"
output: 
  rmarkdown::html_vignette:
    keep_md: true 
    toc: true
vignette: >
  %\VignetteIndexEntry{mapping using aggregated units}
  %\VignetteEngine{knitr::rmarkdown}
  %\usepackage[utf8]{inputenc}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE, 
                      fig.width = 7, fig.height = 4,
                      fig.align = "center", message = FALSE, warning = FALSE)

```

The CRAN version can be loaded as follows:
```{r, message = FALSE}
library('mapping')
```

or the development version from GitHub:

```{r, message = FALSE, eval=FALSE}
remotes::install_github('serafinialessio/mapping')
```

The objects created in this package build the hierarchy starting from the statistical units in input to the larger statistical unit aggregate (look at the other vignettes for more details about the hierarchy).

`Aggregations` arguments allows to easily aggregate and mapping larger units starting from those given in input. 


## Italian Examples

```{r}
data("popIT")
it_pr <- IT(data = popIT, unit = "provincia", year = "2019", check.unit.names = FALSE)
str(it_pr,1)
```

In the structure we have bigger units, as *regione*

```{r}
mappingIT(data = it_pr, var = "totale")
mappingIT(data = it_pr, var = "totale", type = "interactive")
```

Aggregating the *province* in the beloging regions  summing the total population of each *province*

```{r}
mappingIT(data = it_pr, var = "totale", aggregation_unit = "regione")

```
```{r, eval = FALSE}
mappingIT(data = it_pr, var = "totale", aggregation_unit = "regione", type = "interactive")
```


or for each *ripartizione*, removing the missing values

```{r}
mappingIT(data = it_pr, var = "totale", 
          aggregation_unit = "ripartizione", 
          aggregation_fun = function(x) sum(x, na.rm = TRUE))

mappingIT(data = it_pr, var = "totale", 
          aggregation_unit = "ripartizione", 
          aggregation_fun = function(x) sum(x, na.rm = TRUE),
          type = "interactive")
```

This can be also used with facetes

```{r}
mappingIT(data = it_pr, var = "totale", 
          aggregation_unit = "regione", facets = "regione")

mappingIT(data = it_pr, var = "totale", 
          aggregation_unit = "regione", facets = "regione",
          type = "interactive")

mappingIT(data = it_pr, var = "totale", 
          aggregation_unit = "ripartizione", facets = "ripartizione", 
          aggregation_fun = function(x) sum(x, na.rm = TRUE))

mappingIT(data = it_pr, var = "totale", 
          aggregation_unit = "ripartizione", facets = "ripartizione", 
          aggregation_fun = function(x) sum(x, na.rm = TRUE),
          type = "interactive")
```

Also in the facetes we can chose the aggregation

```{r}
mappingIT(data = it_pr, var = "totale", 
          aggregation_unit = "regione", facets = "ripartizione")

mappingIT(data = it_pr, var = "totale", 
          aggregation_unit = "regione", facets = "ripartizione",
          type = "interactive")

```

<br/>

## European Union Example

```{r}
data("popEU")
popEU <- popEU
euNuts2 <- EU(data = popEU, colID = "GEO",unit = "nuts2",matchWith = "id")
str(euNuts2,1)
```

```{r}
mappingEU(data = euNuts2, var = "total")
mappingEU(data = euNuts2, var = "total", type = "interactive")
```

Aggregating the *nuts 0* in the belonging countries with the average population of each *nuts 0*

```{r}
mappingEU(data = euNuts2, var = "total", 
          aggregation_unit = "nuts0", aggregation_fun = mean)

mappingEU(data = euNuts2, var = "total", 
          aggregation_unit = "nuts0", aggregation_fun = mean,
          type = "interactive")
```

or with a summation to have the country population

```{r}
mappingEU(data = euNuts2, var = "total", 
          aggregation_unit = "nuts0", aggregation_fun = sum)

mappingEU(data = euNuts2, var = "total", 
          aggregation_unit = "nuts0", aggregation_fun = sum,
          type = "interactive")
```

We have many countries and the facetes can be visualise well. At this point we can subset, France for example, and show the nuts 1 units.

```{r}
mappingEU(data = euNuts2, var = "total", subset = ~I(iso3 == "FRA"),
          aggregation_unit = "nuts1", aggregation_fun = sum, facets = "nuts1")


```

```{r, eval = FALSE}
mappingEU(data = euNuts2, var = "total", subset = ~I(iso3 == "FRA"),
          aggregation_unit = "nuts1", aggregation_fun = sum, facets = "nuts1",
          type = "interactive")
```


## USA Example

```{r}
data("popUS")
us <- US(data = popUS, unit = "state")
str(us,1)
```

```{r}
mappingUS(data = us, var = "population")

mappingUS(data = us, var = "population", type = "interactive")

```

```{r}
mappingUS(data = us, var = "population", 
          aggregation_unit = "region")

mappingUS(data = us, var = "population", 
          aggregation_unit = "region",
          type = "interactive")

```
```{r}
mappingUS(data = us, var = "population", 
          aggregation_unit = "region", facets = "region")

mappingUS(data = us, var = "population", 
          aggregation_unit = "region", facets = "region",
          type = "interactive")
```



## World example

```{r}
data("popWR")
popWR <- popWR
wr <- WR(data = popWR, colID = "country_code",
         matchWith = "iso3_eh", check.unit.names = FALSE,
         res = "low")
str(wr,1)
```

```{r}
mappingWR(data = wr, var = "total")
mappingWR(data = wr, var = "total", type = "interactive")

```

Aggregating by *continent*

```{r}
mappingWR(data = wr, var = "total", 
          aggregation_unit = "continent", 
          aggregation_fun = function(x) sum(x, na.rm = TRUE))

mappingWR(data = wr, var = "total", 
          aggregation_unit = "continent", 
          aggregation_fun = function(x) sum(x, na.rm = TRUE),
          type = "interactive")
```

