Skip to content

Instantly share code, notes, and snippets.

@fernandobarbalho
Created July 4, 2020 22:18
Show Gist options
  • Save fernandobarbalho/bb71d5625af0722b71e2734a2c84eafc to your computer and use it in GitHub Desktop.
Save fernandobarbalho/bb71d5625af0722b71e2734a2c84eafc to your computer and use it in GitHub Desktop.
library(readxl)
library(readr)
library(tidyverse)
library(lubridate)
library(rsiconfi)
arquivo_geral <- read_excel("HIST_PAINEL_COVIDBR_30jun2020.xlsx",
col_types = c("text", "text", "numeric",
"text", "numeric", "numeric", "numeric",
"date", "numeric", "text", "numeric",
"numeric", "numeric", "numeric",
"numeric", "numeric", "numeric"))
mortos_dia_covid <-
arquivo_geral %>%
filter(regiao=="Brasil") %>%
#as.Date(as.numeric(series_temporais_analise$Data), origin="1899-12-30")
#mutate(data = as.Date(as.numeric(data), origin = "1899-12-30")) %>%
group_by(data) %>%
summarise(
obitosAcumulados = sum(obitosAcumulado)
)
#Para o dia 01/07. o dado teve que ser incluído na mão
mortos_dia_covid<-
mortos_dia_covid%>%
mutate(data= date(data)) %>%
bind_rows(tibble(data= date("2020-07-01"),obitosAcumulados = 60632))
municipios_IBGE<-
municipios_IBGE %>%
mutate(pop_estimada = as.numeric(stringr::str_remove_all(pop_estimada,",")))
df_cross_covid_cities<-
crossing(mortos_dia_covid, municipios_IBGE) %>%
mutate(COVID = "Cumulative death by date" )
df_cross_covid_cities$tipo <- "Municípios brasileiros"
municipios_IBGE$tipo <- "Municípios brasileiros"
library(gganimate)
library(ggrepel)
df_cross_covid_cities<-
df_cross_covid_cities %>%
mutate(ultrapassou = ifelse(obitosAcumulados>pop_estimada,1,0))
df_maior_pop<-
df_cross_covid_cities %>%
filter(ultrapassou == 1) %>%
group_by(data) %>%
summarise(
pop_estimada = max(pop_estimada)
) %>%
mutate(tipo = "Municípios brasileiros" )
dates<- c(lubridate::ymd("2020-02-25"):lubridate::ymd("2020-04-08"))#"2020-04-10"
dates<-as.Date(dates, origin= "1970-01-01")
names(df_maior_pop)
df_maior_pop<-
df_maior_pop %>%
bind_rows(tibble(data= dates,pop_estimada = 0,tipo= "Municípios brasileiros" ))
df_maior_pop<-
df_maior_pop %>%
left_join(df_cross_covid_cities) %>%
rename(municipio_pop = nome_municipio) %>%
mutate(obitosAcumulados= ifelse(is.na(obitosAcumulados),0,obitosAcumulados))
df_animate<-
df_cross_covid_cities %>%
filter(data>="2020-04-09")
#left_join(df_maior_pop)
# df_animate<-
# df_cross_covid_cities %>%
# filter(data<= "2020-04-16")
df_maior_pop_anim<-
df_maior_pop %>%
filter(data>="2020-04-09")
#filter(data<= "2020-04-16")
names(df_maior_pop)
p<-
df_animate %>%
#filter(data== "2020-06-16") %>%
mutate(data_ocorr = data) %>%
mutate(ultrapassou = ifelse(obitosAcumulados>pop_estimada,1,0)) %>%
#filter(data_ocorr>= "2020-04-09")%>%
ggplot(aes(x= tipo, y= pop_estimada, color= as.factor(ultrapassou) )) +
geom_jitter(show.legend = FALSE, position = position_jitter(width = 0.32, height = 0, seed = 1972) )+ # width=0.32, height =0
geom_text(data= df_maior_pop_anim,aes(x=1.32, label = paste0(municipio_pop,"-", uf,": ", pop_estimada )),color= "black", show.legend = FALSE, nudge_y = 0, hjust = 0 ) + #%>% filter(data == "2020-06-16")
geom_text(data= df_maior_pop_anim,aes(x=1,y=10.1^7, label = paste0("Óbitos acumulados: ", obitosAcumulados )),color= "black", show.legend = FALSE, nudge_y = 0.2 ) + #%>% filter(data == "2020-06-16")
scale_discrete_manual ("colour",values= c('lightgray', "#b5403e" ))+
scale_y_log10(labels=function(x) format(x, big.mark = ".", scientific = FALSE)) +
theme_light(
)+
labs(
#title = paste("Cumulative deaths by COVID:", max(df_cross_covid_others$obitosAcumulados)),
y = "População dos municípios"
)+
theme(
panel.grid = element_blank()
)
p
library(transformr)
library(gifski)
library(png)
anim<-
p + transition_time(data)
anim<-
anim+
labs(title = paste("Date: {format(frame_time, '%d/%m/%Y')}"))
anim_gif<- animate(anim, renderer = gifski_renderer(), fps= 1.5, end_pause = 15, nframes=NROW(df_maior_pop_anim), height = 500, width = 866)
anim_save("covid_cities.gif",animation = anim_gif)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment