Day 03

R
Data Viz
IGP
ggplot2
Data analysis
Day 3 form #30datChartChallenge
Fecha de publicación

3 de abril de 2022

p_name = 'plots/day3_dcc_22.png'
knitr::opts_chunk$set(
  warning = F
)
librarian::shelf(
  tidyverse
  , PeruData
  , gganimate
  , lubridate
)

  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'.

  These packages will be installed:

  'gganimate'

  It may take some time.
also installing the dependency 'tweenr'
package 'tweenr' successfully unpacked and MD5 sums checked
package 'gganimate' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\Jhon\AppData\Local\Temp\RtmpE333kV\downloaded_packages
Warning: package 'gganimate' was built under R version 4.3.1
eq <- 
  PeruData::igp |> 
  distinct() |> 
  select(!c(lat, lon, hour, intensi_km)) |> 
  mutate(
    anio = year(date)
    , mes = month(date)
  ) |> 
  group_by(anio, mes, alert) |> 
  summarise(promedio = mean(magn)) |> 
  ungroup() |> 
  mutate(date = ymd(paste(anio, mes, "01", sep = "/"))) |> 
  select(!1:2) |> 
  drop_na() |> 
  mutate(
    alert = factor(alert, c("Red", "Yellow", "Green"))
    , alerta_name = case_when(
      alert == "Red" ~ 'Alerta "Roja"'
      , alert == "Yellow" ~ 'Alerta "Ararilla"'
      , T ~ 'Alerta "Verde"'
    ) |> 
      factor(c('Alerta "Roja"', 'Alerta "Ararilla"', 'Alerta "Verde"'))
  ) |> 
  filter(year(date) < 2023) |> 
  with_groups(
    alert, mutate, mid = mean(promedio, na.rm = T)
  )
`summarise()` has grouped output by 'anio', 'mes'. You can override using the
`.groups` argument.
eq |> head()
# A tibble: 6 × 5
  alert  promedio date       alerta_name             mid
  <fct>     <dbl> <date>     <fct>                 <dbl>
1 Red        6.97 1960-01-01 "Alerta \"Roja\""      6.47
2 Yellow     5.75 1960-01-01 "Alerta \"Ararilla\""  4.91
3 Yellow     5.6  1960-02-01 "Alerta \"Ararilla\""  4.91
4 Red        6.2  1960-03-01 "Alerta \"Roja\""      6.47
5 Red        6.1  1960-04-01 "Alerta \"Roja\""      6.47
6 Yellow     5.4  1960-05-01 "Alerta \"Ararilla\""  4.91
pal <- c(
  "#C20008" 
  , "#FFB400"
  # , "#595A52"  
  # , "#13AFEF"
  , "#006320"
)
eq |> 
  ggplot() +
  geom_point(size = 1) +
  aes(date, promedio, color = alert) +
  geom_step(direction = "vh") +
  geom_hline(aes(yintercept = mid, color = alert), linetype = "dashed") +
  facet_wrap(~alerta_name, scales = "free_y", ncol = 1) +
  labs(
    x = "", y = ""
    , title = "Terremotos/Sismos en el Perú"
    , subtitle = "Promedio de la magnitud de los terremotos segun su clasificación"
    , caption = "Data: IGP - Peru | Viz: @JhonKevinFlore1 | #30DayChartChallenge | Day3: Historical"
    
    ) +
  scale_color_manual(
    values = pal
  ) + 
  scale_x_date(
    date_labels = "%m - %Y", date_breaks = "2 years"
    , limits = as.Date(c('1958-05-01','2023-01-01'))
    )  +
  
  theme(
    axis.text.x = element_text(angle = 60, vjust = .1, hjust = .5, size = 40)
    , legend.position = "none"
    , panel.grid.minor = element_blank()
    , panel.grid.major.x = element_line(size = .5, color = "grey70", linetype = "dotted")
    , text = element_text(
      # family = "Ubuntu",
      face = 'bold',
      colour = "#004D40",
      size = 40
    ),
    plot.background = element_rect(fill = "white", color = NA),
    panel.background = element_rect(fill = "white", color = NA), 
    
    plot.title = element_text(
      hjust = 0.5,
      size = 80,
      margin = margin(10, 0, 0, 0),
      color = "#c00000"
    ),
    plot.subtitle = element_text(
      hjust = 0.5,
      size = 70,
      margin = margin(10, 0, 0, 0),
      color = "grey40"
    ),
    plot.caption = element_text(
      hjust = .5,
      margin = margin(20, 0, 2, 0),
      color = "grey50"
      , lineheight = .7
      , size = 20
    ),
    plot.margin = margin(0, 1, 1, 0, unit = "cm")
    , strip.background = element_blank()
    , strip.background.x = element_blank()
    , axis.line.x.bottom = element_line(linetype = "solid", arrow = grid::arrow(length = unit(0.3, "cm"), 
                                                                                ends = "last", type = "closed"))
    , axis.line.y.left = element_line(linetype = "solid")
  )

ggsave(
  p_name
  , width = 15 
  , height = 8
)
knitr::include_graphics(p_name)