rm(list=ls())

library(ineq)
library(ggcorrplot)
# library(ggarrange)
library(ggpubr)

setwd('D:\\areas_protegidas\\xls_dbf\\salidas txt')
dat_AL <-read.table('d_aichi_AL.txt',header=T)
dat_AR <-read.table('d_aichi_AR.txt',header=T)
dat_BO <-read.table('d_aichi_BO.txt',header=T)
dat_BR <-read.table('d_aichi_BR.txt',header=T)
dat_CL <-read.table('d_aichi_CL.txt',header=T)
dat_CO <-read.table('d_aichi_CO.txt',header=T)
dat_EQ <-read.table('d_aichi_EQ.txt',header=T)
dat_GI <-read.table('d_aichi_GI.txt',header=T)
dat_PY <-read.table('d_aichi_PY.txt',header=T)
dat_PE <-read.table('d_aichi_PE.txt',header=T)
dat_UY <-read.table('d_aichi_UY.txt',header=T)
dat_VZ <-read.table('d_aichi_VZ.txt',header=T)

dat_AL <-data.frame(dat_AL[,-c(6,7)])
dat_AR <-data.frame(dat_AR[,-c(6,7)])
dat_BO <-data.frame(dat_BO[,-c(6,7)])
dat_BR <-data.frame(dat_BR[,-c(6,7)])
dat_CL <-data.frame(dat_CL[,-c(6,7)])
dat_CO <-data.frame(dat_CO[,-c(6,7)])
dat_EQ <-data.frame(dat_EQ[,-c(6,7)])
dat_GI <-data.frame(dat_GI[,-c(6,7)])
dat_PY <-data.frame(dat_PY[,-c(6,7)])
dat_PE <-data.frame(dat_PE[,-c(6,7)])
dat_UY <-data.frame(dat_UY[,-c(6,7)])
dat_VZ <-data.frame(dat_VZ[,-c(6,7)])

colnames(dat_AL) <- 
colnames(dat_AR) <- 
colnames(dat_BO) <- 
colnames(dat_BR) <- 
colnames(dat_CL) <- 
colnames(dat_CO) <- 
colnames(dat_EQ) <- 
colnames(dat_GI) <- 
colnames(dat_PY) <- 
colnames(dat_PE) <- 
colnames(dat_UY) <- 
colnames(dat_VZ) <- c("PPT","TMP","ELE","SLO","SOI")

correlacion_AL <- round(cor(dat_AL, method = 'kendall'), 2)
correlacion_AR <- round(cor(dat_AR, method = 'kendall'), 2)
correlacion_BO <- round(cor(dat_BO, method = 'kendall'), 2)
correlacion_BR <- round(cor(dat_BR, method = 'kendall'), 2)
correlacion_CL <- round(cor(dat_CL, method = 'kendall'), 2)
correlacion_CO <- round(cor(dat_CO, method = 'kendall'), 2)
correlacion_EQ <- round(cor(dat_EQ, method = 'kendall'), 2)
correlacion_GI <- round(cor(dat_GI, method = 'kendall'), 2)
correlacion_PY <- round(cor(dat_PY, method = 'kendall'), 2)
correlacion_PE <- round(cor(dat_PE, method = 'kendall'), 2)
correlacion_UY <- round(cor(dat_UY, method = 'kendall'), 2)
correlacion_VZ <- round(cor(dat_VZ, method = 'kendall'), 2)



plot_AL <- ggcorrplot(correlacion_AL, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6, 
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_AL)

plot_AR <- ggcorrplot(correlacion_AR, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_AR)

plot_BO <- ggcorrplot(correlacion_BO, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_BO)

plot_BR <- ggcorrplot(correlacion_BR, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_BR)

plot_CL <- ggcorrplot(correlacion_CL, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_CL)

plot_CO <- ggcorrplot(correlacion_CO, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_CO)

plot_EQ <- ggcorrplot(correlacion_EQ, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_EQ)

plot_GI <- ggcorrplot(correlacion_GI, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_GI)

plot_PY <- ggcorrplot(correlacion_PY, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_PY)

plot_PE <- ggcorrplot(correlacion_PE, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_PE)

plot_UY <- ggcorrplot(correlacion_UY, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_UY)

plot_VZ <- ggcorrplot(correlacion_VZ, hc.order = TRUE,  type = "lower",
                      outline.col = "white", tl.cex=6,
                      ggtheme = ggplot2::theme_gray,
                      colors = c("#6D9EC1", "white", "#E46726"),
                      lab = TRUE, lab_size = 2, lab_col = 'gray30', 
                      show.legend = F)
# print(plot_VZ)


resumen_correlaciones <- ggarrange(plot_AL,
                                   plot_AR,
                                   plot_BO,
                                   plot_BR,
                                   plot_CL,
                                   plot_CO,
                                   plot_EQ,
                                   plot_GI,
                                   plot_PY,
                                   plot_PE,
                                   plot_UY,
                                   plot_VZ,ncol = 4, nrow = 3, align = "h")
resumen_correlaciones
ggsave("resumen_correlaciones.png", plot = resumen_correlaciones, scale = 1, width =16, height = 12.8, units = "cm",
       dpi = 400, path = 'D:\\areas_protegidas\\xls_dbf\\salidas graficas\\graficos sam')
