Evolução da Divisão Territorial do Brasil

Página de Rosto

Página de dados

Load libraries

library(tidyverse)
## -- Attaching packages ------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.2.1     v purrr   0.3.3
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   1.0.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts ---------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(gganimate)
library(janitor)
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(geobr)
## Loading required namespace: sf

Load data

# Municipalities population
temp = tempfile(fileext = ".xls")
dadosUrl2 <- "ftp://geoftp.ibge.gov.br/organizacao_do_territorio/estrutura_territorial/evolucao_da_divisao_territorial_do_brasil/evolucao_da_divisao_territorial_do_brasil_1872_2010/municipios_1872_1991/Documentacao/pop_mun_TUR_1872_a_2010.xls"
download.file(dadosUrl2, destfile = temp, mode = "wb")

dados <- readxl::read_excel(temp, col_types= ) %>% 
  clean_names()
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AI3210 / R3210C35: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AJ3210 / R3210C36: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3210 / R3210C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3238 / R3238C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3259 / R3259C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3264 / R3264C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3266 / R3266C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3292 / R3292C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3407 / R3407C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3543 / R3543C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3569 / R3569C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3673 / R3673C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3831 / R3831C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AK3874 / R3874C37: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AN4032 / R4032C40: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AO4032 / R4032C41: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Expecting numeric in AY4914 / R4914C51: got '-'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Coercing text to numeric in AB5151 / R5151C28: '5103109'
## Warning in read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, :
## Coercing text to numeric in AG5151 / R5151C33: '5103109'
# States map
map_state <- geobr::read_state()
## Using data from year 2010
## Loading data for the whole country
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |======================================================================| 100%
# Point in the municipalities
map_lon_lat <- rio::import("https://github.com/italocegatta/brmap/raw/master/data-raw/municipio_lat_lon.xlsx")

Set theme

theme_map <- function(){ 
  theme_classic() + 
  theme(
    panel.grid.minor = element_blank(),
    panel.grid.major = element_blank(),
    plot.title       = element_text(color = "lightgrey", hjust = 0.5, size =25),
    panel.background = element_rect(fill = "#000000"),
    plot.background  = element_rect(fill = "#000000"),
    legend.position  = "none",
    axis.ticks       = element_blank(),
    axis.text        = element_blank(),
    axis.line        = element_blank())
}  

First manipulation

# 2010 year contain the decimal separator ".", different from the rest, that
# in the original database comes with "," as decimal and when we load it 
# the "," desappear, but not the "."
dados <- dados %>% 
  mutate(pop_tot_2010 = str_replace_all(pop_tot_2010, "[.]", ""))  
  
# Select and pivot_table
dados <- dados %>% 
  select(codigo, starts_with ("pop_tot")) %>%
  mutate_at(vars(starts_with ("pop_tot")), as.integer) %>% 
  pivot_longer(-codigo,names_to="pre_year", values_to= "qt") %>% 
  mutate(pre_year = recode(pre_year, pop_total = "pop_tot_1872")) %>% 
  mutate(year = str_extract(pre_year, pattern = "[0-9]+")) %>%
  mutate(year = as.integer(year)) %>% 
  select(1,4,3)
## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion
# Checar
dados %>%
  group_by(year) %>% 
  summarise(n = sum(qt, na.rm=T)) %>% 
  ggplot()+
  geom_line(aes(x=year, y = n))+
  theme_classic()

Join map data

dados_map <- map_lon_lat %>% 
  select(1,3,4) %>% 
  mutate(municipio_cod = as.character(municipio_cod)) %>% 
  inner_join(dados, by = c("municipio_cod" = "codigo"))

Maps

# Static state map
static_map <- map_state %>% 
  ggplot()+
  geom_sf(fill=NA, 
          color = alpha("#ccff00", .4), lty = 3)+
  theme_map()

