Skip to content

Instantly share code, notes, and snippets.

@fernandobarbalho
Created November 4, 2022 13:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fernandobarbalho/63788a72a2fd3ce1d42249b5612ea8a4 to your computer and use it in GitHub Desktop.
Save fernandobarbalho/63788a72a2fd3ce1d42249b5612ea8a4 to your computer and use it in GitHub Desktop.
Gera mapa do Brasil deformado de acordo com variação da votação do PT entre 2018 e 2022 (2º turno)
library(readr)
library(tidyverse)
library(data.table)
library(geobr)
library(cartogram)
library(sf)
library(colorspace)
library(gganimate)
tema <- function(){
theme_minimal() +
theme(
text = element_text(family = "Lora", colour = "grey20"),
title = element_text(size = 10, color = "dimgrey", face = "plain"),
plot.subtitle = element_text(color = "grey20", face = "plain", size = 10),
axis.text = element_text(colour = "grey20", size = 8, family = "Source Sans Pro"),
plot.caption = element_text(face = "italic"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks = element_line(size = 0.4),
axis.ticks.length = unit(.2, "cm"),
axis.title = element_text(size = 8, colour = "grey20"),
legend.position = 'none',
legend.text = element_text(size = 8, family = "Source Sans Pro"),
legend.title = element_text(size = 9, family = "Source Sans Pro")
)
}
tema_mapa <- function() {
tema() +
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = "none",
legend.text = element_text(size = 10),
plot.background = element_blank(),
panel.background = element_blank())
}
votacao_candidato_munzona_2022_BR <- read_delim("votacao_candidato_munzona_2022/votacao_candidato_munzona_2022_BR.csv",
delim = ";", escape_double = FALSE, locale = locale(encoding = "LATIN1"),
trim_ws = TRUE)
votacao_presidente_2022<-
votacao_candidato_munzona_2022_BR %>%
filter(NR_TURNO==2,
CD_CARGO == 1) %>%
group_by(SG_UF,
SG_PARTIDO) %>%
summarise(
quantidade_voto_partido =sum(QT_VOTOS_NOMINAIS_VALIDOS)
)
votacao_pt_percentual<-
votacao_presidente_2022 %>%
filter(SG_PARTIDO == "PT") %>%
inner_join(
votacao_candidato_munzona_2022_BR %>%
filter(NR_TURNO==2,
CD_CARGO == 1) %>%
group_by(SG_UF) %>%
summarise(
quantidade_voto_total =sum(QT_VOTOS_NOMINAIS_VALIDOS)
)) %>%
mutate(perc_pt = (quantidade_voto_partido/quantidade_voto_total)*100 )
votacao_secao_2018_BR <- read_delim("votacao_secao_2018_BR/votacao_secao_2018_BR.csv",
delim = ";", escape_double = FALSE, locale = locale(encoding = "LATIN1"),
trim_ws = TRUE)
votacao_secao_2018_BR <-
data.table::fread("votacao_secao_2018_BR/votacao_secao_2018_BR.csv",
sep= ";",
encoding = "Latin-1")
votacao_presidente_2018<-
votacao_secao_2018_BR %>%
filter(NR_TURNO==2,
CD_CARGO == 1) %>%
group_by(SG_UF,
NM_VOTAVEL) %>%
summarise(
quantidade_voto_partido =sum(QT_VOTOS)
)
votacao_pt_percentual_2018<-
votacao_presidente_2018 %>%
filter(NM_VOTAVEL == "FERNANDO HADDAD") %>%
mutate(SG_PARTIDO = "PT" ) %>%
inner_join(
votacao_secao_2018_BR %>%
filter(NR_TURNO==2,
CD_CARGO == 1) %>%
group_by(SG_UF) %>%
summarise(
quantidade_voto_total =sum(QT_VOTOS)
)) %>%
mutate(perc_pt_2018 = (quantidade_voto_partido/quantidade_voto_total)*100 )
votacao_variacao<-
votacao_pt_percentual %>%
filter(SG_UF != "ZZ") %>%
inner_join(votacao_pt_percentual_2018 %>%
select(SG_UF,
SG_PARTIDO,
perc_pt_2018)) %>%
mutate(var_h = ((perc_pt-perc_pt_2018)/perc_pt_2018)*100 )
mapa<- geobr::read_state()
qde_estados<- dados_selecionados %>%
count(Estado)
mapa_cartograma <- mapa %>%
inner_join(votacao_variacao, by = c("abbrev_state" = "SG_UF"))
#mp_sf <- as_Spatial(mapa_cartograma)
mp_sf <- mapa_cartograma %>%
st_as_sf() %>%
st_transform(crs = 29101) #5641
mapa_deform <- cartogram_cont(mp_sf, 'var_h', 3)
mp_def <- sf::st_as_sf(mapa_deform)
#mp_def<- st_cast(mapa_deform, "MULTIPOLYGON")
# testa gif transição entre mapas normal e deformado.
mp_def_join <- mp_def %>%
mutate(tipo_geometria = "deformada")
mp_nor_join <- mp_sf %>%
mutate(tipo_geometria = "normal")
mp_duplo <- rbind(mp_def_join, mp_nor_join)
mapa_duplo_gif <-
mp_duplo %>%
ggplot( aes(fill = var_h, group = code_state)) +
geom_sf(color = "lightblue") +
ease_aes("cubic-in-out") +
#scale_fill_viridis(option = "viridis", direction = -1) +
scale_fill_continuous_sequential(palette= "Reds 3")+
labs(x = NULL, y = NULL) +
tema_mapa() +
transition_states(states = tipo_geometria,
transition_length = 1,
state_length = 1)
animate(mapa_duplo_gif, fps = 8, renderer = gifski_renderer())
anim_save("cartograma.gif", animation = last_animation())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment