General informations


summary(PA_malha)
##       ID             CD_GEOCODI            TIPO            CD_GEOCODM       
##  Length:47935       Length:47935       Length:47935       Length:47935      
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##   NM_MUNICIP        total_ct_area          WDPAID            WDPA_PID        
##  Length:47935       Min.   :5.880e+02   Length:47935       Length:47935      
##  Class :character   1st Qu.:1.161e+05   Class :character   Class :character  
##  Mode  :character   Median :4.833e+05   Mode  :character   Mode  :character  
##                     Mean   :2.235e+07                                        
##                     3rd Qu.:1.870e+07                                        
##                     Max.   :2.250e+09                                        
##                                                                              
##   ORIG_NAME            DESIG             IUCN_CAT            MARINE         
##  Length:47935       Length:47935       Length:47935       Length:47935      
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##    REP_M_AREA        GIS_M_AREA         REP_AREA           GIS_AREA       
##  Min.   :   0.00   Min.   :   0.00   Min.   :    0.00   Min.   :    0.03  
##  1st Qu.:   0.00   1st Qu.:   0.00   1st Qu.:   27.34   1st Qu.:   43.45  
##  Median :   0.00   Median :   0.00   Median :  117.47   Median :  117.98  
##  Mean   :  26.13   Mean   :  38.84   Mean   : 1011.23   Mean   : 1021.80  
##  3rd Qu.:   0.00   3rd Qu.:   0.00   3rd Qu.:  472.45   3rd Qu.:  474.27  
##  Max.   :1358.45   Max.   :1357.76   Max.   :16287.41   Max.   :16359.35  
##  NA's   :46419     NA's   :46419     NA's   :46419      NA's   :46419     
##   STATUS_YR           OWN_TYPE            GRUPO            TREATMENT        
##  Length:47935       Length:47935       Length:47935       Length:47935      
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##  PA_total_area       intersected_PA_area area_occupied_pct  threshold        
##  Min.   :1.550e+02   Min.   :        0   Min.   :  0.000   Length:47935      
##  1st Qu.:1.774e+07   1st Qu.:        0   1st Qu.:  0.000   Class :character  
##  Median :6.742e+07   Median :        0   Median :  0.000   Mode  :character  
##  Mean   :3.702e+08   Mean   :   422920   Mean   :  1.253                     
##  3rd Qu.:1.328e+08   3rd Qu.:        0   3rd Qu.:  0.000                     
##  Max.   :5.181e+09   Max.   :482593013   Max.   :100.000                     
##  NA's   :46419                                                               
##     id_dummy      mang_plan         mang_comitte                  geom      
##  Min.   :  1.0   Length:47935       Length:47935       MULTIPOLYGON :47935  
##  1st Qu.: 52.0   Class :character   Class :character   epsg:4674    :    0  
##  Median : 98.5   Mode  :character   Mode  :character   +proj=long...:    0  
##  Mean   :100.3                                                              
##  3rd Qu.:155.0                                                              
##  Max.   :216.0                                                              
##  NA's   :46419
number_of_pa<-sum(length(unique(PA_malha$ORIG_NAME))) %>% glimpse
##  int 217
number_of_pa<-sum(length(unique(PA_malha$CD_GEOCODI))) %>% glimpse
##  int 47753

#number of PA by treatment
PA_malha %>% 
  st_drop_geometry() %>%
  group_by(TREATMENT) %>%
  summarise(number_of_pa=sum(length(unique(ORIG_NAME)))) %>% 
  print(n = 300)
## # A tibble: 6 × 2
##   TREATMENT number_of_pa
##   <chr>            <int>
## 1 APA                 41
## 2 Non_PA_CT            1
## 3 PI                  47
## 4 RPPN                67
## 5 TI                  45
## 6 US                  16

# Number of CT and PA by each treatment and threshold
PA_malha %>% 
  st_drop_geometry() %>% 
  #filter(TREATMENT!="Non_PA_CT") %>% 
  group_by(TREATMENT, threshold) %>% 
  summarise(ct_within_category=sum(length(unique(CD_GEOCODI))),
            pa_within_category=sum(length(unique(ORIG_NAME)))) %>%  
  paged_table()
# ps: o treatment is based on 10%. Therefore, with_PA is more than 10%
# ps: o número de PA vai tá sempre duplicado, mas a ideia é que dê pra ver o quanto delas tão como aceitas e o quanto delas perdemos com 10%. Só subtrair without_PA - with_PA para isso. 


#CT amount by each PA

PA_malha %>%
  st_drop_geometry() %>% 
  group_by(TREATMENT, ORIG_NAME) %>%
  summarise(
    PA_total_area=first(PA_total_area),
    intersected_PA_area=first(intersected_PA_area),
    area_occupied_pct=first(area_occupied_pct),
    ct_amount=sum(length(unique(CD_GEOCODI)))
  ) %>% paged_table()

## visualization 
PA_malha %>% 
  filter(ORIG_NAME=="Tapeba") %>% #View()
  ggplot()+
  geom_sf()


## note que existe uma diferença entre a área da PA e a área da soma das intersecções devido a complexidade da nossa geometria (dos CT). Mas é muito próximo o valor
PA_malha %>% 
  filter(ORIG_NAME=="Tapeba") %>% 
  summarise(sum(intersected_PA_area))
