Day 15

R
Data Viz
ggplot2
Data analysis
BCRP
Day 15 from #30dataChartChallenge
Fecha de publicación

15 de abril de 2022

knitr::opts_chunk$set(
  cache = T
)
librarian::shelf(
  tidyverse
  , PeruData
  , lubridate
  # , sysfonts
  # , showtext
)

  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'.
sysfonts::font_add_google("Ubuntu", "regular")
# showtext_auto()


mlt <- c(
  "https://estadisticas.bcrp.gob.pe/estadisticas/series/diarias/resultados/PD04704XD/html"
  , "https://estadisticas.bcrp.gob.pe/estadisticas/series/diarias/resultados/PD04703XD/html"
  , "https://estadisticas.bcrp.gob.pe/estadisticas/series/diarias/resultados/PD04702XD/html"
  , "https://estadisticas.bcrp.gob.pe/estadisticas/series/diarias/resultados/PD04701XD/html"
)

Sys.setlocale("LC_ALL", "Spanish")
Warning in Sys.setlocale("LC_ALL", "Spanish"): using locale code page other
than 65001 ("UTF-8") may cause problems
[1] "LC_COLLATE=Spanish_Spain.1252;LC_CTYPE=Spanish_Spain.1252;LC_MONETARY=Spanish_Spain.1252;LC_NUMERIC=C;LC_TIME=Spanish_Spain.1252"
pi <- 
  bcrp_get(mlt)
Getting data ...
pi |> head()
[[1]]
# A tibble: 6,941 x 2
   fecha   cotizaciones_internacionales_oro_londres_us_por_onzas_troy
   <chr>   <chr>                                                     
 1 02Ene97 366.10                                                    
 2 03Ene97 362.40                                                    
 3 06Ene97 357.70                                                    
 4 07Ene97 359.10                                                    
 5 08Ene97 356.00                                                    
 6 09Ene97 356.60                                                    
 7 10Ene97 359.25                                                    
 8 13Ene97 358.90                                                    
 9 14Ene97 356.90                                                    
10 15Ene97 354.20                                                    
# i 6,931 more rows

[[2]]
# A tibble: 6,941 x 2
   fecha   cotizaciones_internacionales_zinc_londres_c_us_por_libras
   <chr>   <chr>                                                    
 1 02Ene97 46.97                                                    
 2 03Ene97 47.29                                                    
 3 06Ene97 47.20                                                    
 4 07Ene97 47.31                                                    
 5 08Ene97 47.51                                                    
 6 09Ene97 47.26                                                    
 7 10Ene97 47.40                                                    
 8 13Ene97 47.36                                                    
 9 14Ene97 48.72                                                    
10 15Ene97 49.49                                                    
# i 6,931 more rows

[[3]]
# A tibble: 6,941 x 2
   fecha   cotizaciones_internacionales_plata_h_harman_us_por_onzas_troy
   <chr>   <chr>                                                        
 1 02Ene97 4.73                                                         
 2 03Ene97 4.71                                                         
 3 06Ene97 4.65                                                         
 4 07Ene97 4.67                                                         
 5 08Ene97 4.65                                                         
 6 09Ene97 4.70                                                         
 7 10Ene97 4.73                                                         
 8 13Ene97 4.69                                                         
 9 14Ene97 4.71                                                         
10 15Ene97 4.67                                                         
# i 6,931 more rows

[[4]]
# A tibble: 6,941 x 2
   fecha   cotizaciones_internacionales_cobre_londres_c_us_por_libras
   <chr>   <chr>                                                     
 1 02Ene97 102.29                                                    
 2 03Ene97 104.46                                                    
 3 06Ene97 104.55                                                    
 4 07Ene97 105.05                                                    
 5 08Ene97 108.05                                                    
 6 09Ene97 107.41                                                    
 7 10Ene97 109.32                                                    
 8 13Ene97 111.18                                                    
 9 14Ene97 111.95                                                    
10 15Ene97 109.09                                                    
# i 6,931 more rows
last_p <- 
  pi |> 
  reduce(left_join) |> 
  slice(n():(n() - 300)) |> 
  mutate(
    fecha1 = str_to_lower(fecha)
  )
Joining with `by = join_by(fecha)`
Joining with `by = join_by(fecha)`
Joining with `by = join_by(fecha)`
nn <- 
  last_p |> 
  mutate(
    fecha = str_replace_all(fecha, "Set", "Sep")
    , fecha1 = str_to_lower(fecha)
    , fecha2 = paste(str_sub(fecha1, 1, 5), ".", str_sub(fecha1, 6, -1), sep = "")
    , fecha = dmy(fecha2)
  ) |> 
  # filter(is.na(fecha))
  dplyr::select(1:5) |> 
  rename(
    oro = 2
    , zinc = 3
    , plata = 4
    , cobre = 5
  ) |> 
  pivot_longer(!fecha) |> 
  mutate(
    value = as.numeric(value)
  )

mi_cl <- 
  c(
    "#ffd700"
    , "#bac4c8"
    , "#b87333"
    , "#5b7582"
  )
nn |> head()
# A tibble: 6 x 3
  fecha      name   value
  <date>     <chr>  <dbl>
1 2023-08-10 oro   1916. 
2 2023-08-10 zinc   112. 
3 2023-08-10 plata   22.7
4 2023-08-10 cobre  382. 
5 2023-08-09 oro   1914. 
6 2023-08-09 zinc   113. 
nn |> 
  mutate(
    mi = factor(str_to_sentence(name), c("Oro", "Plata", "Cobre", "Zinc"))
  ) |> 
  # filter(name == 'oro') |> 
  ggplot() +
  aes(fecha, value, group = mi, color = mi) +
  # ggalt::geom_xspline() +
  geom_line(size = 1, show.legend = F) +
  facet_wrap(~mi, scales = "free_y") +
  scale_x_date(date_breaks = "18 weeks") +
  labs(
    x = "", y = ""
    , title = "Commodities"
    , subtitle = "Cottizaciones internacionales ($)"
    , caption = "#30DayChartChallenge | Data: BCRP \nDay15: Multivariable | Viz: @JhonKevinFlore1"
    ) +
  scale_color_manual(
    values = mi_cl
  ) +
  scale_y_continuous(labels = scales::dollar) +
  theme_minimal()+
  theme(
    panel.background = element_rect("white", color = NA)
    , plot.background = element_rect("white")
    , panel.grid.minor = element_blank()
    , plot.margin = margin(.3, 1, .1, .3, "cm")
    , axis.line = element_line()
    , axis.text.x = element_text(hjust = .5, angle = 12)
    , plot.caption = element_text(hjust = .5)
  )
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
i Please use `linewidth` instead.

ggsave(
  'plots/day15_dcc_22.png'
  , height = 8
  , width = 12
  , dpi = 320
)
knitr::include_graphics('plots/day15_dcc_22.png')