---
title: "Lec 32: Post-class Exercise: Choropleth Maps"
output:
  html_document
---

```{r, include=FALSE}
# Do not edit this code chunk
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning = FALSE)
```

# Step 1: Loading Shapefiles into R

We will use [India Village-Level Geospatial Socio-Economic Data Set: 1991, 2001](https://earthdata.nasa.gov/data/catalog/sedac-ciesin-sedac-india-vilsocioecon-1.00) published by NASA Socioeconomic Data and Applications Center (SEDAC). I've filtered this dataset to the Gulbarga (now Kalaburagi) district. It is distributed as a shapefile. We will also use the shapefile for the Gulbarga district. In this exercise, we will map the literacy rate in the Gulbarga district.

We will need the `tidyverse`, `sf`, and `RColorBrewer` packages.

```{r}
# Write your code below to load the three packages and the two shapefiles

```

You can read `india-village-level-geospatial-socio-econ-1991-2001-documentation.pdf` Page 10-17 in the `data_documentation` folder to understand the variables in the `gulbarga_census` dataset.

# Step 3: Checking CRS

Run the code below to check the coordinate reference system (CRS). They both should be in EPSG:4326, so we're good to go to the next step.

```{r}
gulbarga_district |>
  st_crs()

gulbarga_census |>
  st_crs()
```

# Step 4: Calculating the Literacy Rate

Use a data wrangling function to create a new column in `gulbarga_census` that calculates the literacy rate using `P_LIT` divided by `TOT_P`. Set that column name to `literacy_rate`. The `P_LIT` column refers to Person Literates and `TOT_P` refers to Total Population. Notice that `P_LIT` and `TOT_P` are currently categorical variables, so you need to use the same data wrangling function to first convert them to numerical variables using `as.numeric`. If you are not sure which function to use, you may refer to Lec13 and its exercises for help.

```{r}
# Write your code below

```

# Step 5: All Classes Choropleth Static Map

Plot an all classes choropleth static map based on the numerical variable `literacy_rate` you created above, setting the palette to `YlOrRd`. The first layer of the map is `gulbarga_district` and the second layer of the map is `gulbarga_census`. Make sure to make the legend title look better.

```{r}
# Write your code below

```

# Step 6: Quantile Choropleth Static Map

Now let's use the `cut_number()` function to convert the numerical variable `literacy_rate` to a categorical variable with 3 levels like we did in Exercise 1 in Lec31 In-class Exercises. After creating the quantile bins, you need to use the `factor()` function to order 3 levels so that the lower literacy rate corresponds to darker color and the higher literacy rate corresponds to lighter color when you plot. Then plot a quantile choropleth static map. How to do that is the same as how we ordered 3 levels in the Challenge exercise in Lec31 In-class Exercises.

```{r}
# Write your code below

```

# Step 7: Equal Intervals Choropleth Static Map

Now create equally sized interval bins "(0.6781, 1]", "(0.3561, 0.6781]", "[0.0342, 0.3561]". How to do that is the same as how we created 3 bins in the Challenge exercise in Lec31 In-class Exercises. After creating the equally sized interval bins, you need to use the `factor()` function to order 3 levels so that the lower literacy rate corresponds to darker color and the higher literacy rate corresponds to lighter color when you plot. Then plot an equal intervals choropleth static map. 

```{r}
# Write your code below

```

# Step 8: Understanding the Above Three Approaches Creating Choropleth Maps 

**Discussion**: Why do the colors appear differently on each map? Which map best represents the distribution of values in `literacy_rate`?

**Answer**:  
