rm(list=ls())

# install.packages("ggrepel")
# install.packages("gridExtra")
library(ggplot2)
library(ggrepel)
library(gridExtra)

setwd('D:\\areas_protegidas\\xls_dbf\\salidas txt\\')
paises <- c("Argentina","Bolivia","Brazil","Chile","Colombia",
            "Ecuador","Guayana Francesa","Guayanas","Guyana","Peru",
            "Paraguay","Suriname","Uruguay","Venezuela")

gini_eco_pais <- read.table("gini_eco_pais_historico.txt",sep=';',header=T)  

gini_eco_pais$FR <- 1-gini_eco_pais$giniFP
gini_eco_pais$QR <- 1-gini_eco_pais$giniAP

gini_eco_pais$yr  <- NULL
gini_eco_pais$yr[gini_eco_pais$decada==1]<-1960
gini_eco_pais$yr[gini_eco_pais$decada==2]<-1970
gini_eco_pais$yr[gini_eco_pais$decada==3]<-1980
gini_eco_pais$yr[gini_eco_pais$decada==4]<-1990
gini_eco_pais$yr[gini_eco_pais$decada==5]<-2000
gini_eco_pais$yr[gini_eco_pais$decada==6]<-2010
gini_eco_pais$yr[gini_eco_pais$decada==7]<-2016

# gini_eco_pais$yr <- ifelse(gini_eco_pais$decada==1, 1960, 
#                     ifelse(gini_eco_pais$decada==2, 1970, 
#                     ifelse(gini_eco_pais$decada==3, 1980, 
#                     ifelse(gini_eco_pais$decada==4, 1990, 
#                     ifelse(gini_eco_pais$decada==5, 2000, 
#                     ifelse(gini_eco_pais$decada==6, 2010, 
#                     ifelse(gini_eco_pais$decada==7, 2016, 
#                            NA)))))))

gini_eco_pais <- gini_eco_pais[,-c(2,3,4)] # saco la decada y los ginis
gini_eco_pais <- gini_eco_pais[order(gini_eco_pais$pais),]
gini_eco_pais <- gini_eco_pais[!(gini_eco_pais$pais=="Suriname" 
                                 | gini_eco_pais$pais=="Guayana_Fra"
                                 | gini_eco_pais$pais=="Guyana"),] # saco guayanas individuales


#################################################################

datos_basicos_historicos <- read.table("datos_basicos_historicos.txt",sep='',header=T)
datos_basicos_historicos <- datos_basicos_historicos[!(datos_basicos_historicos$pais=="Suriname" 
                                 | datos_basicos_historicos$pais=="Guayana Francesa"
                                 | datos_basicos_historicos$pais=="Guyana"),] # saco guayanas individuales

#################################################################

unionFP <- cbind(datos_basicos_historicos,gini_eco_pais)
unionFP <- unionFP[,-c(3,4)]

unionFP$FR[unionFP$pais=="Uruguay"] <- NA #porque tiene una sola ecoregion
unionFP$QR[unionFP$pais=="Uruguay"] <- NA

########################################################################
# FR

# FR_FPplot1 <- ggplot(data=unionFP) +
#   geom_smooth(aes(x=yr, y=FR),linetype=1, size=0.5, colour = "gray80", se=FALSE) +
#   geom_point(aes(x=yr, y=FR, size=FR_FP), colour = "red4") +
#   scale_size_area(limits=c(0,8), max_size=3) +
#   facet_wrap(~pais, ncol=4) + 
#   theme_grey(base_size = 8, base_family = "") +
#   theme(legend.position=c(0.9, 0.1), legend.key.size = unit(.4, "cm"),
#         legend.title = element_text(size=7)) +
#   labs(x = "Year", y=expression(FR~biogeographical~units~(G^-1)), size = "FR:PF")
# FR_FPplot1
# 
# ggsave("tendencia_FR_FPeco1.png", plot = FR_FPplot1, scale = 1, width = 12, height = 9, units = "cm",
#        dpi = 300, path = '..\\salidas graficas\\graficos sam')

FR_FPplot2 <- ggplot(data=unionFP) +
  geom_smooth(aes(x=yr, y=FR),linetype=1, size=0.5, colour = "gray80", se=FALSE) +
  geom_point(aes(x=yr, y=FR, size=FP), colour = "red4") +
  scale_size_area(limits=c(0,20), max_size=3) +
  facet_wrap(~pais, ncol=4) + 
  theme_grey(base_size = 8, base_family = "") +
  theme(legend.position=c(0.9, 0.1), legend.key.size = unit(.4, "cm"),
        legend.title = element_text(size=7)) +
  labs(x = "Year", y="Protection biogeographical equality (G')", size = "Protection extent (%)")
