Skip to content

Instantly share code, notes, and snippets.

@gonzalezgouveia
Created May 26, 2019 08:24
Show Gist options
  • Save gonzalezgouveia/1b96a155943b7f12b82a0532a83d851f to your computer and use it in GitHub Desktop.
Save gonzalezgouveia/1b96a155943b7f12b82a0532a83d851f to your computer and use it in GitHub Desktop.
library(tidyverse)
library(gganimate)
library(ggrepel)
library(ggthemes)
# Hecho por Rafa @GonzalezGouveia
# Con gusto para #DatosDeMiercoles, propuesto por @R4DS_es
# colores del tema
ltgray <- "#cccccc"
dkgray <- "#757575"
dkgray2 <- "#666666"
# descarga de csv de datosDeMiercoles
data_oms <- read_csv('./data/oms.csv')
# preparacion de datos
tidy_oms <- data_oms %>%
filter(anio >= 1995,
anio <= 2012) %>%
select(1:46) %>% # excluyendo recaidas
gather('tipos_de_casos', 'casos', 5:46) %>%
separate('tipos_de_casos',
c('nuevos', 'tipo', 'generoedad'),
sep = '_') %>%
mutate(genero = substr(generoedad,1,1)) %>%
group_by(anio, genero) %>%
summarise(suma_casos = sum(casos, na.rm = TRUE))
# para agregar la media en el medio
mean_by_year <- tidy_oms %>%
group_by(anio) %>%
summarise(suma_casos = mean(suma_casos),
genero = 'media')
# creando animacion
anim <- tidy_oms %>%
ggplot(aes(x = anio, y = suma_casos/1000000, color = genero, label = genero)) +
geom_line(size = 2) +
geom_point(size = 2) +
geom_label_repel(direction = "y",
hjust = 1,
nudge_x = 50,
segment.size = 1,
size = 10) +
geom_point(data = mean_by_year) +
geom_label(data = mean_by_year,
aes(label = paste(
'Media: ', round(suma_casos/1000000, digits = 1), ' M')
),
size = 10) +
labs(title = 'Casos de tuberculosis parecen ir en aumento',
x = 'Años',
y = 'Casos (Millones)') +
theme_gdocs() +
theme(legend.position = "none",
axis.title = element_text(face = "plain", colour = dkgray2,
size = 20),
axis.text = element_text(face = "plain",
colour = dkgray, size = 20)
) +
transition_reveal(anio)
# animando
animate(anim, fps = 50, nframes = 500)
# Hecho por Rafa @GonzalezGouveia
# Con gusto para #DatosDeMiercoles, propuesto por @R4DS_es
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment