---
title: 'Mapping population data'
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
urlcolor: blue
vignette: >
  %\VignetteIndexEntry{Mapping population data} 
  %\VignetteEngine{knitr::rmarkdown} 
  \usepackage[utf8]{inputenc}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = identical(tolower(Sys.getenv("NOT_CRAN")), "true"),
  out.width = "100%"
)

```

Here are a few quick examples to illustrate how you can use the {aopdata} package
to map the spatial distribution of population in Brazilian cities.


```{r, message = FALSE, warning=FALSE, eval=TRUE}
# load libraries
library(aopdata)
library(data.table)
library(ggplot2)
library(sf)
library(scales)

```

## Download population data
```{r, message = TRUE, eval = TRUE}
df <- aopdata::read_population(
  city = 'Fortaleza',
  year = 2010,
  geometry = TRUE,
  showProgress = FALSE
  )

```

## Map total population

```{r, message = FALSE, eval=!is.null(df)}
ggplot() +
  geom_sf(data=subset(df, P001>0), aes(fill=P001), color=NA, alpha=.8) +
  scale_fill_distiller(palette = "YlOrRd", direction = 1)+
  labs(title='Population distribution', fill="Total population") +
  theme_void()

```


## Map population by income levels

Here, we map the spatial distribution population by income decile (column `R003`).

```{r, eval = !is.null(df)}
ggplot() +
  geom_sf(data=subset(df, !is.na(R002)), aes(fill=factor(R003)), color=NA, alpha=.8) +
  scale_fill_brewer(palette = "RdBu") +
  labs(title='Average household income per capita', fill="Income decile") +
  theme_void()

```



## Map population by race

Here, we map the spatial distribution of the black population.

```{r, message = FALSE, eval=!is.null(df)}
df$prop_black <- df$P003 / df$P001
  
ggplot() +
  geom_sf(data=subset(df, P001 >0), aes(fill=prop_black), color=NA, alpha=.8) +
  scale_fill_distiller(palette = "RdPu", direction = 1, labels = percent)+
  labs(title='Proportion of black population', fill="Black population") +
  theme_void()

```