## Simple feature collection with 1 feature and 1 field
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -38.7712 ymin: -3.803471 xmax: -38.60123 ymax: -3.652474
## Geodetic CRS:  SIRGAS 2000
## # A tibble: 1 × 2
##   `sum(intersected_PA_area)`                                                geom
##                        <dbl>                                       <POLYGON [°]>
## 1                  49429897. ((-38.74363 -3.774289, -38.74353 -3.772461, -38.74…

Splitting data by each treatment

list_PA_by_treatment<-PA_malha %>% 
  st_drop_geometry() %>% 
  mutate(
    across(where(is.character), as.factor)) %>%
  group_by(TREATMENT) %>%
  group_split()


#non-pa
summary(list_PA_by_treatment[[2]])
##        ID                  CD_GEOCODI        TIPO         CD_GEOCODM   
##  11759  :    2   210090705000024:    1   RURAL :20887   2304400: 3032  
##  11760  :    2   210090705000025:    1   URBANO:25532   2408102:  907  
##  11761  :    2   210090705000026:    1                  2910800:  600  
##  12902  :    2   210090705000031:    1                  2504009:  488  
##  12903  :    2   210090705000032:    1                  2933307:  463  
##  12904  :    2   210090705000033:    1                  3143302:  413  
##  (Other):46407   (Other)        :46413                  (Other):40516  
##                 NM_MUNICIP    total_ct_area           WDPAID     
##  FORTALEZA           : 3032   Min.   :5.880e+02   10821  :    0  
##  NATAL               :  907   1st Qu.:1.134e+05   10842  :    0  
##  FEIRA DE SANTANA    :  600   Median :4.529e+05   10846  :    0  
##  CAMPINA GRANDE      :  488   Mean   :2.087e+07   115666 :    0  
##  VITÓRIA DA CONQUISTA:  463   3rd Qu.:1.722e+07   115992 :    0  
##  MONTES CLAROS       :  413   Max.   :2.250e+09   (Other):    0  
##  (Other)             :40516                       NA's   :46419  
##     WDPA_PID                                                  ORIG_NAME    
##  10821  :    0   Anace                                             :    0  
##  10842  :    0   Area De Protecao Ambiental Bacia Do Rio De Janeiro:    0  
##  10846  :    0   Area De Protecao Ambiental Bacia Do Rio Pandeiros :    0  
##  115666 :    0   Area De Protecao Ambiental Baia De Todos Os Santos:    0  
##  115992 :    0   Area De Protecao Ambiental Barra Do Rio Mamanguape:    0  
##  (Other):    0   (Other)                                           :    0  
##  NA's   :46419   NA's                                              :46419  
##                                    DESIG                 IUCN_CAT    
##  Área de Proteção Ambiental           :    0   Cannot_applied:    0  
##  Área de Relevante Interesse Ecológico:    0   Ia            :    0  
##  Estação Ecológica                    :    0   II            :    0  
##  Floresta                             :    0   III           :    0  
##  Indigenous Area                      :    0   IV            :    0  
##  (Other)                              :    0   (Other)       :    0  
##  NA's                                 :46419   NA's          :46419  
##   MARINE        REP_M_AREA      GIS_M_AREA       REP_AREA        GIS_AREA    
##  0   :    0   Min.   : NA     Min.   : NA     Min.   : NA     Min.   : NA    
##  1   :    0   1st Qu.: NA     1st Qu.: NA     1st Qu.: NA     1st Qu.: NA    
##  2   :    0   Median : NA     Median : NA     Median : NA     Median : NA    
##  NA's:46419   Mean   :NaN     Mean   :NaN     Mean   :NaN     Mean   :NaN    
##               3rd Qu.: NA     3rd Qu.: NA     3rd Qu.: NA     3rd Qu.: NA    
##               Max.   : NA     Max.   : NA     Max.   : NA     Max.   : NA    
##               NA's   :46419   NA's   :46419   NA's   :46419   NA's   :46419  
##    STATUS_YR                      OWN_TYPE                   GRUPO      
##  0      :    0   Communal             :    0   Área Indígena    :    0  
##  1946   :    0   Individual landowners:    0   Proteção Integral:    0  
##  1950   :    0   Not Reported         :    0   Uso Sustentável  :    0  
##  1961   :    0   NA's                 :46419   NA's             :46419  
##  1973   :    0                                                          
##  (Other):    0                                                          
##  NA's   :46419                                                          
##      TREATMENT     PA_total_area   intersected_PA_area area_occupied_pct  
##  APA      :    0   Min.   : NA     Min.   :  0.00000   Min.   :0.000e+00  
##  Non_PA_CT:46419   1st Qu.: NA     1st Qu.:  0.00000   1st Qu.:0.000e+00  
##  PI       :    0   Median : NA     Median :  0.00000   Median :0.000e+00  
##  RPPN     :    0   Mean   :NaN     Mean   :  0.00566   Mean   :8.200e-09  
##  TI       :    0   3rd Qu.: NA     3rd Qu.:  0.00000   3rd Qu.:0.000e+00  
##  US       :    0   Max.   : NA     Max.   :262.52180   Max.   :3.789e-04  
##                    NA's   :46419                                          
##       threshold        id_dummy        mang_plan       mang_comitte  
##  Non_PA_CT :46418   Min.   : NA     Não     :    0   Não     :    0  
##  with_PA   :    0   1st Qu.: NA     Sem_info:    0   Sem_info:    0  
##  without_PA:    1   Median : NA     Sim     :    0   Sim     :    0  
##                     Mean   :NaN     TI      :    0   TI      :    0  
##                     3rd Qu.: NA     NA's    :46419   NA's    :46419  
##                     Max.   : NA                                      
##                     NA's   :46419
# acho que não tem muito oq mostrar aqui

