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

@rafapereirabr
Copy link
Author

geobr_metros_1970-2001-2018

@schmert
Copy link

schmert commented Jan 3, 2020

Is all of SC a metro area??

@rafapereirabr
Copy link
Author

rafapereirabr commented Jan 3, 2020 via email

@schmert
Copy link

schmert commented Jan 6, 2020

Does this work with geobr 1.1, or only with a new version? (I tried running the code above, in order to later modify it and look at SC in detail, but with the script above I'm getting "could not find function read_metro_area". )

@rafapereirabr
Copy link
Author

Hi Carl. It doesn't work with geobr 1.1. The fuction read_metro_area() is in the dev version. If you'd like to test geobr 1.2 (rev), you need to reinstall it:

  utils::remove.packages('geobr')
  devtools::install_github("ipeaGIT/geobr", subdir = "r-package")
  library(geobr)

@schmert
Copy link

schmert commented Jan 6, 2020

Thanks, Rafa. It worked after (1) removing the old version, then (2) installing the dev version.

The sub-areas within metros are municípios?

SC

@rafapereirabr
Copy link
Author

I'm glad to hear it worked! Yes, the function returns the shapes of municipalities grouped by their respective metro areas.

@schmert
Copy link

schmert commented Jan 6, 2020

Yeah, I think this was a re-run of an earlier conversation that we had. I know to check by installing the dev version, but apparently that doesn't work (at least on my machine) until the CRAN version is uninstalled. Thanks again for the great work on geobr.

@rafapereirabr
Copy link
Author

Thank you for all the support, Carl !

@Wondtree
Copy link

Hi, I'm new in R programming and I'm trying to plotting your code, I installed all the packages it shows as library, but it get an error in the "geometry". I just copy and paste your code but it don't work, can you help?

@rafapereirabr
Copy link
Author

Hi, I'm new in R programming and I'm trying to plotting your code, I installed all the packages it shows as library, but it get an error in the "geometry". I just copy and paste your code but it don't work, can you help?

There was a small error in the code. I've removed the word geometry from line 14. It should work now.

@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