rm(list=ls())
library(ggplot2)
library(ggrepel)
library(gridExtra)

setwd('D:\\areas_protegidas\\xls_dbf\\salidas txt')

periodos <- c(1960,1970,1980,1990,2000,2010,2016)
periodos2 <- c(1970,1980,1990,2000,2010,2016)
paises <- c("All countries","Argentina","Bolivia","Brazil","Chile","Colombia",
            "Ecuador","GF","Guayanas","GU","Peru","Paraguay","SU","Uruguay","Venezuela")

paises2 <- c("All countries","Argentina","Bolivia","Brazil","Chile","Colombia",
            "Ecuador","Guayanas","Peru","Paraguay","Uruguay","Venezuela")

###########################################################################
# FRbio

FR1960<-read.table('td_FP_pais_-1960.txt', header=T)
FR1970<-read.table('td_FP_pais_-1970.txt', header=T)
FR1980<-read.table('td_FP_pais_-1980.txt', header=T)
FR1990<-read.table('td_FP_pais_-1990.txt', header=T)
FR2000<-read.table('td_FP_pais_-2000.txt', header=T)
FR2010<-read.table('td_FP_pais_-2010.txt', header=T)
FR2016<-read.table('td_FP_pais.txt', header=T) 

FR1960<-1/FR1960
FR1970<-1/FR1970
FR1980<-1/FR1980
FR1990<-1/FR1990
FR2000<-1/FR2000
FR2010<-1/FR2010
FR2016<-1/FR2016


FR1960$pais<-paises
FR1970$pais<-paises
FR1980$pais<-paises
FR1990$pais<-paises
FR2000$pais<-paises
FR2010$pais<-paises
FR2016$pais<-paises


FR1960$yr<-1960
FR1970$yr<-1970
FR1980$yr<-1980
FR1990$yr<-1990
FR2000$yr<-2000
FR2010$yr<-2010
FR2016$yr<-2016


FR1960<-FR1960[-c(8,10,13),]
FR1970<-FR1970[-c(8,10,13),]
FR1980<-FR1980[-c(8,10,13),]
FR1990<-FR1990[-c(8,10,13),]
FR2000<-FR2000[-c(8,10,13),]
FR2010<-FR2010[-c(8,10,13),]
FR2016<-FR2016[-c(8,10,13),]


FRunion<-rbind(FR1960,FR1970,FR1980,FR1990,FR2000,FR2010,FR2016)
colnames(FRunion)<- c("FR", "pais", "yr")
FRunion <- FRunion[order(FRunion$pais),]
class(FRunion)


FRplot <- ggplot(data=FRunion, aes(x=yr, y=FR)) +
  geom_smooth(linetype=1, size=0.5, colour = "gray80", se=FALSE) +
  geom_point(size=1, colour = "red4") +
  # facet_grid(. ~ pais)
  facet_wrap(~pais, ncol=4) + 
  theme_grey(base_size = 8, base_family = "") +
  # geom_line(linetype=1, size=1, colour = "dodgerblue4") +
  labs(x = "Year", y=expression(FR~biophysical~gradients~(G^-1)), size = 2.5)
FRplot

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

# 
# # hay que repetir pero para ecoregiones
# FR1960<-read.table('td_FP_pais_-1960.txt', header=T)



###########################################################################
# AP

QR1960<-read.table('td_AP_pais_-1960.txt', header=T)
QR1970<-read.table('td_AP_pais_-1970.txt', header=T)
QR1980<-read.table('td_AP_pais_-1980.txt', header=T)
QR1990<-read.table('td_AP_pais_-1990.txt', header=T)
QR2000<-read.table('td_AP_pais_-2000.txt', header=T)
QR2010<-read.table('td_AP_pais_-2010.txt', header=T)
QR2016<-read.table('td_AP_pais.txt', header=T) 

QR1960<-1/QR1960
QR1970<-1/QR1970
QR1980<-1/QR1980
QR1990<-1/QR1990
QR2000<-1/QR2000
QR2010<-1/QR2010
QR2016<-1/QR2016


QR1960$pais<-paises
QR1970$pais<-paises
QR1980$pais<-paises
QR1990$pais<-paises
QR2000$pais<-paises
QR2010$pais<-paises
QR2016$pais<-paises


