Task One

Produce a map showing the centroids of each municipality in just the state of Säo Paulo, and add the outer boundary of Säo Paulo state.

  1. Open municipalities and filter for SP state
municipalities <- read_sf("Municipalities/municipalities.shp")

municipalities_sf <- municipalities %>% filter(UF=="SP")
  1. Generate municipalities centroids and plot them
municipalities_centroids <- municipalities_sf %>% st_centroid()

municipalities_centroids %>% ggplot() + geom_sf()

  1. Dissolve SP state polygons to create SP state border
SP_border <- municipalities_sf %>% st_union()

SP_border %>% ggplot() + geom_sf()

4. Plot SP centroids and SP border together

SP_border %>% ggplot() + geom_sf() +
  geom_sf(data=municipalities_centroids) +
  theme_classic()

## Task Two What is the mean Human Development Index of municipalities in each state of Brazil?

  1. HDI is a variable from municipalities data set

  2. Calculate mean HDI by state

br_states <- municipalities %>% group_by(UF) %>% summarise(IDHM_mean=mean(IDHM_10))
  1. Plot the result
br_states %>% ggplot() +
  geom_sf(aes(fill=IDHM_mean)) +
  theme_classic() +
  scale_fill_continuous(low="red",high="green")

## Task Three Produce a polygon/shapefile mapping the area of the municipality ‘Gaucha do Norte’ that is in the indigenous territory “Parque do Xingu”.

  1. Import indigenous park shapefile and plot it
indigenous <- read_sf("BC250_Terra_Indigena_A/BC250_Terra_Indigena_A.shp") %>% st_transform(4326)

br_states %>% ggplot() +
  geom_sf() +
  geom_sf(data=indigenous, fill="red")

2. Create a shapefile for Gaucha do Norte e Xingu

Xingu <- indigenous %>% filter(nome=="Parque do Xingu") %>% st_transform(4326)
Gaucha <- municipalities %>% filter(NOME=="GAUCHA DO NORTE") %>% st_transform(4326)
  1. Plot the shapefiles to see the overlap
Gaucha %>% ggplot() +
  geom_sf(fill="red") +
  geom_sf(data=Xingu, fill="blue", alpha=0.5)

  1. Create shapefile for intersection and plot it
interaction <- Gaucha %>% st_intersection(Xingu)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
interaction %>% ggplot() + geom_sf(fill="dark green")

  1. Plot all together
Gaucha %>% ggplot() +
  geom_sf(fill="red", aplha = 0.5) +
  geom_sf(data=Xingu, fill="blue", alpha=0.5) +
  geom_sf(data = interaction, fill = "black")
## Warning in layer_sf(geom = GeomSf, data = data, mapping = mapping, stat = stat,
## : Ignoring unknown parameters: `aplha`

Calculate the intersection area

st_area(interaction)
## 8109249396 [m^2]

Task Four

In the state of Acre (AC), which two social housing (MCMV) projects are closest to each other? Create a 10km buffer around each housing project.

  1. Import housing shapefile
Housing <- read_sf("MCMV_new/MCMV_new.shp")
  1. Select housing in AC state and plot the results
Housing_AC <- Housing %>% filter(UF=="AC")

br_states %>% filter(UF == "AC") %>% ggplot() + geom_sf() + geom_sf(data = Housing_AC)

  1. Calculate distance between housing points and view results
distance <- Housing_AC %>%
  st_transform(29189) %>%
  st_distance() %>%
  as.data.frame()
  1. Generate buffer around housing points
Housing_AC %>% st_transform(29189) %>%
  st_buffer(20000) %>%
  ggplot() + geom_sf(fill="dark green")

  1. Plot it all together
br_states %>% filter(UF == "AC") %>% ggplot() +
  geom_sf() +
  geom_sf(data = (Housing_AC %>% st_transform(29189) %>% st_buffer(20000)), fill = "dark green")+
  geom_sf(data = Housing_AC, color = "red")

Task Five

Across Brazil, which municipalities have the lowest and highest number of MCMV housing units (UH) in its territory? Create a map of the distribution of total housing units by municipality.

  1. Spatial join between municipalities and housing
mun_Housing_units <- municipalities %>% st_join(Housing)
  1. Calculate total of Housing units in each municipality
mun_Housing_units <- mun_Housing_units %>%
  group_by(COD_MUN,NOME) %>%
  summarise(UH=sum(UH,na.rm=T)) %>%
  ungroup()
## `summarise()` has grouped output by 'COD_MUN'. You can override using the
## `.groups` argument.
  1. Select municipality with most Housing units
mun_Housing_units %>% arrange(-UH) %>% slice(1) %>% pull(NOME)
## [1] "RIO DE JANEIRO"
  1. Plot the distribution of total housing units by municipality
mun_Housing_units %>% ggplot() +
  geom_sf(aes(fill=UH) , col=NA) +
  scale_fill_gradient(low="#ccece6",high="dark green", trans="log") +
  theme_classic()
## Warning in scale_fill_gradient(low = "#ccece6", high = "dark green", trans =
## "log"): log-2.718282 transformation introduced infinite values.

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.