Monday, July 1, 2019

geobr: shapefiles and official spatial data sets of Brazil in R

In 2012, I published here a blog post about where to find shapefiles of Brazil. Since then, this has become one of the most popular posts in 9 years of the blog. However, the links to the original data sets change every now and then, and it gets a bit tricky to find the most up to date data. My team and I at the Institute for Applied Economic Research (Ipea) have created geobr, an R package that allows users to easily access shapefiles of the Brazilian Institute of Geography and Statistics (IBGE) and other official spatial data sets of Brazil.

The geobr package currently includes a variety of data sets, such as the shapefiles of municipalities and states (from 1872 to 2018), census weighting areas, a spatial grid with population count at a resolution of 200 x 200 meters, a geolocated database of health facilities in the country etc. All the data sets are read into R as sf data. We will gradually add other databases to the package, but feel free to make specific requests and suggestions by opening new issues on the GitHub page of geobr or tweeting the hashtag #geobr.

The advantage of geobr: Intuitive syntax that provides easy and quick access to a wide variety shapefiles and official spatial data sets with updated geometries for various years using harmonazied attributes and geographic projections across geographies and years.




Here is a quick intro to geobr:


Quick intro to geobr logo

geobr is an R package that allows users to easily download shapefiles of the Brazilian Institute of Geography and Statistics (IBGE) and other official spatial data sets of Brazil.

Installation

 install.packages("geobr")
  
  library(geobr)
  library(sf)
  library(magrittr)
  library(dplyr)

Basic Usage

The syntax of all geobr functions operate one the same logic. Here is a quick sample of a few functions and how to use them:

Read an specific geographic area at a given year

state <- read_state(code_state=11, year=2000)              # State
micro <- read_micro_region(code_micro=110205, year=2000)   # Micro region
munic <- read_municipality(code_muni=1200179, year=2017)   # Municipality
...

Read all geographic areas within a state at a given year

micro <- read_micro_region(code_micro=15, year=2013)       # Micro region
munic <- read_municipality(code_muni= 33, year=2010)       # Municipality

# Or simply use the two-digit abbreviation of a state
  micro <- read_micro_region(code_micro="PA", year=2000)   # Micro region
  munic <- read_municipality(code_muni= "RJ", year=2010)   # Municipality


Read all geographic areas in the country

state <- read_state(code_state="all", year=2000)           # State
micro <- read_micro_region(code_micro="all", year=2015)    # Micro region
munic <- read_municipality(code_muni="all", year=2018)     # Municipality

Plot the data

It's extremely simple to plot sf spataial data using ggplot2::geom_sf(). But let's make a nice plot to introduce geobr using data at various scales in the same figure.

library(ggplot2)
library(sf)
library(cowplot)
library(sysfonts)
library(grid)
library(beepr)

# download data
  y <- 2010
  state <- read_state(code_state="all", year=y)
  mesor <- read_meso_region(code_meso="all", year=y)
  micro <- read_micro_region(code_micro="all", year=y)
  munic <- read_municipality(code_muni="all", year=y)

# No plot axis
  no_axis <- theme(axis.title=element_blank(),
                   axis.text=element_blank(),
                   axis.ticks=element_blank())


# individual plots
  p_state <- ggplot() + geom_sf(data=state, fill="#2D3E50", color="#FEBF57", size=.15, show.legend = FALSE) + 
    theme_minimal() +
    no_axis +
    labs(subtitle="States", size=8)

  
  p_mesor <- ggplot() + geom_sf(data=mesor, fill="#2D3E50", color="#FEBF57", size=.15, show.legend = FALSE) + 
    theme_minimal() +
    no_axis +
    labs(subtitle="Meso regions", size=8)
  
  p_micro <- ggplot() + geom_sf(data=micro, fill="#2D3E50", color="#FEBF57", size=.15, show.legend = FALSE) + 
    theme_minimal() +
    no_axis +
    labs(subtitle="Micro regions", size=8)
  
  p_munic <- ggplot() + geom_sf(data=munic, fill="#2D3E50", color="#FEBF57", size=.05, show.legend = FALSE) + 
    theme_minimal() +
    no_axis +
    labs(subtitle="Municipalities", size=8)

  
# Arrange plots
  p <- plot_grid(p_state, p_mesor, p_micro, p_munic, ncol = 2) #+ p_micro, p_munic
  
# add annotation
  sysfonts::font_add_google(name = "Roboto", family = "Roboto") # add special text font
  t1 <- grid::textGrob(expression(bold("geobr:")), 
        gp = gpar(fontsize=15, col="#2D3E50", fontfamily = "Roboto"), x = 0.1, y = .02)
  t2 <- grid::textGrob(expression(underline("https://github.com/ipeaGIT/geobr")), 
        gp = gpar(fontsize=10, col="#000066"), x = 0.34, y = .02)
  my_note <- annotation_custom(grobTree(t1, t2))
  s <- p + my_note

# Save plot
  ggsave(s, filename = "./plot_geobr_intro.png", width = 6, height = 6,  dpi = 300)
  beepr::beep()

view raw geobr_intro.md hosted with ❤ by GitHub