QR1960$yr<-1960
QR1970$yr<-1970
QR1980$yr<-1980
QR1990$yr<-1990
QR2000$yr<-2000
QR2010$yr<-2010
QR2016$yr<-2016


QR1960<-QR1960[-c(8,10,13),]
QR1970<-QR1970[-c(8,10,13),]
QR1980<-QR1980[-c(8,10,13),]
QR1990<-QR1990[-c(8,10,13),]
QR2000<-QR2000[-c(8,10,13),]
QR2010<-QR2010[-c(8,10,13),]
QR2016<-QR2016[-c(8,10,13),]


QRunion<-rbind(QR1960,QR1970,QR1980,QR1990,QR2000,QR2010,QR2016)
colnames(QRunion)<- c("QR", "pais", "yr")
QRunion <- QRunion[order(QRunion$pais),]
class(QRunion)

QRplot <- ggplot(data=QRunion, aes(x=yr, y=QR)) +
  geom_smooth(linetype=1, size=0.5, colour = "gray80", se=FALSE) +
  geom_point(size=1, colour = "gray25") +
  # facet_grid(. ~ pais)
  facet_wrap(~pais, ncol=4) + 
  theme_grey(base_size = 8, base_family = "") +
  # geom_line(linetype=1, size=1, colour = "dodgerblue4") +
  labs(x = "Year", y=expression(QR~biophysical~gradients~(G^-1)), size = 2.5)
QRplot

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


################################################################################
# TASAS DE REPRESENTATIVIDAD:FRACCION PROTEGIDA
# LA FRACCION PROTEGIDA PROVIENE DEL SCRIPT datos_basicos_historico.R

FP1960<-read.table('D:\\areas_protegidas\\xls_dbf\\salidas txt\\FP_pais_-1960.txt', header=T)
FP1970<-read.table('D:\\areas_protegidas\\xls_dbf\\salidas txt\\FP_pais_-1970.txt', header=T)
FP1980<-read.table('D:\\areas_protegidas\\xls_dbf\\salidas txt\\FP_pais_-1980.txt', header=T)
FP1990<-read.table('D:\\areas_protegidas\\xls_dbf\\salidas txt\\FP_pais_-1990.txt', header=T)
FP2000<-read.table('D:\\areas_protegidas\\xls_dbf\\salidas txt\\FP_pais_-2000.txt', header=T)
FP2010<-read.table('D:\\areas_protegidas\\xls_dbf\\salidas txt\\FP_pais_-2010.txt', header=T)
FP2016<-read.table('D:\\areas_protegidas\\xls_dbf\\salidas txt\\FP_pais_-2016.txt', header=T)

FP1960$yr<-1960
FP1970$yr<-1970
FP1980$yr<-1980
FP1990$yr<-1990
FP2000$yr<-2000
FP2010$yr<-2010
FP2016$yr<-2016

FP1960<-FP1960[,-c(1,3)]
FP1970<-FP1970[,-c(1,3)]
FP1980<-FP1980[,-c(1,3)]
FP1990<-FP1990[,-c(1,3)]
FP2000<-FP2000[,-c(1,3)]
FP2010<-FP2010[,-c(1,3)]
FP2016<-FP2016[,-c(1,3)]

FPunion <- rbind(FP1960,FP1970,FP1980,FP1990,FP2000,FP2010,FP2016)
FPunion <- FPunion[order(FPunion$pais),]
write.table(FPunion, file = "datos_basicos_historicos.txt", col.names = T, row.names = F)

merge_FR_FP <- merge(FPunion, FRunion, na.rm=FALSE) # revisar
merge_FR_FP$FR_FP <-  merge_FR_FP$FR/merge_FR_FP$FP
write.table(merge_FR_FP, file = "merge_FR_FP.txt", col.names = T, row.names = F)

merge_QR_FP <- merge(FPunion, QRunion, na.rm=FALSE) # revisar
merge_QR_FP$QR_FP <-  merge_QR_FP$QR/merge_QR_FP$FP
write.table(merge_QR_FP, file = "merge_QR_FP.txt", col.names = T, row.names = F)

