Skip to content

Instantly share code, notes, and snippets.

@schoulten
Created March 15, 2021 11:33
Show Gist options
  • Save schoulten/ca673122a587086afd606647dda7a3da to your computer and use it in GitHub Desktop.
Save schoulten/ca673122a587086afd606647dda7a3da to your computer and use it in GitHub Desktop.
Ministros da Saúde no Brasil: dias no cargo
# Load packages -----------------------------------------------------------
library(tidyverse)
library(magrittr)
library(readxl)
library(lubridate)
library(bbplot) # devtools::install_github('bbc/bbplot')
# Import data (from wikipedia page) ---------------------------------------
# URL: https://pt.wikipedia.org/wiki/Lista_de_ministros_da_Sa%C3%BAde_do_Brasil
ministers <- readxl::read_xlsx("wiki.xlsx") # accessed on 2021-03-14
# Data wrangling ----------------------------------------------------------
ministers$Nome[7] <- "Adib Jatene " # to avoid unwanted behavior on the geom_bar
ministers$Nome[8] <- "Jamil Haddad "
ministers %<>%
janitor::clean_names() %>%
mutate(
across(inicio:fim, ~stringr::str_replace(., "de", "")),
across(inicio:fim, ~lubridate::parse_date_time(., "%d %B %Y")),
days = as.numeric(fim - inicio),
presidente = recode(
presidente,
"Fernando Collor de Mello" = "Collor",
"Fernando Henrique Cardoso" = "FHC",
"Luiz Inácio Lula da Silva" = "Lula",
"José Sarney" = "Sarney",
"Itamar Franco" = "Itamar",
"Dilma Rousseff" = "Dilma",
"Michel Temer" = "Temer",
"Jair Bolsonaro" = "Bolsonaro"
) %>% as_factor(),
nome = as_factor(nome) %>%
recode(
"Carlos Corrêa de Menezes Sant'anna" = "Carlos C. M. Sant'anna",
"Luiz Carlos Borges da Silveira" = "Luiz C. B. da Silveira"
)
)
# Plot --------------------------------------------------------------------
plot = ministers %>%
ggplot(aes(nome, days, fill = presidente)) +
geom_bar(position = "dodge",stat = "identity") +
scale_fill_brewer(palette = "Dark2") +
coord_flip() +
guides(fill = guide_legend("Governo")) +
ggtitle("Ministros da Saúde: dias no cargo") +
bbc_style()
# BBC style
finalise_plot(
plot_name = plot,
source = "Fonte: Ministério da Saúde",
save_filepath = "plot.png",
width_pixels = 640,
height_pixels = 550
)
@schoulten
Copy link
Author

plot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment