Skip to content

Instantly share code, notes, and snippets.

@gabrielzanlorenssi
Created November 17, 2019 22:35
Show Gist options
  • Save gabrielzanlorenssi/d8198895fe5535c1a4ef9e9540936fcc to your computer and use it in GitHub Desktop.
Save gabrielzanlorenssi/d8198895fe5535c1a4ef9e9540936fcc to your computer and use it in GitHub Desktop.
# Setwd -------------------------------------------------------------------
setwd("605. Picadas de animais/")
# Libraries ---------------------------------------------------------------
library(tidyverse)
library(readxl)
library(sf)
# Read data ---------------------------------------------------------------
obitos <- read_xls("obitos.xls")
municipios <- read_xls("municipios.xls")
mes <- read_xls("mes.xls")
genero <- read_xls("genero.xls")
faixa_etaria <- read_xls("faixa_etaria.xls")
# Graphs ------------------------------------------------------------------
## totais de picadas
obitos %>%
filter(.[[1]]!="Ign/Branco" & .[[1]]!="Total") %>%
mutate(Obito = `Óbito pelo agravo notificado`+`Óbito por outra causa`) %>%
mutate(total = (`Ign/Branco`+Cura+Obito)) %>%
ggplot(aes(x=reorder(`Tipo de Acidente`, total), y=total)) +
geom_col() +
scale_y_continuous() +
coord_flip()
ext.functions::extplot("Total de notificacoes por tipo de animal", 1, 605)
obitos %>%
filter(.[[1]]!="Ign/Branco" & .[[1]]!="Total") %>%
mutate(total = `Óbito pelo agravo notificado`+`Óbito por outra causa`) %>%
ggplot(aes(x=reorder(`Tipo de Acidente`, total), y=total)) +
geom_col() +
scale_y_continuous() +
coord_flip()
ext.functions::extplot("Total de obitos por tipo de animal", 2, 605)
## obitos
obitos %>%
filter(.[[1]]!="Ign/Branco") %>%
mutate(Obito = `Óbito pelo agravo notificado`+`Óbito por outra causa`) %>%
gather(situacao, n, `Ign/Branco`, Cura, Obito) %>%
ggplot(aes(x=`Tipo de Acidente`, y=n, fill=situacao)) +
geom_col(position="fill") +
coord_flip()
ext.functions::extplot("Situação por tipo de animal", 3, 605)
obitos %>%
filter(.[[1]]!="Ign/Branco") %>%
mutate(Obito = `Óbito pelo agravo notificado`+`Óbito por outra causa`) %>%
mutate(perc_obito = Obito / (`Ign/Branco`+Cura+Obito)) %>%
ggplot(aes(x=reorder(`Tipo de Acidente`, perc_obito), y=perc_obito)) +
geom_col() +
scale_y_continuous(labels=scales::percent) +
coord_flip()
ext.functions::extplot("Total de óbitos / Total de notificações por animais", 4, 605)
## mes
colnames(mes)[2:13] <- 1:12
mes %>%
filter(.[[1]]!="Ign/Branco" & .[[1]]!="Total") %>%
gather(mes, valor, `1`:`12`) %>%
mutate(mes = as.numeric(mes)) %>%
mutate(perc = valor/Total) %>%
ggplot(aes(x=mes, y=perc)) +
geom_line(aes(group=`Tipo de Acidente`, col=`Tipo de Acidente`)) +
scale_x_continuous(breaks = c(1,3,5,7,9,11))
ext.functions::extplot("Total de notificações por mês e tipo de animal", 5, 605)
## genero
genero %>%
gather(genero, valor, Ignorado:Feminino) %>%
filter(`Tipo de Acidente`!="Ign/Branco") %>%
mutate(Tipo = factor(`Tipo de Acidente`, levels=c("Escorpião", "Aranha", "Lagarta", "Outros", "Abelha", "Serpente", "Total"))) %>%
ggplot(aes(x=Tipo, y=valor, fill=genero)) +
geom_col(position="fill") +
scale_y_continuous(labels=scales::percent) +
coord_flip()
ext.functions::extplot("Total de notificações por gênero e tipo de animal", 6, 605)
## casos totais por faixa etária
faixa_etaria %>%
filter(.[[2]]!="Total") %>%
filter(`Faixa Etária`!="Em branco/IGN") %>%
mutate(Faixa = case_when(`Faixa Etária` %in% c("<1 Ano", "1-4", "5-9", "10-14", "15-19") ~ "19 ou menos",
`Faixa Etária` %in% c("20-39", "40-59") ~ `Faixa Etária`,
TRUE ~ "60 mais")) %>%
gather(animal, valor, Serpente:Outros) %>%
filter(animal != "Total") %>%
group_by(Faixa) %>%
summarise(total = sum(valor, na.rm=T)) %>%
ggplot(aes(x=(Faixa), y=total)) +
geom_col() +
scale_y_continuous(labels=scales::comma)
ext.functions::extplot("Total de notificações por faixa etária", 7, 605)
## faixa etaria por tipo
faixa_etaria %>%
filter(.[[2]]!="Total") %>%
gather(animal, valor, Serpente:Outros) %>%
group_by(animal) %>%
mutate(total = sum(valor, na.rm=T)) %>%
mutate(perc = valor/total) %>%
ggplot(aes(x=reorder(`Faixa Etária`, Order), y=perc)) +
geom_line(aes(group=animal, col=animal))
ext.functions::extplot("Total de notificações por faixa etária e tipo de animal -- NÃO UTILIZAR", 8, 605)
# Mapas -------------------------------------------------------------------
library(sf)
# shape
brazil <- geobr::brazil_2010 %>%
select(code_muni) %>%
mutate(cod6 = as.numeric(str_sub(code_muni, 1,6)))
# pop
pop <- read.csv("../datasets/populacao_municipios.csv") %>%
tbl_df() %>%
filter(between(ano, 2007, 2017)) %>%
filter(!is.na(populacao) & populacao>0) %>%
group_by(cod_ibge, uf) %>%
summarise(n=n(), pop=sum(populacao, na.rm=T)) %>%
mutate(media = pop/n)
# final
municipios %>%
separate(`Munic. Ocorrência`, c("cod6", "cidade"), sep=" ", extra="merge") %>%
mutate(cod6 = as.numeric(cod6)) %>%
left_join(pop, by=c("cod6"="cod_ibge")) %>%
gather(var, val, Serpente:Total) %>%
mutate(val = replace_na(as.numeric(val), 0)) %>%
mutate(cem_mil = val * 100000 / media) %>%
left_join(brazil, by=c("cod6"="cod6")) %>%
mutate(cem_mil_cat = cut(cem_mil, breaks = c(-Inf,100,500,1000,2500,5000, Inf))) -> final
## mapas
animais <- unique(final$var)
for (i in seq_along(animais)) {
final %>%
filter(var == animais[i]) %>%
ggplot() +
geom_sf(aes(fill=cem_mil_cat), col=NA) +
scale_fill_viridis_d(drop=F)
ext.functions::extplot(paste0("total de casos - ", animais[i]), 20+i, 605)
}
## principal
final %>%
filter(var!="Total") %>%
group_by(cod6) %>%
mutate(rank = rank(-val)) %>%
filter(rank==1) %>%
ggplot() +
geom_sf(aes(fill=var), col=NA)
ext.functions::extplot("Principal animal", 30, 605)
# Por estado --------------------------------------------------------------
final %>%
filter(var!="Total") %>%
mutate(regiao = str_sub(cod6,1,1)) %>%
filter(!is.na(regiao)) %>%
filter(!is.na(uf)) %>%
group_by(uf, var, regiao) %>%
summarise(val = sum(val, na.rm=T)) -> Tt
Tt %>%
ggplot() +
geom_col(aes(x=reorder(uf, as.numeric(regiao)), y=val, fill=var),position = "fill") +
coord_flip()
ext.functions::extplot("Por uf", 31, 605)
# Close wd ----------------------------------------------------------------
setwd("..")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment