municipalities <- read_sf("C:/Users/accou/Downloads/Spatial Week 3/municipalities/municipalities.shp")
municipalities_sp <- municipalities %>% filter(UF=="SP")
municipalities_centroids <- municipalities_sp %>% st_centroid()
municipalities_centroids %>% ggplot()+geom_sf()
SP_border <- municipalities_sp %>% st_union()
SP_border %>% ggplot()+geom_sf()
SP_border %>% ggplot()+geom_sf()+
geom_sf(data = municipalities_centroids)+
theme_classic() +
labs(title = "Sao Paulo and Their Centroids")
### Task 2: What is the mean human development index of municipalities
in each state of Brazil?
1.HDI mean by state
br_states <- municipalities %>%
group_by(UF) %>%
summarise(IDHM_mean = mean(IDHM_10))
br_states %>% ggplot()+
geom_sf(aes(fill=IDHM_mean))+
theme_classic()+scale_fill_continuous(low = "red",high="green")+
labs(title = "Brazilian Sates by Mean Human Development Index")
indegenous <- read_sf("C:/Users/accou/Downloads/Spatial Week 3/BC250_Terra_Indigena_A/BC250_Terra_Indigena_A.shp")
## Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, : GDAL
## Message 1: C:\Users\accou\Downloads\Spatial Week
## 3\BC250_Terra_Indigena_A\BC250_Terra_Indigena_A.shp contains polygon(s) with
## rings with invalid winding order. Autocorrecting them, but that shapefile
## should be corrected using ogr2ogr for example.
br_states %>% ggplot()+geom_sf() + geom_sf(data = indegenous,fill = "red")
Xingu <- indegenous %>% filter(nome == "Parque do Xingu") %>% st_transform(4326)
Gaucha <- municipalities %>% filter(NOME== "GAUCHA DO NORTE") %>% st_transform(4326)
Gaucha %>% ggplot()+ geom_sf(fill = "red") + geom_sf(data=Xingu,fill="blue",alpha= 0.5)
intersection <- Gaucha %>% st_intersection(Xingu)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
intersection %>% ggplot()+geom_sf(fill = "dark green")
5. Plotting together
Gaucha %>% ggplot()+geom_sf(fill = "red",alpha=0.5) + geom_sf(data=Xingu, fill="blue",alpha=0.5) +
geom_sf(data = intersection,fill = "black")
Intersection area
st_area(intersection)
## 8109249396 [m^2]
mun_Housing_units <- municipalities %>% st_join(Housing)
mun_Housing_units <- mun_Housing_units %>%
group_by(COD_MUN,NOME) %>%
summarize(UH=sum(UH,na.rm = T)) %>%
ungroup()
## `summarise()` has grouped output by 'COD_MUN'. You can override using the
## `.groups` argument.
mun_Housing_units %>% arrange(-UH) %>% slice(1) %>% pull(NOME)
## [1] "RIO DE JANEIRO"
mun_Housing_units %>% ggplot() +
geom_sf(aes(fill=UH),col=NA) +
scale_fill_gradient(low="#ccece6",high = "dark green",trans = "log")+
theme_classic()+
labs(title = "Total Housing Units by Municipality")
## Warning in scale_fill_gradient(low = "#ccece6", high = "dark green", trans =
## "log"): log-2.718282 transformation introduced infinite values.