---
title: "Testing - Facetting"
author: "Martijn Tennekes"
date: "`r Sys.Date()`"
output: html_document
---
	
	```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 9)
```

# Instructions

Open the tmap repository as RStudio project.
Set Knit Directory (via drop-down menu of the Knit button in RStudio) to Project Directory.

```{r echo=FALSE,message=FALSE,fig.height = 2}
library(devtools)
load_all()
library(stars)
library(terra)

data(World,metro,land)

World$pop_class = cut(World$pop_est, breaks = c(0, 10, 100, 1000, Inf) * 1e6, labels = c("Small", "Medium", "Large", "Extra Large"))					   
World$HPI_class = cut(World$HPI, breaks = seq(10, 50, by = 10))
World$well_being_class = cut(World$well_being, breaks = seq(2, 8, by = 2))
World$footprint_class = cut(World$footprint, breaks = seq(0, 16, by = 4))

metro$pop2020_class = cut(metro$pop2020, breaks = c(.5, 1.5, 2.5, 5, 15, 40) * 1e6)
metro$pop2020_class8 = cut(metro$pop2020, breaks = c(.5, 1, 1.5, 2.5, 5, 10, 15, 20, 40) * 1e6)



Africa = World[World$continent == "Africa", ]

data(land)

land$cover_int = as.integer(land$cover)
land$cover_int[1:1000] = NA

World$economy_int = as.integer(World$economy)
World$economy_int[1:100] = NA


landsat_stars = read_stars(system.file("raster/landsat.tif", package = "spDataLarge"))
landsat_terra = rast(system.file("raster/landsat.tif", package = "spDataLarge"))

tif_stars = system.file("tif/L7_ETMs.tif", package = "stars") %>% stars::read_stars()
tif_terra = system.file("tif/L7_ETMs.tif", package = "stars") %>% terra::rast()

prec = stars::read_ncdf(system.file("nc/test_stageiv_xyt.nc", package = "stars"), curvilinear = c("lon", "lat"), ignore_bounds = TRUE)
nc = sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf"), "nc.gpkg") %>%
	sf::st_transform(sf::st_crs(prec))
prec_nc = aggregate(prec, by = nc, FUN = max)

weather = stars::read_ncdf(system.file('nc/bcsd_obs_1999.nc', package = 'stars'))
weather1 = stars::st_set_dimensions(merge(weather), names = c('longitude','latitude','time','attributes'))
weather2 = split(weather1, 'time')




tmapV =  ifelse(packageVersion("tmap") >= "3.4", "4", "3")
txt = function(x) print(grid::grid.text(x, gp = gpar(cex = 3)))
v3 = function(e) {
	if (tmapV == "3") {
		print(e)
	} else {
		txt("Only for tmap 3")
	}
	invisible(NULL)
}
v4 = function(e) {
	if (tmapV == "4") {
		print(e)
	} else {
		txt("Only for tmap 4")
	}
	invisible(NULL)
}
txt(paste("Loaded tmap version", tmapV))
```

# Facetting shapes (variables)

These tests focus on dealing with creating facets with variables, group by variables or (stars) dimensions etc.

### One facet

```{r}
tm_shape(World) +
	tm_polygons("HPI")
```


### Wraps

```{r}
# wrap over variables
tm_shape(World) +
	tm_polygons(c("HPI", "income_grp"))
```

### Multiple facets, specified by a 'facet wrap'

```{r}
# wrap by
tm_shape(World) +
	tm_polygons("HPI") +
	tm_facets(by = "continent")
```

```{r}
# conflict: should take first variable
tm_shape(World) +
	tm_polygons(c("HPI", "income_grp")) +
	tm_facets(by = "continent")
```

```{r}
tm_shape(World) +
	tm_polygons(c("HPI", "income_grp")) +
	tm_facets_grid(columns = "continent")
```

```{r}
tm_shape(World) +
	tm_polygons(c("HPI", "income_grp")) +
	tm_facets_grid(rows = "continent")
```

```{r}
tm_shape(World) +
	tm_polygons(c("HPI")) +
	tm_facets_grid(columns = "well_being_class", rows = "continent")
```

```{r}
# stars 2 attributes and 3th dimensions
tm_shape(weather) +
	tm_raster()
```

```{r}
# idem
tm_shape(weather) +
	tm_raster(c("tas", "pr"))
```


```{r}
# inverse grid
tm_shape(weather) +
	tm_raster() +
	tm_facets_grid(rows = "time")
```

```{r}
# idem
tm_shape(weather) +
	tm_raster(c("tas", "pr")) +
	tm_facets_grid(rows = "time")
```

```{r}
# idem
tm_shape(weather) +
	tm_raster(c("tas", "pr")) +
	tm_facets_grid(columns = "VARS__")
```


```{r}
# wrap with warning (only first variable used)
tm_shape(weather) +
	tm_raster() +
	tm_facets_wrap()
```

```{r}
# select other variable, wrap (default to shape dim)
tm_shape(weather) +
	tm_raster("tas") +
	tm_facets_wrap()
```


```{r}
# idem
tm_shape(weather) +
	tm_raster("tas")
```





```{r}
# idem
tm_shape(weather) +
	tm_raster(c("tas", "pr")) +
	tm_facets_grid(rows = "time")
```


```{r}
tm_shape(weather2) +
	tm_raster()
```

```{r}
weather22 = weather2[1:2]

tm_shape(weather22) +
	tm_raster(col.free = c(T, F))

tm_shape(weather22) +
	tm_raster(col.free = c(F, F))

tm_shape(weather22) +
	tm_raster(col.free = c(T, T))

tm_shape(weather22) +
	tm_raster(col.free = c(F, T))


```


```{r}
tm_shape(World) + tm_polygons("pop_est", fill.free = FALSE) + tm_facets("continent")
tm_shape(World) + tm_polygons("pop_est", fill.free = TRUE) + tm_facets("continent")
tm_shape(World) + tm_polygons("pop_est", fill.free = TRUE) + tm_facets("continent", nrows = 1)
```


```{r}
# merging facets OK
tm_shape(World) +
	tm_polygons(c("area", "pop_est", "pop_est_dens", "economy")) +
tm_shape(World) +
	tm_symbols(size = "pop_est") +
	tm_facets(by = "pop_class")
```

```{r}
# merging facets OK
tm_shape(World) +
	tm_polygons(c("area", "pop_est", "pop_est_dens", "economy")) +
tm_shape(World) +
	tm_symbols(size = "pop_est") +
	tm_facets(by = "pop_class", nrows = 1)
```



```{r}
tm_shape(World) + tm_polygons("HPI") + tm_facets(by = "economy")
```

```{r}
tm_shape(World) + tm_polygons("HPI") + tm_facets(by = "economy_int")
```

```{r}
tm_shape(World) + tm_polygons("HPI") + tm_facets(by = "economy", drop.NA.facets = TRUE)
```


```{r}
tm_shape(land) + tm_raster("trees") + tm_facets(by = "cover_int")
```


```{r}
tm_shape(land) + 
	tm_raster("trees") +
	tm_facets_wrap("cover_cls") +
tm_shape(World) +
	tm_polygons(col = rainbow(8), lwd = 3, fill = NA) +
tm_shape(metro) +
	tm_symbols(fill = "pop2020_class8", size = .5)


```