############################################################################
# le agrego la relación FR_FP o el FP

FR_FPplot1 <- ggplot(data=merge_FR_FP) +
  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~biophysical~gradients~(G^-1)), size = "FR:PF")
FR_FPplot1

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

FR_FPplot2 <- ggplot(data=merge_FR_FP) +
  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=expression(FR~biophysical~gradients~(G^-1)), size = "Protected fraction (%)")
FR_FPplot2

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

FR_FPplot3 <- ggplot(data=merge_FR_FP,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 = "Protected fraction (%)", y=expression(FR~biophysical~gradients~(G^-1)), size = 2.5)
FR_FPplot3

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

# le agrego la relación QR_FP

QR_FPplot1 <- ggplot(data=merge_QR_FP) +
  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 = "gray25") +
  scale_size_area(limits=c(0,8), max_size=3) +
  # geom_col(aes(x=yr, y=FP), colour = "red") +
  facet_wrap(~pais, ncol=4) + 
  theme_grey(base_size = 8, base_family = "") +
  # geom_line(linetype=1, size=1, colour = "dodgerblue4") +
  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~biophysical~gradients~(G^-1)), size = "FR:PF")
QR_FPplot1

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

QR_FPplot2<- ggplot(data=merge_QR_FP) +
  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,8), max_size=3) +
  # geom_col(aes(x=yr, y=FP), colour = "red") +
  facet_wrap(~pais, ncol=4) + 
  theme_grey(base_size = 8, base_family = "") +
  # geom_line(linetype=1, size=1, colour = "dodgerblue4") +
  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~biophysical~gradients~(G^-1)), size = "Protected fraction (%)")
QR_FPplot2

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

QR_FPplot3 <- ggplot(data=merge_QR_FP,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 = "Protected fraction (%)", y=expression(QR~biophysical~gradients~(G^-1)), size = 2.5)
QR_FPplot3

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


########################################
# para explorar el salto entre periodos
# merge_FR_FP_salto <- subset(merge_FR_FP, merge_FR_FP$yr == 1990 | merge_FR_FP$yr == 2000 | merge_FR_FP$yr == 2010 )

salto_FR_FP1 = NULL
salto_FR_FP2 = NULL

for (k in paises2) {
      salto_FR_FP1 = NULL
      dat_pais <- subset(merge_FR_FP, merge_FR_FP$pais == k)
      
      for (p in 2:nrow(dat_pais)) {
      resta <- dat_pais[p,] - dat_pais[p-1,]
      resta$FR_FP <- resta$FR/resta$FP 
      resta$yr <- dat_pais[p,2]
      colnames(resta) <- c("pais","yr","dFP","dFR","dFR_FP")
      resta$pais <- k
      salto_FR_FP1 = rbind(salto_FR_FP1, resta) 
      }
    
    salto_FR_FP2 <- rbind(salto_FR_FP1, salto_FR_FP2)
}

print(salto_FR_FP2)

write.table(salto_FR_FP2, file = "salto_FR_FP.txt", col.names = T, row.names = F)

salto_graph <- ggplot(data=salto_FR_FP2,aes(x=yr, y=dFR_FP)) +
  geom_path(size = 0.5, colour = "gray80") +
  geom_point(size=1.5, colour = "gray25") +
  # ylim(-0.75,2)+
  facet_wrap(~pais, ncol=4, scales = "free_y") +
  theme_grey(base_size = 8, base_family = "") +
  # scale_x_continuous(breaks=c("1970","1980","1990","2000","2010","2016"),
  #                  labels=c("-70","70-80","80-90","90-00","00-10","10-16"))+
  scale_x_continuous(labels=c("1970"="-70","1980"="70-80","1990"="80-90","2000"="90-00",
                            "2010"="00-10","2016"="10-16"))+
  geom_abline(intercept = 1, slope = 0, colour = "red4", linetype = "dashed")+
  labs(x = "Decade", y=expression(paste(delta,"RF","/", delta,"PF")), size = 2.5)
salto_graph

ggsave("salto_graph.png", plot = salto_graph, scale = 1, width = 16, height = 10, units = "cm",
       dpi = 300, path = '..\\salidas graficas\\graficos sam')
########################################