APA

summary(list_PA_by_treatment[[1]])
##        ID                CD_GEOCODI      TIPO       CD_GEOCODM 
##  37368  :  2   210090705000010:  2   RURAL :371   2100907: 64  
##  37369  :  2   210090705000011:  2   URBANO:158   2902708: 56  
##  40187  :  2   210090705000012:  2                2910800: 54  
##  40188  :  2   210090705000013:  2                2512903: 31  
##  41276  :  2   210090705000014:  2                2305803: 25  
##  541    :  2   210090705000015:  2                2301406: 19  
##  (Other):517   (Other)        :517                (Other):280  
##             NM_MUNICIP  total_ct_area             WDPAID         WDPA_PID  
##  ARAIOSES        : 64   Min.   :    11490   351861   : 67   351861   : 67  
##  BARRA           : 56   1st Qu.:   476438   555682870: 55   555682870: 55  
##  FEIRA DE SANTANA: 54   Median : 13263975   352131   : 41   352131   : 41  
##  RIO TINTO       : 31   Mean   : 71625386   115666   : 32   115666   : 32  
##  IPU             : 25   3rd Qu.: 82742808   115993   : 31   115993   : 31  
##  ARATUBA         : 19   Max.   :783615615   33544    : 25   33544    : 25  
##  (Other)         :280                       (Other)  :278   (Other)  :278  
##                                                                                             ORIG_NAME  
##  Area De Protecao Ambiental Dunas E Veredas Do Baixo Medio Sao Francisco                         : 67  
##  Area De Protecao Ambiental Lago De Pedra Do Cavalo                                              : 55  
##  Area De Protecao Ambiental Da Foz Do Rio Das Preguicas Pequenos Lencois Regiao Lagunar Adjacente: 41  
##  Area De Protecao Ambiental Delta Do Parnaiba                                                    : 32  
##  Area De Protecao Ambiental Barra Do Rio Mamanguape                                              : 31  
##  Area De Protecao Ambiental Da Bica Do Ipu                                                       : 25  
##  (Other)                                                                                         :278  
##                                    DESIG               IUCN_CAT   MARINE 
##  Área de Proteção Ambiental           :529   Cannot_applied:  0   0:403  
##  Área de Relevante Interesse Ecológico:  0   Ia            :  0   1:113  
##  Estação Ecológica                    :  0   II            :  0   2: 13  
##  Floresta                             :  0   III           :  0          
##  Indigenous Area                      :  0   IV            :  0          
##  Monumento Natural                    :  0   V             :529          
##  (Other)                              :  0   VI            :  0          
##    REP_M_AREA          GIS_M_AREA          REP_AREA            GIS_AREA        
##  Min.   :   0.0000   Min.   :   0.000   Min.   :    0.314   Min.   :    0.316  
##  1st Qu.:   0.0000   1st Qu.:   0.000   1st Qu.:  149.186   1st Qu.:  149.851  
##  Median :   0.0000   Median :   0.000   Median :  472.449   Median :  474.270  
##  Mean   :  72.2872   Mean   : 107.053   Mean   : 2477.690   Mean   : 2487.854  
##  3rd Qu.:   0.1078   3rd Qu.:   1.556   3rd Qu.: 3095.901   3rd Qu.: 3109.663  
##  Max.   :1358.4482   Max.   :1357.761   Max.   :16287.406   Max.   :16359.349  
##                                                                                
##    STATUS_YR                    OWN_TYPE                 GRUPO    
##  1997   :138   Communal             :  0   Área Indígena    :  0  
##  1998   : 61   Individual landowners:  0   Proteção Integral:  0  
##  1999   : 51   Not Reported         :529   Uso Sustentável  :529  
##  1991   : 45                                                      
##  1996   : 39                                                      
##  2000   : 35                                                      
##  (Other):160                                                      
##      TREATMENT   PA_total_area       intersected_PA_area area_occupied_pct 
##  APA      :529   Min.   :6.038e+03   Min.   :        0   Min.   :  0.0000  
##  Non_PA_CT:  0   1st Qu.:3.563e+07   1st Qu.:    51267   1st Qu.:  0.4577  
##  PI       :  0   Median :9.292e+07   Median :   289448   Median : 54.0515  
##  RPPN     :  0   Mean   :8.873e+08   Mean   : 23876010   Mean   : 50.4246  
##  TI       :  0   3rd Qu.:6.468e+08   3rd Qu.:  6404108   3rd Qu.: 99.9983  
##  US       :  0   Max.   :5.181e+09   Max.   :482593013   Max.   :100.0000  
##                                                                            
##       threshold      id_dummy         mang_plan     mang_comitte
##  Non_PA_CT :  1   Min.   :  2.00   Não     : 91   Não     :291  
##  with_PA   :327   1st Qu.: 28.00   Sem_info:424   Sem_info: 96  
##  without_PA:201   Median : 72.00   Sim     : 14   Sim     :142  
##                   Mean   : 76.92   TI      :  0   TI      :  0  
##                   3rd Qu.:115.00                                
##                   Max.   :200.00                                
## 

