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.
install.packages("geobr")
library(geobr)
library(sf)
library(magrittr)
library(dplyr)
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
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()