Results of the 2018 Brazilian Presidential election

Fábio N. Demarqui

This vignette aims to show how the results of the 2018 Brazilian presidential election can be combined with data from the COVID-19 pandemic.

library(covid19br)
library(tidyverse)

# loading the election data:
data(election2018Cities)

# looking at the data:
glimpse(election2018Cities)
#> Rows: 5,570
#> Columns: 11
#> $ region           <chr> "North", "North", "North", "North", "North", "North",…
#> $ state            <chr> "AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC", "AC",…
#> $ city             <chr> "Acrelândia", "Assis Brasil", "Brasiléia", "Bujari", …
#> $ region_code      <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
#> $ state_code       <dbl> 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 1…
#> $ mesoregion_code  <int> 1202, 1202, 1202, 1202, 1202, 1201, 1202, 1201, 1201,…
#> $ microregion_code <int> 12004, 12005, 12005, 12004, 12004, 12001, 12005, 1200…
#> $ city_code        <int> 120001, 120005, 120010, 120013, 120017, 120020, 12002…
#> $ Bolsonaro        <int> 5165, 2333, 8711, 4676, 3895, 31147, 6284, 6296, 1375…
#> $ Haddad           <int> 1300, 1660, 3208, 1274, 1086, 7818, 2026, 5256, 1415,…
#> $ pop              <dbl> 15256, 7417, 26278, 10266, 11733, 88376, 18411, 34780…

election2018Cities <- election2018Cities %>%
  add_geo() %>%
  mutate(
    prop = 100*Bolsonaro/(Bolsonaro + Haddad),
  ) %>%
  pivot_longer(cols = c("Bolsonaro", "Haddad"), values_to = "votes", names_to = "candidate")


ggplot(election2018Cities) +
  geom_sf(aes(fill = prop)) +
  labs(fill = "% votes")

The 2018 Brazilian presidential election results observed in Minas Gerais (MG) state, for instance, can be easily extracted from the data set election2018Cities as illustrated below:


# extracting the data:
mg_election <- election2018Cities %>%
  filter(state == "MG")

# visualizing:
ggplot(mg_election) +
  geom_sf(aes(fill = prop)) +
  labs(fill = "% votes")

The package covid19br also provides datasets containing Development Human Indexes (DHI) at city (ipeaCities), state (ipeaStates) and region (ipeaRegions) levels. The code presented below shows how to combine the COVID-19 data (at region level) with the election and the DHI datasets.


# loading the election data:
data(election2018Regions)

  # putting all together 
regions <- downloadCovid19("regions") %>%
  filter(date == max(date)) %>%
  add_epi_rates() %>%
  left_join(election2018Regions) %>%
  add_geo()
#> Downloading COVID-19 data from the official Brazilian repository: https://covid.saude.gov.br/
#> Please, be patient...
#> Done!
#> Joining, by = c("region", "pop")
#> Joining, by = c("region", "pop")

glimpse(regions)
#> Rows: 5
#> Columns: 23
#> $ region       <chr> "Midwest", "North", "Northeast", "South", "Southeast"
#> $ date         <date> 2022-03-29, 2022-03-29, 2022-03-29, 2022-03-29, 2022-03-2…
#> $ epi_week     <int> 13, 13, 13, 13, 13
#> $ newCases     <int> 2586, 1643, 4298, 7194, 14335
#> $ accumCases   <int> 3208764, 2467981, 6170909, 6351493, 11683250
#> $ newDeaths    <int> 23, 4, 55, 47, 156
#> $ accumDeaths  <int> 62880, 49812, 127896, 103583, 315070
#> $ newRecovered <int> NA, NA, NA, NA, NA
#> $ newFollowup  <int> NA, NA, NA, NA, NA
#> $ pop          <dbl> 16297074, 18430980, 57071654, 29975984, 88371433
#> $ incidence    <dbl> 19689.20, 13390.39, 10812.56, 21188.61, 13220.62
#> $ lethality    <dbl> 1.96, 2.02, 2.07, 1.63, 2.70
#> $ mortality    <dbl> 385.8361, 270.2624, 224.0972, 345.5533, 356.5292
#> $ Bolsonaro    <int> 5163023, 4242504, 8824454, 11084395, 28351800
#> $ Haddad       <int> 2595426, 3933015, 20289812, 5152685, 15016238
#> $ DHI          <dbl> 0.6894678, 0.6079510, 0.5906721, 0.7141128, 0.6989844
#> $ EDHI         <dbl> 0.5841416, 0.4904276, 0.4884060, 0.6130152, 0.6081894
#> $ LDHI         <dbl> 0.8224442, 0.7803808, 0.7543157, 0.8353316, 0.8281859
#> $ IDHI         <dbl> 0.6844893, 0.5928241, 0.5622705, 0.7134444, 0.6811571
#> $ region_code  <dbl> 5, 1, 2, 4, 3
#> $ area         [km^2] 1611899.2 [km^2], 3870624.8 [km^2], 1560644.5 [km^2], 56…
#> $ demoDens     [1/km^2] 10.110480 [1/km^2], 4.761758 [1/km^2], 36.569286 [1/km^2]…
#> $ geometry     <GEOMETRY [°]> POLYGON ((-54.08399 -23.864..., POLYGON ((-68.38821 -11.0…