list_PA_by_treatment[[1]] %>% 
  group_by(ORIG_NAME, CD_GEOCODI) %>%
  dplyr::select(PA_total_area, intersected_PA_area, area_occupied_pct) %>%
  paged_table()

list_PA_by_treatment[[1]] %>% 
  group_by(ORIG_NAME, threshold) %>% 
  summarise(ct_within_category=sum(length(unique(CD_GEOCODI)))) %>% 
  paged_table()

## Figure - CT amount by APA
prep_APA <- list_PA_by_treatment[[1]] %>% 
  group_by(ORIG_NAME) %>%
  arrange(CD_GEOCODI) %>%  
  mutate(
    cumulative_area = cumsum(intersected_PA_area),  
    x_position = row_number()  
  ) %>%
  ungroup()

unique_APA_names <- unique(prep_APA$ORIG_NAME)

for (name in unique_APA_names) {
  data_filtered <- prep_APA %>% filter(ORIG_NAME == name)
  
  p <- ggplot(data_filtered, aes(x = x_position, y = cumulative_area)) +
    geom_line(aes(color = threshold, group = 1), size = 1) +  
    geom_point(aes(color = threshold, group = 1 ), size = 2) +  
    scale_color_manual(values = c("with_PA" = "green", "without_PA" = "red"))+
    labs(title = name,
         y="Total acumulado da intersecção da área da PA por CT",
         x="Número de CT") +
    theme_classic()
  
  print(p)
}
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# perceba que a definição de tratamento é por proporção da área no CT, enquanto o nosso y só diz respeito a acumulação da área da PA
# tudo que tá em verde é pq mais de 10% do seu CT, enquanto oq tá em vermelho é pq ocupa menos que isso. E isso é medido pelo ponto em si, e não pela linha ;)
# tem APA que vai ser excluida com 10%

PI


summary(list_PA_by_treatment[[3]])
##        ID                CD_GEOCODI      TIPO       CD_GEOCODM     NM_MUNICIP 
##  19971  :  2   290370605000007:  2   RURAL :223   2903706: 23   BOA NOVA: 23  
##  36799  :  2   290370605000008:  2   URBANO: 30   2602803: 14   BUÍQUE  : 14  
##  36800  :  2   290370605000009:  2                3135209: 13   JANUÁRIA: 13  
##  36801  :  2   290370605000012:  2                2901304: 11   ANDARAÍ : 11  
##  36804  :  2   290370605000014:  2                2304707: 10   GRANJA  : 10  
##  36806  :  2   290370605000019:  2                2908101:  9   COCOS   :  9  
##  (Other):241   (Other)        :241                (Other):173   (Other) :173  
##  total_ct_area             WDPAID         WDPA_PID  
##  Min.   :8.658e+04   19447    : 20   19447    : 20  
##  1st Qu.:1.501e+07   555576384: 15   555576384: 15  
##  Median :5.816e+07   351760   : 14   351760   : 14  
##  Mean   :1.226e+08   478424   : 12   478424   : 12  
##  3rd Qu.:1.511e+08   555576380: 11   555576380: 11  
##  Max.   :1.982e+09   555576442: 10   555576442: 10  
##                      (Other)  :171   (Other)  :171  
##                                   ORIG_NAME                          DESIG    
##  Parque Nacional Da Chapada Diamantina : 20   Parque                    :157  
##  Refugio De Vida Silvestre De Boa Nova : 15   Refúgio de Vida Silvestre : 35  
##  Parque Nacional Do Catimbau           : 14   Estação Ecológica         : 22  
##  Monumento Natural Do Rio Sao Francisco: 12   Monumento Natural         : 22  
##  Parque Estadual Das Carnaubas         : 11   Reserva Biológica         : 17  
##  Parque Nacional De Boa Nova           : 10   Área de Proteção Ambiental:  0  
##  (Other)                               :171   (Other)                   :  0  
##            IUCN_CAT   MARINE    REP_M_AREA        GIS_M_AREA     
##  Cannot_applied:  0   0:246   Min.   : 0.0000   Min.   : 0.0000  
##  Ia            : 39   1:  7   1st Qu.: 0.0000   1st Qu.: 0.0000  
##  II            :157   2:  0   Median : 0.0000   Median : 0.0000  
##  III           : 57           Mean   : 0.3545   Mean   : 0.3744  
##  IV            :  0           3rd Qu.: 0.0000   3rd Qu.: 0.0000  
##  V             :  0           Max.   :22.3719   Max.   :23.4784  
##  VI            :  0                                              
##     REP_AREA           GIS_AREA          STATUS_YR  
##  Min.   :   0.148   Min.   :   0.148   2010   : 46  
##  1st Qu.:  63.037   1st Qu.:  63.315   2002   : 34  
##  Median : 148.336   Median : 148.877   1985   : 20  
##  Mean   : 621.802   Mean   : 624.241   2006   : 19  
##  3rd Qu.: 622.953   3rd Qu.: 625.562   2009   : 14  
##  Max.   :8238.508   Max.   :8272.734   1990   : 13  
##                                        (Other):107  
##                   OWN_TYPE                 GRUPO         TREATMENT  
##  Communal             :  0   Área Indígena    :  0   APA      :  0  
##  Individual landowners:  0   Proteção Integral:253   Non_PA_CT:  0  
##  Not Reported         :253   Uso Sustentável  :  0   PI       :253  
##                                                      RPPN     :  0  
##                                                      TI       :  0  
##                                                      US       :  0  
##                                                                     
##  PA_total_area       intersected_PA_area area_occupied_pct      threshold  
##  Min.   :1.550e+02   Min.   :        0   Min.   :  0.000   Non_PA_CT :  1  
##  1st Qu.:1.171e+07   1st Qu.:    32258   1st Qu.:  0.104   with_PA   : 87  
##  Median :7.207e+07   Median :   335128   Median :  1.626   without_PA:165  
##  Mean   :1.559e+08   Mean   : 17906939   Mean   : 17.733                   
##  3rd Qu.:1.328e+08   3rd Qu.:  9458301   3rd Qu.: 25.807                   
##  Max.   :1.230e+09   Max.   :395238155   Max.   :100.000                   
##                                                                            
##     id_dummy         mang_plan     mang_comitte
##  Min.   :  5.00   Não     : 17   Não     :116  
##  1st Qu.: 21.00   Sem_info:171   Sem_info: 72  
##  Median : 83.00   Sim     : 65   Sim     : 65  
##  Mean   : 83.59   TI      :  0   TI      :  0  
##  3rd Qu.:131.00                                
##  Max.   :215.00                                
## 

