Day 01

R
Data Viz
sf
ggplot2
Day 1 form #30datChartChallenge
Fecha de publicación

1 de abril de 2022

librarian::shelf(
  PeruData,
  tidyverse,
  janitor, 
  ggstream,
  cowplot
)

  The 'cran_repo' argument in shelf() was not set, so it will use
  cran_repo = 'https://cran.r-project.org' by default.

  To avoid this message, set the 'cran_repo' argument to a CRAN
  mirror URL (see https://cran.r-project.org/mirrors.html) or set
  'quiet = TRUE'.
Warning: package 'janitor' was built under R version 4.3.1
Warning: package 'ggstream' was built under R version 4.3.1
peru_covid <- PeruData::covid_casos_fallecidos
peru_covid |> head()
# A tibble: 6 × 18
  fecha      ubigeo departamento provincia   distrito    sexo    pcr    pr    ag
  <date>     <chr>  <chr>        <chr>       <chr>       <chr> <dbl> <dbl> <dbl>
1 2022-02-21 010101 amazonas     chachapoyas chachapoyas feme…     2     0     8
2 2022-02-21 010101 amazonas     chachapoyas chachapoyas masc…     1     0     4
3 2022-02-21 010110 amazonas     chachapoyas leimebamba  feme…     0     0     3
4 2022-02-21 010111 amazonas     chachapoyas levanto     masc…     0     0     1
5 2022-02-21 010201 amazonas     bagua       bagua       feme…     1     0     1
6 2022-02-21 010202 amazonas     bagua       aramango    feme…     0     0     1
# ℹ 9 more variables: total_casos <dbl>, criterio_cla_nico <dbl>,
#   criterio_sinadef <dbl>, criterio_nexo_epidemiola_gico <dbl>,
#   criterio_investigacia_n_epidemiola_gica <dbl>, criterio_virola_gico <dbl>,
#   criterio_radiola_gico <dbl>, criterio_serola_gico <dbl>,
#   total_fallecidos <dbl>
peru_prueba <- 
  peru_covid |> 
  select(fecha, sexo, pcr:total_casos) |> 
  filter(lubridate::year(fecha) > 2020) |> 
  mutate(anio = lubridate::year(fecha), mes = lubridate::month(fecha)) |> 
  select(!fecha) |>
  pivot_longer(!c(anio, mes, sexo, total_casos), names_to = "prb") |> 
  group_by(anio, mes, sexo, prb) |> 
  mutate(across(c(total_casos, value), sum)) |> 
  distinct() |> 
  mutate(
    fecha = paste(anio, mes, "01", sep = "-")
    , fecha = lubridate::ymd(fecha)
    , prb = case_when(
      prb == "pr" ~ "Prueba rápida"
      , prb == "ag" ~ 'Prueba de antigeno'
      , T ~ "PCR"
    )
    ) |> 
  drop_na() |> 
  ungroup()
peru_prueba |> head()
# A tibble: 6 × 7
  sexo      total_casos  anio   mes prb                value fecha     
  <chr>           <dbl> <dbl> <dbl> <chr>              <dbl> <date>    
1 femenino      2044429  2022     2 PCR                40894 2022-02-01
2 femenino      2044429  2022     2 Prueba rápida          0 2022-02-01
3 femenino      2044429  2022     2 Prueba de antigeno 61312 2022-02-01
4 masculino     1842830  2022     2 PCR                40299 2022-02-01
5 masculino     1842830  2022     2 Prueba rápida          0 2022-02-01
6 masculino     1842830  2022     2 Prueba de antigeno 48871 2022-02-01
pal <- c(
  "#595A52",  
  "#C20008", 
  "#13AFEF"
  # "#FFB400", 
  # "#8E038E", 
)
p <- 
  peru_prueba |>
  ggplot() +
  aes(fecha, value, fill = prb) +
  geom_stream() +
  # geom_stream_label(aes(label = prb)) +
  # geom_line(aes(fecha, value, color = prb, group = prb, fill = prb)) +
  facet_wrap(~sexo, ncol = 1) +
  scale_fill_manual(values = pal) +
  labs(fill = "", title = "COVID Perú (2020 - 2022)", subtitle = "Casos positivos según prueba realizada") +
  guides(
    fill = guide_legend(
      direction = "horizontal"
      , title.position = "top"
      , title.hjust = .5
      , label.hjust = .5
      , label.position = "top"
      , keywidth = 2.3
      , keyheight = 1
    )
  ) +
  theme_minimal() +
  theme(
    axis.text.y = element_blank()
    , axis.title = element_blank()
    , legend.position = c(.5, .6)
    , legend.background = element_rect(fill = "white", color = NA)
    , legend.key.height = unit(.25, "mm")
    , panel.background = element_rect(fill = "white", color = NA)
    , plot.background = element_rect(fill = "white", color = NA)
    , plot.title = element_text(
      size = 27
      , face = "bold"
      , hjust = .5
      , margin = 
    )
    , plot.subtitle = element_text(
      size = 20
      , hjust = .5
    )
    , legend.text = element_text(size = 15)
    , axis.text.x = element_text(size = 15)
    , panel.grid.major.y = element_blank()
    , panel.grid.minor.y = element_blank()
    , panel.grid.major.x = element_line(size = 1, linetype = "dashed", color = "darkblue")
    , panel.grid.minor.x = element_blank()
    , strip.background = element_blank()
    , strip.text = element_blank()
  ) +
  scale_x_date(date_labels = "%b - %Y", date_breaks = "3 months") 
Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.
p

p1 <- 
  ggdraw(p) +
  cowplot::draw_label("Femenino", x = .74, y = .75, size = 22, color = "#d17dd8", fontface = "bold") +
  cowplot::draw_label("Masculino", x = .74, y = .30, size = 22, color = "#0b4dbf",fontface = "bold") +
  draw_line(x = c(.34, .72), y = c(.90, .90), size = 28, color = "white") +
  draw_line(x = c(.34, .72), y = c(.485, .485), size = 38, color = "white") +
  draw_label("#30DayChartChallenge\nDay1: Part-to-whole\nViz: @JhonKevinFlore1\nData: MINSA Perú", x = .02, y = .08, hjust = 0)
p1

p_loc <- paste('plots', 'day1_dcc_22.png', sep = "/")
ggsave(
  plot = p1, 
  p_loc,
  width = 16, height = 12
)
knitr::include_graphics(p_loc)