# IBUKI LAB · Experiencia 02
# El mismo dato. Tres historias.
# Datos 100 % FICTICIOS: visitas a un centro comunitario imaginario.
# Para ejecutar: install.packages(c("ggplot2", "dplyr", "tidyr"))

library(ggplot2)
library(dplyr)
library(tidyr)

# Escribe las visitas por día, en orden: 09:00, 12:00, 15:00 y 18:00.
matriz <- matrix(c(
  18, 32, 46, 25,   # Lunes
  21, 37, 56, 31,   # Martes
  24, 41, 61, 29,   # Miércoles
  20, 39, 65, 34,   # Jueves
  29, 52, 78, 61,   # Viernes
  48, 74, 96, 80,   # Sábado
  40, 65, 88, 67    # Domingo
), nrow = 7, byrow = TRUE)

dias <- c("Lun", "Mar", "Mié", "Jue", "Vie", "Sáb", "Dom")
horas <- c("09:00", "12:00", "15:00", "18:00")

datos <- as.data.frame(matriz)
colnames(datos) <- horas
datos$dia <- dias

datos_largos <- datos |>
  pivot_longer(cols = all_of(horas), names_to = "hora", values_to = "visitas") |>
  mutate(dia = factor(dia, levels = dias),
         hora = factor(hora, levels = rev(horas)))

base_tema <- theme_minimal(base_size = 13) +
  theme(panel.grid.minor = element_blank(),
        plot.title = element_text(face = "bold", colour = "#203934"),
        plot.subtitle = element_text(colour = "#62766f"))

# 01 · HEATMAP. Pregunta: ¿qué combinaciones de día y horario destacan?
g_1 <- ggplot(datos_largos, aes(x = dia, y = hora, fill = visitas)) +
  geom_tile(colour = "white", linewidth = 2, width = .96, height = .96) +
  geom_text(aes(label = visitas), size = 3.5, colour = "#173d37") +
  scale_fill_gradient(low = "#e6f0ea", high = "#286f74") +
  labs(title = "¿Dónde se concentra la actividad?", subtitle = "Visitas ficticias por día y horario", x = NULL, y = NULL, fill = "Visitas") +
  coord_equal() + base_tema + theme(panel.grid = element_blank())
print(g_1)

# 02 · LÍNEAS. Pregunta: ¿cómo varía cada franja horaria durante la semana?
g_2 <- ggplot(datos_largos, aes(x = dia, y = visitas, group = hora, color = hora)) +
  geom_line(linewidth = 1.3) + geom_point(size = 2.7) +
  scale_color_manual(values = c("#c18d62", "#225761", "#3d8480", "#a1baaa")) +
  labs(title = "¿Cómo cambia a lo largo de la semana?", subtitle = "Una trayectoria por horario", x = NULL, y = "Visitas", color = "Horario") +
  base_tema
print(g_2)

# 03 · RANKING LOLLIPOP. Pregunta: ¿qué días suman más visitas?
ranking <- datos_largos |>
  group_by(dia) |>
  summarise(total = sum(visitas), .groups = "drop") |>
  mutate(dia = reorder(as.character(dia), total))

g_3 <- ggplot(ranking, aes(x = total, y = dia)) +
  geom_segment(aes(x = 0, xend = total, yend = dia), colour = "#a9cdc0", linewidth = 1.5) +
  geom_point(size = 4.5, colour = "#286f74") +
  geom_text(aes(label = total), hjust = -0.6, colour = "#315c53") +
  scale_x_continuous(expand = expansion(mult = c(0, .13))) +
  labs(title = "¿Qué días concentran más visitas?", subtitle = "Total por día, datos ficticios", x = "Visitas totales", y = NULL) +
  base_tema + theme(panel.grid.major.y = element_blank())
print(g_3)

# Para guardar: ggsave("ibuki_mapa_de_calor.png", g_1, width = 8, height = 5, dpi = 300)