list_PA_by_treatment[[3]] %>% 
  group_by(ORIG_NAME, CD_GEOCODI) %>%
  dplyr::select(PA_total_area,intersected_PA_area, area_occupied_pct) %>% 
  paged_table()

list_PA_by_treatment[[3]] %>% 
  group_by(ORIG_NAME, threshold) %>% 
  summarise(ct_within_category=sum(length(unique(CD_GEOCODI)))) %>% 
  paged_table()

## Figure - CT amount by PI
prep_PI <- list_PA_by_treatment[[3]] %>% 
  group_by(ORIG_NAME) %>%
  arrange(CD_GEOCODI) %>%  
  mutate(
    cumulative_area = cumsum(intersected_PA_area),  
    x_position = row_number()  
  ) %>%
  ungroup()

unique_PI_names <- unique(prep_PI$ORIG_NAME)

for (name in unique_PI_names) {
  data_filtered <- prep_PI %>% filter(ORIG_NAME == name)
  
  p <- ggplot(data_filtered, aes(x = x_position, y = cumulative_area)) +
    geom_line(aes(color = threshold, group = 1), size = 1) +  
    geom_point(aes(color = threshold, group = 1 ), size = 2) +  
    scale_color_manual(values = c("with_PA" = "green", "without_PA" = "red"))+
    labs(title = name,
         y="Total acumulado da intersecção da área da PA por CT",
         x="Número de CT") +
    theme_classic()
  
  print(p)
}

RPPN

summary(list_PA_by_treatment[[4]]) 
##        ID                CD_GEOCODI      TIPO       CD_GEOCODM
##  47377  :  5   292020515000008:  5   RURAL :124   2310308:23  
##  45340  :  4   291735905000029:  4   URBANO: 21   2920205: 8  
##  23368  :  3   260160710000002:  3                2601607: 4  
##  47372  :  3   292020515000003:  3                2606408: 4  
##  3534   :  2   270900405000005:  2                2908101: 4  
##  10152  :  1   210090705000072:  1                2917359: 4  
##  (Other):127   (Other)        :127                (Other):98  
##                   NM_MUNICIP total_ct_area             WDPAID   
##  PARAMBU               :23   Min.   :    74678   555682596: 23  
##  MALHADA               : 8   1st Qu.: 18381881   555600236:  4  
##  BELÉM DO SÃO FRANCISCO: 4   Median : 50508749   555600144:  3  
##  COCOS                 : 4   Mean   :117530406   555600235:  3  
##  GRAVATÁ               : 4   3rd Qu.:185165659   555682526:  3  
##  JABORANDI             : 4   Max.   :947190009   555682540:  3  
##  (Other)               :98                       (Other)  :106  
##       WDPA_PID  
##  555682596: 23  
##  555600236:  4  
##  555600144:  3  
##  555600235:  3  
##  555682526:  3  
##  555682540:  3  
##  (Other)  :106  
##                                                                 ORIG_NAME  
##  Reserva Particular Do Patrimonio Natural Fazenda Olho DAgua Do Urucu: 23  
##  Reserva Particular Do Patrimonio Natural Pico Do Barbado            :  4  
##  Reserva Particular Do Patrimonio Natural Adilia Paraguassu Batista  :  3  
##  Reserva Particular Do Patrimonio Natural Fazenda Almas              :  3  
##  Reserva Particular Do Patrimonio Natural Fazenda Boa Ventura        :  3  
##  Reserva Particular Do Patrimonio Natural Fazenda Tamandua           :  3  
##  (Other)                                                             :106  
##                                       DESIG               IUCN_CAT   MARINE 
##  Reserva Particular do Patrimônio Natural:145   Cannot_applied:  0   0:145  
##  Área de Proteção Ambiental              :  0   Ia            :  0   1:  0  
##  Área de Relevante Interesse Ecológico   :  0   II            :  0   2:  0  
##  Estação Ecológica                       :  0   III           :  0          
##  Floresta                                :  0   IV            :145          
##  Indigenous Area                         :  0   V             :  0          
##  (Other)                                 :  0   VI            :  0          
##    REP_M_AREA   GIS_M_AREA    REP_AREA           GIS_AREA          STATUS_YR 
##  Min.   :0    Min.   :0    Min.   : 0.03476   Min.   : 0.03488   1991   :23  
##  1st Qu.:0    1st Qu.:0    1st Qu.: 1.31138   1st Qu.: 1.31683   2002   :17  
##  Median :0    Median :0    Median : 2.91187   Median : 2.92446   2000   :12  
##  Mean   :0    Mean   :0    Mean   : 9.34245   Mean   : 9.38210   2001   :12  
##  3rd Qu.:0    3rd Qu.:0    3rd Qu.:15.24091   3rd Qu.:15.29550   2009   :11  
##  Max.   :0    Max.   :0    Max.   :58.43412   Max.   :58.68946   1998   :10  
##                                                                  (Other):60  
##                   OWN_TYPE                 GRUPO         TREATMENT  
##  Communal             :  0   Área Indígena    :  0   APA      :  0  
##  Individual landowners:145   Proteção Integral:  0   Non_PA_CT:  0  
##  Not Reported         :  0   Uso Sustentável  :145   PI       :  0  
##                                                      RPPN     :145  
##                                                      TI       :  0  
##                                                      US       :  0  
##                                                                     
##  PA_total_area      intersected_PA_area area_occupied_pct       threshold  
##  Min.   :   21582   Min.   :       0    Min.   :  0.0000   Non_PA_CT :  0  
##  1st Qu.:  784974   1st Qu.:   68471    1st Qu.:  0.1344   with_PA   : 30  
##  Median : 2009351   Median :  455000    Median :  1.1087   without_PA:115  
##  Mean   : 7553129   Mean   : 1860521    Mean   : 14.1871                   
##  3rd Qu.: 9403487   3rd Qu.: 1639794    3rd Qu.:  5.1700                   
##  Max.   :30377439   Max.   :21303100    Max.   :100.0000                   
##                                                                            
##     id_dummy        mang_plan     mang_comitte
##  Min.   :  3.0   Não     :  2   Não     :  0  
##  1st Qu.: 74.0   Sem_info:143   Sem_info:145  
##  Median :108.0   Sim     :  0   Sim     :  0  
##  Mean   :113.8   TI      :  0   TI      :  0  
##  3rd Qu.:158.0                                
##  Max.   :214.0                                
## 


