Skip to content

Instantly share code, notes, and snippets.

@rafapereirabr
Last active May 23, 2021 18:54
Show Gist options
  • Save rafapereirabr/2d2b324f5ea34ce4b2dfb385be96cbc5 to your computer and use it in GitHub Desktop.
Save rafapereirabr/2d2b324f5ea34ce4b2dfb385be96cbc5 to your computer and use it in GitHub Desktop.
plot Brazilian metropolitan areas in different years

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")

@Wondtree
Copy link

Sorry for bothering you but it says Erro: package ‘digest’ does not have a namespace** I am doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment