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

## introduction

The following exercises are modified from [Chapter 9 of Geocomputation with R by Rovin Lovelace](https://geocompr.robinlovelace.net/adv-map.html).

## prerequisites

```{r load, include=TRUE, message=FALSE, warning=FALSE}
library(sf)
library(raster)
library(terra)
library(spData)
library(spDataLarge)
library(tidyverse)
library(ggspatial)
library(patchwork)
```

## Exercise 1

These exercises rely on a new data object based on the **world** and **worldbank_df** datasets from the **spData* package.

```{r include=TRUE}
africa = world |> 
  filter(continent == "Africa", !is.na(iso_a2)) |> 
  left_join(worldbank_df, by = "iso_a2") |> 
  dplyr::select(name, subregion, gdpPercap, HDI, pop_growth) |> 
  st_transform("+proj=aea +lat_1=20 +lat_2=-23 +lat_0=0 +lon_0=25")
```

## Map 1

Create a map showing the geographic distribution of the Human Development Index (HDI) across Africa.  

```{r include=TRUE}
?geom_sf

m1 = ggplot(data = africa) +
  geom_sf(aes(fill = HDI))
```

## Map 2

Update the map created for question 1 so the legend has three bins: "high" (HDI above 0.7), "medium" (HDI between 0.55 and 0.7), and "low" (HDI below 0.55).   

```{r include=TRUE}
m2 = m1 + 
  scale_fill_binned(breaks = c(0.55, 0.7))
```

## Map 3

Represent Africa's subregions on the map.  
-   change the color palette 
-   change the legend title 

```{r include=TRUE}
m3 = ggplot(africa) +
  geom_sf(aes(fill = subregion)) +
  theme_bw() +
  scale_fill_viridis_d() +
  labs(fill = "Sub Region")
```

-   combine this map with the map from question 2 into a single plot   

```{r}
m2 + m3
```


## Exercise 2

Here, we will use the **zion** and **nlcd** datasets from the **spDataLarge** package.

```{r include=TRUE}
zion = st_read((system.file("vector/zion.gpkg", package = "spDataLarge")))
data(nlcd, package = "spDataLarge")
force(nlcd)
```

First, we need to convert the raster into a dataframe

```{r}
nlcd_df = as.data.frame(nlcd, xy=TRUE)
head(nlcd_df)
```

## Map 4

Create a land cover map of Zion National Park    
-   change the default colors to match your perception of land cover categories   
-   move the map legend outside of the map to improve readability 
-   add a scale bar and north arrow and change the position of both to improve the maps aesthetics 
-   add the park boundaries on top of the land cover map    

```{r include=TRUE}
?geom_raster

ggplot() +
  geom_raster(data = nlcd_df, aes(x=x, y=y, fill = layer_levels)) + 
  annotation_scale() +
  annotation_north_arrow(location = "br", 
                         style = ggspatial::north_arrow_nautical(),
                         pad_x = unit(0.2, "in"), 
                         pad_y = unit(0.2, "in")) + geom_sf(data = zion, aes(fill = NA), alpha = 0.2, linewidth = 1, color = "black")
```

## Map 5

-   add an inset of Zion's location in the context of the state of Utah  

```{r}

```

```{r}

```

```{r}

```