list_PA_by_treatment[[4]] %>% 
  group_by(ORIG_NAME, CD_GEOCODI) %>%
  dplyr::select(PA_total_area, intersected_PA_area, area_occupied_pct) %>% 
  paged_table()

list_PA_by_treatment[[4]] %>% 
  group_by(ORIG_NAME, threshold) %>% 
  summarise(ct_within_category=sum(length(unique(CD_GEOCODI)))) %>% 
  paged_table()

# 27 CTs
PA_malha%>%
  filter(ORIG_NAME=="Reserva Particular Do Patrimonio Natural Fazenda Olho DAgua Do Urucu") %>% 
  ggplot()+geom_sf()

  
## Figure - CT amount by RPPN
prep_RPPN <- list_PA_by_treatment[[4]] %>% 
  group_by(ORIG_NAME) %>%
  arrange(CD_GEOCODI) %>%  
  mutate(
    cumulative_area = cumsum(intersected_PA_area),  
    x_position = row_number()  
  ) %>%
  ungroup()

unique_RPPN_names <- unique(prep_RPPN$ORIG_NAME)

for (name in unique_RPPN_names) {
  data_filtered <- prep_RPPN %>% filter(ORIG_NAME == name)
  
  p <- ggplot(data_filtered, aes(x = x_position, y = cumulative_area)) +
    geom_line(aes(color = threshold, group = 1), size = 1) +  
    geom_point(aes(color = threshold, group = 1 ), size = 2) +  
    scale_color_manual(values = c("with_PA" = "green", "without_PA" = "red"))+
    labs(title = name,
         y="Total acumulado da intersecção da área da PA por CT",
         x="Número de CT") +
    theme_classic()
  
  print(p)
}

TI

