Codigo que genera el gráfico del documento:
library(ggplot2)
library(reshape2)
datos<-read.csv("./calidad-del-aire-datos-historicos-diarios.csv",header=TRUE,sep=";")
datos2<-datos[,2:7]
colnames(datos2)<-c("CO","NO","NO2","O3","PM10","PM25")
correlaciones<-cor(datos2,use = "pairwise.complete.obs")
datos <- reshape2::melt(correlaciones)
orden <- corrplot::corrMatOrder(correlaciones, order = "AOE")
nombres <- colnames(correlaciones)[orden]
datos$Var1 <- factor(datos$Var1, levels = nombres)
datos$Var2 <- factor(datos$Var2, levels = nombres)
datos <- datos[as.numeric(datos$Var1) >= as.numeric(datos$Var2),]
ggplot(datos, aes(x = Var2, y = Var1, fill = value)) +
geom_tile(color = "white", linewidth = 0.4) +
geom_text(aes(label = sprintf("%.2f", value)), size = 4, fontface = "bold") +
scale_fill_gradient2(low = "#D0486B",mid = "white",high = "#14FB98",midpoint = 0,limits = c(-1, 1),
breaks = seq(-1, 1, 0.2),name = NULL,guide = guide_colorbar(barwidth = 20,barheight = 0.6,title.position = "top",
ticks = TRUE)) +
scale_x_discrete(position = "top") + coord_fixed() +
labs( title = "Correlograma entre agentes contaminantes en el aire de CyL",x = NULL,y = NULL) +
theme_minimal(base_size = 12) +
theme(plot.title = element_text(hjust = 0.5,face = "bold",size = 16,margin = margin(b = 25)),
axis.text.x = element_text(angle = 0,color = "black",vjust = 0.5,margin = margin(b = 8)),
axis.text.y = element_text(color = "black"),panel.grid = element_blank(),axis.ticks = element_blank(),
legend.position = "bottom",plot.margin = margin(20, 20, 20, 20))+labs(caption = "Fuente: Repositorio de datos Junta CyL, 2026") +
theme(plot.caption = element_text(hjust = 0, size = 8, color = "gray40"))
