paquetes tydiverse, lubridate, ggplot2

Recuperación observatorio socioambiental Ibero

Fuente: https://albert-rapp.de/posts/ggplot2-tips/35_calendar_plots/35_calendar_plots.html

Conversión de fecha tipo caracter a fecha con el paquete lubridate

ruta<-"C:/Users/cguer/Documents/Claudia/Midropbox/Investigacion y escritos/karamanis/data/"
#infile<-"notas2.csv"
infile<-"metrocdmx.csv"

notas<-read.csv(paste0(ruta,infile),encoding="latin1")
#colnames(notas)[4] <- 'Fechapub'
colnames(notas)[1] <- 'Fechapub'

#notas$Fechapubdate <- as.POSIXct(notas$Fechapub, format = "%d/%m/%y")
notas$Fechapubdate <- as.POSIXct(notas$Fechapub)

Conteo de notas por cada día y mes Recuperación del año mes y día de cada una de ellas

notas$ye<-year(notas$Fechapubdate)
notas$me<-month(notas$Fechapubdate)
notas$de<-day(notas$Fechapubdate)

#date_counts <- notas |> 
#  mutate(
#    date = make_date(
#      year = ye, month = me, day = de
#    )
#  ) |> 
#  count(date) 
###date_counts

date_counts <- notas%>%
  group_by(Fechapubdate) %>%
  summarise(n= sum(afluencia))

Filtro del conteo entre fechas

#date_counts<-date_counts %>% filter(between(date_counts$date, as.Date('2021-12-31'), as.Date('2023-01-01')))
date_counts<-date_counts %>% filter(between(date_counts$Fechapubdate, as.Date('2021-12-31'), as.Date('2023-01-01')))

Recuperación de etiquetas de acuerdo al sistema local

argumento de la función: locale = Sys.getlocale(“LC_TIME”) O locale = “es_MX.UTF-8”

Tabla en : https://docs.oracle.com/cd/E23824_01/html/E26033/glset.html

Gráfica de acuerdo a fuente

Fuente: https://albert-rapp.de/posts/ggplot2-tips/35_calendar_plots/35_calendar_plots.html

###date_counts_w_labels
labels_color <- 'grey30'
schedueled_color <- '#009E73'
bar_width_cm <- 15
bar_height_cm <- 0.3
font_family <- 'Fira Sans'
bar_labels_size <- 11
month_size <- 12
nudge_labels <- 0.25
labels_size <- 3


date_counts_w_labels |> 
  ggplot(aes(wday, 5 - week)) +
  geom_tile(
    aes(fill = n), 
    col = labels_color
  ) +
  geom_text(
    aes(label = day),
    nudge_x = nudge_labels,
    nudge_y = nudge_labels,
    col = labels_color,
    size = labels_size,
    family = font_family
  ) +
  facet_wrap(vars(month), ncol = 3) +
  coord_equal(expand = FALSE) +
  scale_fill_gradient(
    high = schedueled_color, 
    low = colorspace::lighten(schedueled_color, 0.9),
  ) +
  theme_void() +
  #labs(
  #  title = 'Notas de conflictos sociombientales',
  #  subtitle = 'En 259 dìas del año 2022',
  #  fill = 'Recuperados en prensa, La Jornada, Reforma y Proceso',
  #  caption = 'Fuente: Observatorio conflictos socioambientales Universidad Iberoamericana Cd. Mexico'
  #) +
  labs(
    title = 'Afluencia diaria en las líneas del metro Cd.México',
    subtitle = 'En los dìas del año 2022',
    fill = 'Recuperados de Portal de datos abiertos. Afluencia diaria del Metro CDMX',
    caption = 'Fuente: https://visdatos.cdmx.gob.mx/app_direct_i/cdmx/_/?ckanConf=0e8ffe58-28bb-4dde-afcd-e5f5b4de4ccb'
  ) +
  
  theme(
    legend.position = 'top',
    text = element_text(
      color = labels_color, 
      family = font_family
    ),
    plot.title = element_text(
      size = 24, 
      margin = margin(t = 0.25, b = 0.25, unit = 'cm')
    ),
    plot.subtitle = element_text(
      size = 16, 
      margin = margin(b = 0.5, unit = 'cm')
    ),
    plot.caption = element_text(
      size = 10, 
      margin = margin(b = 0.25, unit = 'cm')
    ),
    legend.text = element_text(size = bar_labels_size),
    legend.title = element_text(size = 14),
    strip.text = element_text(
      hjust = 0, 
      size = month_size,
      margin = margin(b = 0.25, unit = 'cm')
    ),
    axis.text.x = element_text(
      margin = margin(t = -0.6, b = 0.3, unit = 'cm')
    )
  ) +
  guides(
    fill = guide_colorbar(
      barwidth = unit(bar_width_cm, 'cm'),
      barheight = unit(bar_height_cm, 'cm'),
      title.position = 'top',
      title.hjust = 0,
      title.vjust = 0,
      frame.colour = labels_color
    )
  )