summary(list_PA_by_treatment[[5]]) 
##        ID                CD_GEOCODI      TIPO       CD_GEOCODM 
##  24383  :  3   260300905000018:  3   RURAL :349   2303709: 85  
##  24419  :  3   260300905000054:  3   URBANO:158   2610905: 62  
##  15852  :  2   230020005000027:  2                2603009: 44  
##  15863  :  2   230020040000003:  2                2600500: 39  
##  24382  :  2   250890105000034:  2                2706307: 24  
##  24384  :  2   251290305000024:  2                2902658: 20  
##  (Other):493   (Other)        :493                (Other):233  
##                NM_MUNICIP  total_ct_area           WDPAID       WDPA_PID  
##  CAUCAIA            : 85   Min.   :    11481   352403 : 80   352403 : 80  
##  PESQUEIRA          : 62   1st Qu.:   195795   352583 : 64   352583 : 64  
##  CABROBÓ            : 44   Median :  2726414   352595 : 41   352595 : 41  
##  ÁGUAS BELAS        : 39   Mean   : 23514607   352407 : 27   352407 : 27  
##  PALMEIRA DOS ÍNDIOS: 24   3rd Qu.: 27540666   352598 : 24   352598 : 24  
##  BANZAÊ             : 20   Max.   :327060847   352442 : 23   352442 : 23  
##  (Other)            :233                       (Other):248   (Other):248  
##         ORIG_NAME                                     DESIG    
##  Tapeba      : 80   Terra Indígena                       :452  
##  Xukuru      : 64   Reserva Indígena                     : 51  
##  Fulnio      : 41   Indigenous Area                      :  4  
##  Truka_352407: 27   Área de Proteção Ambiental           :  0  
##  XukuruKariri: 24   Área de Relevante Interesse Ecológico:  0  
##  Kiriri      : 23   Estação Ecológica                    :  0  
##  (Other)     :248   (Other)                              :  0  
##            IUCN_CAT   MARINE    REP_M_AREA   GIS_M_AREA         REP_AREA     
##  Cannot_applied:507   0:481   Min.   :0    Min.   : 0.0000   Min.   :  0.00  
##  Ia            :  0   1: 26   1st Qu.:0    1st Qu.: 0.0000   1st Qu.: 17.31  
##  II            :  0   2:  0   Median :0    Median : 0.0000   Median : 57.69  
##  III           :  0           Mean   :0    Mean   : 0.7098   Mean   : 99.58  
##  IV            :  0           3rd Qu.:0    3rd Qu.: 0.0000   3rd Qu.:123.00  
##  V             :  0           Max.   :0    Max.   :12.0202   Max.   :464.16  
##  VI            :  0                                                          
##     GIS_AREA         STATUS_YR                    OWN_TYPE  
##  Min.   :  2.493   2009   :103   Communal             :  4  
##  1st Qu.: 48.041   2006   : 86   Individual landowners:  0  
##  Median :103.500   2008   : 82   Not Reported         :503  
##  Mean   :119.250   1996   : 65                              
##  3rd Qu.:123.871   2002   : 51                              
##  Max.   :464.911   2010   : 27                              
##                    (Other): 93                              
##                GRUPO         TREATMENT   PA_total_area      
##  Área Indígena    :507   APA      :  0   Min.   :      741  
##  Proteção Integral:  0   Non_PA_CT:  0   1st Qu.: 44560238  
##  Uso Sustentável  :  0   PI       :  0   Median : 58983979  
##                          RPPN     :  0   Mean   : 85663630  
##                          TI       :507   3rd Qu.:107139206  
##                          US       :  0   Max.   :275818109  
##                                                             
##  intersected_PA_area area_occupied_pct       threshold      id_dummy    
##  Min.   :        0   Min.   :  0.0000   Non_PA_CT :  2   Min.   :  4.0  
##  1st Qu.:    23415   1st Qu.:  0.3807   with_PA   :300   1st Qu.: 80.0  
##  Median :   111883   Median : 40.2314   without_PA:205   Median :155.0  
##  Mean   :  4335981   Mean   : 48.7349                    Mean   :129.2  
##  3rd Qu.:  1654140   3rd Qu.: 99.9650                    3rd Qu.:178.0  
##  Max.   :102318015   Max.   :100.0000                    Max.   :216.0  
##                                                                         
##     mang_plan     mang_comitte
##  Não     :  0   Não     :  0  
##  Sem_info:  0   Sem_info:  0  
##  Sim     :  0   Sim     :  0  
##  TI      :507   TI      :507  
##                               
##                               
## 


list_PA_by_treatment[[5]] %>% 
  group_by(ORIG_NAME, CD_GEOCODI) %>%
  dplyr::select(PA_total_area,intersected_PA_area, area_occupied_pct) %>% 
  paged_table()

list_PA_by_treatment[[5]] %>% 
  group_by(ORIG_NAME, threshold) %>% 
  summarise(ct_within_category=sum(length(unique(CD_GEOCODI)))) %>% 
  paged_table()

## Figure - CT amount by TI
prep_TI <- list_PA_by_treatment[[5]] %>% 
  group_by(ORIG_NAME) %>%
  arrange(CD_GEOCODI) %>%  
  mutate(
    cumulative_area = cumsum(intersected_PA_area),  
    x_position = row_number()  
  ) %>%
  ungroup()

unique_TI_names <- unique(prep_TI$ORIG_NAME)

for (name in unique_TI_names) {
  data_filtered <- prep_TI %>% filter(ORIG_NAME == name)
  
  p <- ggplot(data_filtered, aes(x = x_position, y = cumulative_area)) +
    geom_line(aes(color = threshold, group = 1), size = 1) +  
    geom_point(aes(color = threshold, group = 1 ), size = 2) +  
    scale_color_manual(values = c("with_PA" = "green", "without_PA" = "red"))+
    labs(title = name,
         y="Total acumulado da intersecção da área da PA por CT",
         x="Número de CT") +
    theme_classic()
  
  print(p)
}

US

