Skip to content

Instantly share code, notes, and snippets.

@gabrielzanlorenssi
Last active September 5, 2022 01:16
Show Gist options
  • Save gabrielzanlorenssi/395a63eaeacdfc3ae50af72b63e44e4e to your computer and use it in GitHub Desktop.
Save gabrielzanlorenssi/395a63eaeacdfc3ae50af72b63e44e4e to your computer and use it in GitHub Desktop.
codigo_pesquisas_ipec
# atenção: código escrito rapidamente. não foi revisado para deixar mais legível
# Library -----------------------------------------------------------------
library(tidyverse)
library(sf)
library(readxl)
library(googlesheets4)
library(geobr)
# Read file ---------------------------------------------------------------
url = "https://docs.google.com/spreadsheets/d/1BQnJnRipZQC_FR20vVoRf9lZGp6PdbRvvPvlGkAZyDk/edit#gid=0"
data = read_sheet(url)
# Chart -------------------------------------------------------------------
data %>%
mutate(Quem2 = case_when(Quem=="LULA" & Lula>=50 ~ "Lula 50+",
Quem=="LULA" & Lula<50 ~ "Lula 50-",
Quem=="BOLSONARO" & Bolsonaro>=50 ~ "Bolsonaro 50+",
Quem=="BOLSONARO" & Bolsonaro<50 ~ "Bolsonaro 50-",
TRUE ~ Quem)) %>%
# pode usar alternativamente o mapa do geobr
right_join(nexo.utils::mapState, by="ibge2") %>%
#right_join(geobr::read_state(), by=c("ibge2"="code_state")) %>%
st_as_sf() %>%
ggplot(aes(fill=Quem2)) +
geom_sf()
#-- chart
data %>%
mutate(Quem2 = case_when(Quem=="LULA" & Lula>=50 ~ "Lula 50+",
Quem=="LULA" & Lula<50 ~ "Lula 50-",
Quem=="BOLSONARO" & Bolsonaro>=50 ~ "Bolsonaro 50+",
Quem=="BOLSONARO" & Bolsonaro<50 ~ "Bolsonaro 50-",
TRUE ~ Quem)) %>%
mutate(Outros = 100-Lula-Bolsonaro-Ciro-Tebet-BNI, l=Lula) %>%
pivot_longer(cols=c(Lula:BNI,Outros)) %>%
mutate(r = str_sub(ibge2,1,1),
name=fct_rev(fct_relevel(name, "Lula", "Ciro", "Tebet", "Outros",
"BNI", "Bolsonaro")),
r = fct_relevel(r, '2','3','4','5','1')) %>%
mutate(v=case_when(name=="Lula" ~ 5,
name=="Bolsonaro" ~ 95,
TRUE ~ NA_real_)) %>%
ggplot(aes(x=reorder(uf, l), y=value, fill=name)) +
geom_col() +
coord_flip() +
geom_text(aes(label=paste0(value, "%"), y=v)) +
facet_grid(rows=vars(r), space = "free", scales="free") +
scale_fill_manual(values=c('purple', 'grey60', 'orange',
'yellow', 'pink', 'red'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment