Sunday, January 5, 2020

Plotting the metropolitan areas of Brazil in R

Here is a simple R script to download the shape files of Brazilian metropolitan areas by year and plot them in R.

obs. It's important to note that, since the 1988, metro areas are created by state governments. As a rule, this is done with absolutely no transparency nor any technical criteria. It is this legal issue, rather than the urbanization process in the country itself, that led to such an inflated increase in the number of metro areas in the country over the past two decades.

Number of metro areas in Brazil:
1970: 9
2001: 24 
2018: 77

Using R and the geobr package to plot Brazilian metropolitan areas in different years.

logo

library(geobr)
library(dplyr)
library(ggplot2)
library(sf)
library(ggthemes)


# download sf of Brazilian states
  states <- read_state(code_state = 'all')


# download metro areas sf and pille them up
  download_metro <- function(y){
    tmp <- read_metro_area(year = y) %>% select(name_metro, abbrev_state)
    tmp$year <- y
    return(tmp)
  }

  metros_sf <- lapply(X= c(1970, 2001, 2018), FUN=download_metro) %>% do.call('rbind', .)


# plot
  temp_lot <- ggplot() +
                geom_sf(data=states, color="gray90", fill="gray80", size=.4) +
                geom_sf(data=metros_sf, aes(color=as.factor(name_metro), fill=as.factor(name_metro)), show.legend = FALSE) +
                facet_wrap(~year, nrow = 1) +
                theme_map() +
                theme( strip.background = element_rect(colour = "white", fill = "white"),
                       strip.text.x = element_text(size = 8, face ="bold"))

# save plot
  ggsave(temp_lot, file= "geobr_metros_1970-2001-2018.png", dpi = 300, width = 15, height = 6, units = "cm")