summary(list_PA_by_treatment[[6]])
##        ID               CD_GEOCODI     TIPO      CD_GEOCODM
##  10233  : 1   210090705000010: 1   RURAL :64   2100907:18  
##  10237  : 1   210090705000011: 1   URBANO:18   2301901: 7  
##  10238  : 1   210090705000012: 1               2301000: 6  
##  10241  : 1   210090705000013: 1               2304400: 5  
##  11137  : 1   210090705000014: 1               2400208: 5  
##  11146  : 1   210090705000015: 1               2908804: 5  
##  (Other):76   (Other)        :76               (Other):36  
##                 NM_MUNICIP total_ct_area             WDPAID        WDPA_PID 
##  ARAIOSES            :18   Min.   :    29537   351775   :19   351775   :19  
##  BARBALHA            : 7   1st Qu.:  2376728   351788   :11   351788   :11  
##  AQUIRAZ             : 6   Median : 30967424   351770   : 8   351770   : 8  
##  AÇU                 : 5   Mean   : 55382559   220239   : 6   220239   : 6  
##  CONTENDAS DO SINCORÁ: 5   3rd Qu.: 73491451   351765   : 5   351765   : 5  
##  FORTALEZA           : 5   Max.   :345999951   555576297: 5   555576297: 5  
##  (Other)             :36                       (Other)  :28   (Other)  :28  
##                                                 ORIG_NAME 
##  Reserva Extrativista Marinha Do Delta Do Parnaiba   :19  
##  Floresta Nacional Do AraripeApodi                   :11  
##  Reserva Extrativista Do Batoque                     : 8  
##  Floresta Nacional De Contendas Do Sincora           : 6  
##  Area De Relevante Interesse Ecologico Do Sitio Curio: 5  
##  Floresta Nacional De Acu                            : 5  
##  (Other)                                             :28  
##                                     DESIG              IUCN_CAT  MARINE
##  Reserva Extrativista                  :32   Cannot_applied: 0   0:75  
##  Floresta                              :30   Ia            : 0   1: 3  
##  Área de Relevante Interesse Ecológico :16   II            : 0   2: 4  
##  Reserva de Desenvolvimento Sustentável: 4   III           : 0         
##  Área de Proteção Ambiental            : 0   IV            :16         
##  Estação Ecológica                     : 0   V             : 0         
##  (Other)                               : 0   VI            :66         
##    REP_M_AREA         GIS_M_AREA        REP_AREA          GIS_AREA       
##  Min.   :  0.0000   Min.   :  0.00   Min.   :  0.574   Min.   :  0.5766  
##  1st Qu.:  0.0000   1st Qu.:  0.00   1st Qu.:  6.014   1st Qu.:  6.0410  
##  Median :  0.0000   Median :  0.00   Median :112.160   Median :112.5746  
##  Mean   : 15.7322   Mean   : 21.89   Mean   :160.583   Mean   :161.2663  
##  3rd Qu.:  0.9788   3rd Qu.: 26.28   3rd Qu.:270.221   3rd Qu.:271.4224  
##  Max.   :291.7755   Max.   :292.50   Max.   :591.153   Max.   :593.2217  
##                                                                          
##    STATUS_YR                   OWN_TYPE                GRUPO        TREATMENT 
##  2000   :19   Communal             : 0   Área Indígena    : 0   APA      : 0  
##  2003   :12   Individual landowners: 0   Proteção Integral: 0   Non_PA_CT: 0  
##  1946   :11   Not Reported         :82   Uso Sustentável  :82   PI       : 0  
##  2001   :10                                                     RPPN     : 0  
##  1999   : 6                                                     TI       : 0  
##  2006   : 6                                                     US       :82  
##  (Other):18                                                                   
##  PA_total_area       intersected_PA_area area_occupied_pct       threshold 
##  Min.   :     6360   Min.   :       0    Min.   :  0.0000   Non_PA_CT : 0  
##  1st Qu.:  3203299   1st Qu.:   11166    1st Qu.:  0.1269   with_PA   :32  
##  Median : 17735211   Median :  324446    Median :  2.3004   without_PA:50  
##  Mean   : 96084821   Mean   : 7849823    Mean   : 26.1657                  
##  3rd Qu.:163607945   3rd Qu.: 3142025    3rd Qu.: 50.1699                  
##  Max.   :262131668   Max.   :90067926    Max.   :100.0000                  
##                                                                            
##     id_dummy        mang_plan    mang_comitte
##  Min.   :  1.0   Não     :15   Não     :39   
##  1st Qu.: 64.0   Sem_info:50   Sem_info:29   
##  Median : 90.0   Sim     :17   Sim     :14   
##  Mean   : 99.9   TI      : 0   TI      : 0   
##  3rd Qu.:116.0                               
##  Max.   :201.0                               
## 


list_PA_by_treatment[[6]] %>% 
  group_by(ORIG_NAME, CD_GEOCODI) %>%
  dplyr::select(PA_total_area, intersected_PA_area, area_occupied_pct) %>% 
  paged_table()

list_PA_by_treatment[[6]] %>% 
  group_by(ORIG_NAME, threshold) %>% 
  summarise(ct_within_category=sum(length(unique(CD_GEOCODI)))) %>% 
  paged_table()

## Figure - CT amount by US
prep_US <- list_PA_by_treatment[[6]] %>% 
  group_by(ORIG_NAME) %>%
  arrange(CD_GEOCODI) %>%  
  mutate(
    cumulative_area = cumsum(intersected_PA_area),  
    x_position = row_number()  
  ) %>%
  ungroup()

unique_US_names <- unique(prep_US$ORIG_NAME)

for (name in unique_US_names) {
  data_filtered <- prep_US %>% filter(ORIG_NAME == name)
  
  p <- ggplot(data_filtered, aes(x = x_position, y = cumulative_area)) +
    geom_line(aes(color = threshold, group = 1), size = 1) +  
    geom_point(aes(color = threshold, group = 1 ), size = 2) +  
    scale_color_manual(values = c("with_PA" = "green", "without_PA" = "red"))+
    labs(title = name,
         y="Total acumulado da intersecção da área da PA por CT",
         x="Número de CT") +
    theme_classic()
  
  print(p)
}