# Animated map
anim <- static_map + 
  geom_point(data= dados_map, aes(x=lon, y =lat, size = qt), 
             colour = "#cffdfd", alpha = 0.5, shape = 19, inherit.aes=F) +
  scale_size_area(max_size = 30) +
  labs(title = "Year: {frame_time}")+
  transition_time(as.integer(year)) +
  ease_aes('linear')

Creat animation

animate(anim, end_pause = 10, duration = 40)
## Warning: Removed 4992 rows containing missing values (geom_point).

## Warning: Removed 4992 rows containing missing values (geom_point).
## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).

## Warning: Removed 4998 rows containing missing values (geom_point).
## Warning: Removed 4539 rows containing missing values (geom_point).
## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).

## Warning: Removed 4550 rows containing missing values (geom_point).
## Warning: Removed 4332 rows containing missing values (geom_point).
## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).

## Warning: Removed 4395 rows containing missing values (geom_point).
## Warning: Removed 4045 rows containing missing values (geom_point).
## Warning: Removed 4047 rows containing missing values (geom_point).

## Warning: Removed 4047 rows containing missing values (geom_point).

## Warning: Removed 4047 rows containing missing values (geom_point).

## Warning: Removed 4047 rows containing missing values (geom_point).

## Warning: Removed 4047 rows containing missing values (geom_point).
## Warning: Removed 3729 rows containing missing values (geom_point).

## Warning: Removed 3729 rows containing missing values (geom_point).

## Warning: Removed 3729 rows containing missing values (geom_point).

## Warning: Removed 3729 rows containing missing values (geom_point).

## Warning: Removed 3729 rows containing missing values (geom_point).

## Warning: Removed 3729 rows containing missing values (geom_point).

## Warning: Removed 3729 rows containing missing values (geom_point).
## Warning: Removed 2855 rows containing missing values (geom_point).
## Warning: Removed 2857 rows containing missing values (geom_point).

## Warning: Removed 2857 rows containing missing values (geom_point).

## Warning: Removed 2857 rows containing missing values (geom_point).

## Warning: Removed 2857 rows containing missing values (geom_point).

## Warning: Removed 2857 rows containing missing values (geom_point).
## Warning: Removed 1668 rows containing missing values (geom_point).

## Warning: Removed 1668 rows containing missing values (geom_point).

## Warning: Removed 1668 rows containing missing values (geom_point).

## Warning: Removed 1668 rows containing missing values (geom_point).

## Warning: Removed 1668 rows containing missing values (geom_point).

## Warning: Removed 1668 rows containing missing values (geom_point).

## Warning: Removed 1668 rows containing missing values (geom_point).
## Warning: Removed 1628 rows containing missing values (geom_point).
## Warning: Removed 1680 rows containing missing values (geom_point).

## Warning: Removed 1680 rows containing missing values (geom_point).

## Warning: Removed 1680 rows containing missing values (geom_point).

## Warning: Removed 1680 rows containing missing values (geom_point).

## Warning: Removed 1680 rows containing missing values (geom_point).

## Warning: Removed 1680 rows containing missing values (geom_point).
## Warning: Removed 1128 rows containing missing values (geom_point).

## Warning: Removed 1128 rows containing missing values (geom_point).

## Warning: Removed 1128 rows containing missing values (geom_point).

## Warning: Removed 1128 rows containing missing values (geom_point).

## Warning: Removed 1128 rows containing missing values (geom_point).

## Warning: Removed 1128 rows containing missing values (geom_point).
## Warning: Removed 112 rows containing missing values (geom_point).

## Warning: Removed 112 rows containing missing values (geom_point).

## Warning: Removed 112 rows containing missing values (geom_point).

## Warning: Removed 112 rows containing missing values (geom_point).

## Warning: Removed 112 rows containing missing values (geom_point).

## Warning: Removed 112 rows containing missing values (geom_point).
## Warning: Removed 54 rows containing missing values (geom_point).