FR_FPplot2

ggsave("tendencia_FR_FPeco2_1-G.png", plot = FR_FPplot2, scale = 1, width = 12, height = 9, units = "cm",
       dpi = 300, path = '..\\salidas graficas\\graficos sam')

FR_FPplot3 <- ggplot(data=unionFP,aes(x=FP, y=FR)) +
  # geom_smooth(linetype=1, size=0.5, colour = "gray80", se=FALSE) +
  geom_point(size=1.5, colour = "red4") +
  geom_path(size = 0.5, colour = "red4") +
  facet_wrap(~pais, ncol=4) + 
  geom_text_repel(aes(label=yr), size = 1.5, nudge_x = 0.5,
                  segment.color = 'grey50', force = 1.1, 
                  point.padding = unit(0.4, 'lines'),
                  min.segment.length = unit(0.1, 'lines')) +
  theme(legend.position=c(0.9, 0.1)) +
  theme_grey(base_size = 8, base_family = "") +
  labs(x = "Protection extent (%)", y="Protection biogeographical equality (G')", size = 2.5)
FR_FPplot3

ggsave("tendencia_FR_FPeco3_1-G.png", plot = FR_FPplot3, scale = 1, width = 12, height = 9, units = "cm",
       dpi = 300, path = '..\\salidas graficas\\graficos sam')



########################################################################
# QR

# QR_FPplot1 <- ggplot(data=unionFP) +
#   geom_smooth(aes(x=yr, y=QR),linetype=1, size=0.5, colour = "gray80", se=FALSE) +
#   geom_point(aes(x=yr, y=QR, size=QR_FP), colour = "red4") +
#   scale_size_area(limits=c(0,8), max_size=3) +
#   facet_wrap(~pais, ncol=4) + 
#   theme_grey(base_size = 8, base_family = "") +
#   theme(legend.position=c(0.9, 0.1), legend.key.size = unit(.4, "cm"),
#         legend.title = element_text(size=7)) +
#   labs(x = "Year", y=expression(QR~biogeographical~units~(G^-1)), size = "QR:PF")
# QR_FPplot1
# 
# ggsave("tendencia_QR_FPeco1.png", plot = QR_FPplot1, scale = 1, width = 12, height = 9, units = "cm",
#        dpi = 300, path = '..\\salidas graficas\\graficos sam')

QR_FPplot2 <- ggplot(data=unionFP) +
  geom_smooth(aes(x=yr, y=QR),linetype=1, size=0.5, colour = "gray80", se=FALSE) +
  geom_point(aes(x=yr, y=QR, size=FP), colour = "gray25") +
  scale_size_area(limits=c(0,20), max_size=3) +
  facet_wrap(~pais, ncol=4) + 
  theme_grey(base_size = 8, base_family = "") +
  theme(legend.position=c(0.9, 0.1), legend.key.size = unit(.4, "cm"),
        legend.title = element_text(size=7)) +
  labs(x = "Year", y=expression(QR~biogeographical~units~(1-G)), size = "Protection extent (%)")
QR_FPplot2

ggsave("tendencia_QR_FPeco2_1-G.png", plot = QR_FPplot2, scale = 1, width = 12, height = 9, units = "cm",
       dpi = 300, path = '..\\salidas graficas\\graficos sam')

QR_FPplot3 <- ggplot(data=unionFP,aes(x=FP, y=QR)) +
  # geom_smooth(linetype=1, size=0.5, colour = "gray80", se=FALSE) +
  geom_path(size = 0.5, colour = "gray80") +
  geom_point(size=1.5, colour = "gray25") +
  facet_wrap(~pais, ncol=4) + 
  geom_text_repel(aes(label=yr), size = 1.5, nudge_x = 0.5,
                  segment.color = 'grey50', force = 1.1, 
                  point.padding = unit(0.4, 'lines'),
                  min.segment.length = unit(0.1, 'lines')) +
  theme(legend.position=c(0.9, 0.1)) +
  theme_grey(base_size = 8, base_family = "") +
  labs(x = "Protection extent (%)", y=expression(QR~biogeographical~units~(1-G)), size = 2.5)
QR_FPplot3

ggsave("tendencia_QR_FPeco3_1-G.png", plot = QR_FPplot3, scale = 1, width = 12, height = 9, units = "cm",
       dpi = 300, path = '..\\salidas graficas\\graficos sam')
