Pesquería Erizo X y XI Regiones

Análisis Exploratorio Datos Tallas Seguimiento Bentónico 1996-2022

Author

Mauricio Mardones. Inv. Depto Ev. Recursos. IFOP

Published

November 23, 2022

Set de trabajo y librerías utilizadas

knitr::opts_chunk$set(message = FALSE,
                      warning = FALSE,
                      fig.path="Figuras/")
options(bitmapType = "cairo") 
#XQuartz is a mess, put this in your onload to default to cairo instead (https://github.com/tidyverse/ggplot2/issues/2655)
# Lo mapas se hacen mas rapido
# solo para IOs
library(GGally)
library(knitr)
library(tidyverse)
library(patchwork)
library(marmap)
library(mapproj)
library(maps)
library(raster)
library(knitr)
library(ggrepel)
library(sf)
library(ggthemes) # nuevos fondos
library(readxl)
library(performance)
library(ggridges)

Analisis exploratorio y preparación de estructuras de tallas para la evaluación de stockde erizo en las 3 macrozonas.

En este documento se presentan las rutas de análisis exploratorio de estructuras de tallas para cada una de las zonas de evaluación de stock de erizo, a saber; Macrozona X Norte, X Sur y XI, que lleva a cabo el Departamento de Evaluación de Recursos.

Exploración de datos totales

Los datos se actualizan año a año y se van estructurando las estructuras de tallas para cada zona de evaluación

ta9622 <- read.table("Tallas_96_22.csv",sep=",", header = T)
names(ta9622)
 [1] "X"          "REGION"     "PUERTO"     "ANO_ARR"    "MES_ARR"   
 [6] "DIA_ARR"    "FUNCION"    "PROCED"     "CAPTURA"    "DESTINO"   
[11] "PESO_M"     "LONGITUD"   "FRECUENCIA"
dim(ta9622)
[1] 484547     13

Una vez realizado el cbind, procedo a hacer los cruces con los poligonos

pro <- read.csv2("M_Procedencias.csv", header =T, sep=";")
proifop <- read.csv2("M_Procedencias_Poligonos.csv", header =T, sep=",")
names(pro)
 [1] "ACTIVA.INACTIVA"    "X1.seg.y.2..otros"  "USADA_EN"          
 [4] "CODIGO"             "POLIGONO"           "REGION"            
 [7] "NOMBRE_PROCEDENCIA" "OBSERVACIONES_1"    "PUERTO_1"          
[10] "PUERTO_2"           "PUERTO_3"           "PUERTO_4"          
[13] "GRADO_LATITUD"      "MIN_LATITUD"        "SEGUNDO_LATITUD"   
[16] "GRADO_LONGITUD"     "MIN_LONGITUD"       "SEGUNDO_LONGITUD"  
[19] "LATITUD"            "LONGITUD"          
names(proifop)
 [1] "ACTIVA.INACTIVA"    "X1.seg.y.2..otros"  "USADA_EN"          
 [4] "CODIGO"             "POLIGONO"           "REGION"            
 [7] "NOMBRE_PROCEDENCIA" "OBSERVACIONES_1"    "PUERTO_1"          
[10] "PUERTO_2"           "PUERTO_3"           "PUERTO_4"          
[13] "GRADO_LATITUD"      "MIN_LATITUD"        "SEGUNDO_LATITUD"   
[16] "GRADO_LONGITUD"     "MIN_LONGITUD"       "SEGUNDO_LONGITUD"  
[19] "LATITUD"            "LONGITUD"           "POLIGONO_IFOP"     
[22] "IN"                
policru<- subset(proifop, select=c(4,5, 21))
names(policru)
[1] "CODIGO"        "POLIGONO"      "POLIGONO_IFOP"
colnames(policru)<- c("PROCED", "POLIGONO", "POLIGONO_IFOP")
names(policru)
[1] "PROCED"        "POLIGONO"      "POLIGONO_IFOP"
tageo9622 <- merge(policru, ta9622, by="PROCED")

Identifico los registros por año para comprobar lo del 2022

table(tageo9622$ANO_ARR)

 1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008 
 7549  9369  8893 13727  9237  7567 11172 17352 27705 22306 22654 15944 17208 
 2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021 
22783 23438 33490 25131 23569 22228 22225 24235 22607 21448 17761 13545  9327 
 2022 
 9337 

Exploraciòn Datos Talla del 2020

Una vez he generado la base completa, comienzo a explorar los datos de tallas del 2020.

talla2022 <- tageo9622 %>% 
  filter(ANO_ARR >= 2000)

#identifico la cantidad de registros
dim(talla2022)
[1] 442269     15
# Comandos para crear nueva columna de zona  en funcion de los poligonos

talla2022$ZONA[talla2022$POLIGONO %in% c(1,2)] <-1
talla2022$ZONA[talla2022$POLIGONO %in% c(4,5,6,13)] <-2
talla2022$ZONA[talla2022$POLIGONO %in% c(7,8,9,10,11,12)] <-3

talla2022$ZONA<- as.factor(talla2022$ZONA)
class(talla2022$ZONA)
[1] "factor"

Lo primero es ver diferencias entre destinos

El data frame que corresponde usar es el de “talla2020”

p <- ggplot(talla2022, aes(x=LONGITUD, group=DESTINO, fill= DESTINO)) +
    geom_density( alpha=0.4, show.legend = FALSE)+
    facet_wrap(.~ANO_ARR, scales = "free_y", ncol=5) +
    geom_vline(xintercept = 65, color = "red")+
    xlim(40,150)+
    xlab("Longitud (mm.)")+
    theme_bw()
p 

p2 <- ggplot(talla2022, aes(x=LONGITUD, fill=DESTINO, group=DESTINO)) +
    geom_histogram(position = "identity",alpha=0.3, show.legend = F) +
    facet_wrap(.~ANO_ARR, ncol=5, scales = "free_y") +
    #scale_fill_brewer(palette="Dark2")+
    theme_bw() +
    ylab("Frecuencia Absoluta")+
    xlab("Longitud (mm.)")+
    ggtitle("Tallas de erizo Destino Fresco e industria")+
    geom_vline(xintercept = 65, color = "red")+
    geom_vline(xintercept = 70, color = "blue")+
    labs(fill="")
p2

Tallas destino Fresco

M <- ggplot(talla2022 %>% 
              filter(DESTINO==2), aes(LONGITUD)) +
  geom_histogram(binwidth = 3,  color="grey",  alpha=0.4, fill="red", show.legend = FALSE) +
  facet_wrap(.~ANO_ARR, ncol=5) +
  xlim(40,150)+
  scale_x_continuous(breaks = seq(from = 40, to = 150, by = 10))+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  ggtitle("Tallas de erizo Destino Fresco")+
  geom_vline(xintercept = 65, color = "red")+
  geom_vline(xintercept = 70, color = "blue")
M

Tallas destino Industria

M <- ggplot(talla2022 %>% 
              filter(DESTINO==1), aes(LONGITUD)) +
  geom_histogram(binwidth = 3,  color="grey",  alpha=0.4, fill="#225ea8", show.legend = FALSE) +
  facet_wrap(.~ANO_ARR, ncol=5) +
  xlim(40,150)+
  scale_x_continuous(breaks = seq(from = 40, to = 150, by = 10))+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  ggtitle("Tallas de erizo Destino Industria")+
  geom_vline(xintercept = 65, color = "red")+
  geom_vline(xintercept = 70, color = "blue")
M

Estructura de tallas por año y por Zona

jz3 <- ggplot(talla2022 %>% 
                drop_na(), aes(x=LONGITUD, y = as.factor(ANO_ARR), 
                      fill = factor(stat(quantile))))+
  stat_density_ridges(
    geom = "density_ridges_gradient",
    calc_ecdf = TRUE,
    quantiles = c(0.25, 0.75)) +
  scale_fill_manual(
    name = "Probability", values = c("#FF0000A0", "#A0A0A0A0", "#0000FFA0"),
    labels = c("[0, 0.25]", "[0.25, 0.75]", "[0.75, 1]"))+
  facet_wrap(.~ZONA, ncol=3) +   
  geom_vline(xintercept = 65, color = "red")+
  scale_x_continuous(breaks = seq(from = 1, to = 150, by = 10))+
  scale_y_discrete(breaks = seq(from = 1996, to = 2022, by = 1))+
  #scale_fill_viridis_d(name="SubArea")+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  xlim(20,150)+
  xlab("Longitud (cm.)")+
  ylab("")
jz3

Estructura de tallas por año y por Poligono de IFOP

jz3 <- ggplot(talla2022 %>% 
                drop_na(), aes(x=LONGITUD, y = as.factor(ANO_ARR), 
                      fill = factor(stat(quantile))))+
  stat_density_ridges(
    geom = "density_ridges_gradient",
    calc_ecdf = TRUE,
    quantiles = 2, 
    scale = 2,
    rel_min_height = 0.1,
    alpha=0.3) +
  scale_fill_viridis_d(option = "D",
                       name = "50%")+
  facet_wrap(.~POLIGONO, ncol=4) +   
  geom_vline(xintercept = 65, color = "red")+
  scale_x_continuous(breaks = seq(from = 1, to = 150, by = 10))+
  scale_y_discrete(breaks = seq(from = 1996, to = 2022, by = 1))+
  #scale_fill_viridis_d(name="SubArea")+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  xlim(20,150)+
  xlab("Longitud (cm.)")+
  ylab("")
jz3

jz <- ggplot(talla2022 %>% 
                drop_na(), aes(x=LONGITUD, 
                               y = as.factor(ANO_ARR),
                               fill=POLIGONO))+
  #geom_joy(alpha=0.9) +
  geom_density_ridges(stat = "binline", bins = 30, 
                      scale = 1, draw_baseline = FALSE)+
  facet_wrap(.~POLIGONO, ncol=11) +   
  geom_vline(xintercept = 65, color = "red")+
  scale_x_continuous(breaks = seq(from = 1, to = 10, by = 1))+
  scale_y_discrete(breaks = seq(from = 2000, to = 2022, by = 1))+
  scale_fill_viridis_c(name="SubArea")+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  xlim(0,160)+
  xlab("Longitud (cm.)")+
  ylab("")
jz

meant <-talla2022 %>% 
  dplyr::group_by(ANO_ARR, 
           ZONA) %>%
  dplyr::summarise(avg=mean(LONGITUD))
glimpse(meant)
Rows: 86
Columns: 3
Groups: ANO_ARR [23]
$ ANO_ARR <int> 2000, 2000, 2000, 2001, 2001, 2001, 2001, 2002, 2002, 2002, 20…
$ ZONA    <fct> 1, 2, 3, 1, 2, 3, NA, 1, 2, 3, NA, 1, 2, 3, NA, 1, 2, 3, NA, 1…
$ avg     <dbl> 88.36878, 75.49377, 75.79487, 88.31192, 80.68731, 76.67478, 90…
pmea <- ggplot(meant %>% 
                 drop_na(), 
               aes(ANO_ARR,avg))+
    geom_point(shape=21,  aes(fill=ZONA)) +
    stat_smooth(method= "lm", colour='#253494')+
    theme_bw()+ 
    facet_wrap(.~ZONA)+
    scale_x_continuous(breaks = seq(from = 2000, to = 2022, by = 2))+
    #scale_y_discrete(breaks = seq(from = 1, to = 13, by = 1))+
    theme(axis.text.x = element_text(angle = 90, hjust = 2))+
    guides(fill = guide_legend(reverse=F))+
    scale_fill_viridis_d(option="E")+
    ylim(50,100)+
    ylab("") +
    xlab("") +
    ggtitle("Lenght Mean sea urchin fishery")
pmea

Ahora se genera la expansion de tallas a la frecuecnia para obtener los vectores por año y por zona

names(ta9622)
 [1] "X"          "REGION"     "PUERTO"     "ANO_ARR"    "MES_ARR"   
 [6] "DIA_ARR"    "FUNCION"    "PROCED"     "CAPTURA"    "DESTINO"   
[11] "PESO_M"     "LONGITUD"   "FRECUENCIA"
dftalla3<-subset(ta9622,select=c(4,8, 12, 13))
names(dftalla3)
[1] "ANO_ARR"    "PROCED"     "LONGITUD"   "FRECUENCIA"
dim(dftalla3)
[1] 484547      4
colnames(dftalla3) <- c( "ANO"  ,   "PROC",     "TALLA",    "FRECUENCIA")

dftalla3 <- transform(dftalla3, TALLA = as.numeric(TALLA), 
               FRECUENCIA = as.numeric(FRECUENCIA))
dftalla3 <- na.omit(dftalla3)
dim(dftalla3)
[1] 484544      4
head(dftalla3)
   ANO PROC TALLA FRECUENCIA
1 2006 9185    61          6
2 2006 9185    59          4
3 2006 9185    58          1
4 2006 9185    67         10
5 2006 9185    88          1
6 2006 9185    62          8
#ver tallas grandes
# hist y max

Identifico las procedencias de la base

#table(dftalla4$PROC)
unique(dftalla3$PROC)
  [1]  9185  9148  9131  9122  9105  9038  9160  9152  9125  9278  8719  9130
 [13]  8455  8765  9242  9259  9246  9146  9245  9225  9255  9264  9230  9209
 [25]  9571  9572  9919  9254  9271  9679  9282  9300  9985  9217  9875  8904
 [37]  9677  9995  9952  9220  9581  9628  9955  9864  9879  9525  9219  9629
 [49]  9253  9988  9580  9263  9947  8970  9946  9643  9582  9610  9619  9218
 [61]  9203  9232  9231  9270  9138  8757  9156  9164  9283  9175  9195  9202
 [73]  9207  9192  9022  9071  9216  9562  9989  9267  9680  9666  9258  8954
 [85]  8573  9616  8958  9877  9950  9561  9597  9944  9635  9945  9600  9574
 [97]  9564  9603  9269  9212  9268  9595  9573  9986  9250  9243  8506  9276
[109]  9627  9565  9228  8254  9224  8256  8257  9172  9190  9178  9205  9210
[121]  9008  9715  9717  9287  9712  9678  9177  8759  9167  9157  8367  9176
[133]  8439  9248  9655  9664  9604  9173     0  9260  9636  9174  9346  9815
[145]  9284  9328  9460  9422  9182  9142  8615  8600  9711  8699  8620  9792
[157]  8602  9262  9092  9200  9238  9141  9158  9463  9431  9490  9470  8705
[169]  9429  9440  9430  9285  9710  8601  9709  8604  8623  8627  9713  8608
[181]  8534  9789  9351  9186  9714  8606  8716  9110  9281  8718  9072  9050
[193]  9030  9040  9055  9065  9000  9015  9052  9198  9208  8957  9464  9702
[205]  9188  9153  8594  8760  9134  8595  9159  9147  9653  9127  9223  9602
[217]  9392  9247  9876  9559  9594  9555  9222  9553  9658  9233  9612  9204
[229]  9644  9237  9626  9563  9227  8761  9786  9236  9126  9144  9637  9520
[241]  9575  9631  8368  9669  8002  9585  9887  9670  9672  9394  9266  9939
[253]  9918  9943  9991  9685  9671  9560  9406  9691  9633  9566  9652  9399
[265]  8571  8644  9661  8003  9591  9882  9659  8898  9992  9987  9920  9676
[277]  9663  9953  9240  9951  9915  9982  9244  9667  9883 11040  8227  9623
[289]  9641  9642  9650  9554  8007  8222  9940  9980  8211  8210  9956  9551
[301]  9656  9592  8212  9941  9936  9648  9577  8910  9398  9959  8955  9568
[313]  9234  9921  9235  9201  9618  9274  9070  9651  9395  9408  9397  9558
[325]  9692  9689  9624  9067  9931  9608  9681  9074  9087  9878  9257  9916
[337]  8609  9168  9161  8230  8370  8231  8219  9133  9751  8077  9135  9639
[349]  9611  9251  8750  9634  8076  8239  9145  9584  9668  8194  8193  8196
[361]  8195  9120  9475  9720  9476  9162  8258  9621  9942  9683  8906  8438
[373]  9583  9999  9884  8591  9783  9552  9169  8189  9132  9196  9784  9163
[385]  9937  9143  9279  9625  9199  9881  8390  8249  8080  9589  9640  9480
[397]  9599  9277  7777  8359  8179  9184  8364  9703  8365  8360  8200  9165
[409]  8235  8358  8181  8177  8071  8182  8197  8178  8255  8135  8136  8199
[421]  8184  8138  8137  9137  9151  9166

Expansión de tallas para todos los registros. Esto demora un poco

ahora una expansión de las tallas

#expandir long a fracuencias
#esto no lo pude hacer para el total dado q es mucha data
expanav<-dftalla3
#puerto, ano, long, Frec
head(expanav)
   ANO PROC TALLA FRECUENCIA
1 2006 9185    61          6
2 2006 9185    59          4
3 2006 9185    58          1
4 2006 9185    67         10
5 2006 9185    88          1
6 2006 9185    62          8
filas<-nrow(expanav)

aux<-list();auxA<-list();auxB<-list()
for(i in 1:filas){
  aux[[i]]<-rep(expanav[i,3],expanav[i,4])
  auxA[[i]]<-rep(expanav[i,2],expanav[i,4])
  auxB[[i]]<-rep(expanav[i,1],expanav[i,4])
  
}


length(aux)
[1] 484544
length(auxA)
[1] 484544
length(auxB)
[1] 484544
#aux<-as.numeric(aux)
Long<-unlist(aux)
Proc<-unlist(auxA)
Year<-unlist(auxB)
#Puerto<-unlist(auxB);Puerto
tt<-cbind(Long,Proc, Year)
colnames(tt)<-c("Long","Proc", "Year")
dim(tt)
[1] 2792662       3
head(tt)
     Long Proc Year
[1,]   61 9185 2006
[2,]   61 9185 2006
[3,]   61 9185 2006
[4,]   61 9185 2006
[5,]   61 9185 2006
[6,]   61 9185 2006
tt <-as.data.frame(tt)
tt$Long <- as.numeric(as.character(tt$Long))
tt$Year <- as.numeric(as.character(tt$Year))
glimpse(tt)
Rows: 2,792,662
Columns: 3
$ Long <dbl> 61, 61, 61, 61, 61, 61, 59, 59, 59, 59, 58, 67, 67, 67, 67, 67, 6…
$ Proc <dbl> 9185, 9185, 9185, 9185, 9185, 9185, 9185, 9185, 9185, 9185, 9185,…
$ Year <dbl> 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006,…

Ahora hago otro cruce para juntar poligonos nuevamente

names(pro)
 [1] "ACTIVA.INACTIVA"    "X1.seg.y.2..otros"  "USADA_EN"          
 [4] "CODIGO"             "POLIGONO"           "REGION"            
 [7] "NOMBRE_PROCEDENCIA" "OBSERVACIONES_1"    "PUERTO_1"          
[10] "PUERTO_2"           "PUERTO_3"           "PUERTO_4"          
[13] "GRADO_LATITUD"      "MIN_LATITUD"        "SEGUNDO_LATITUD"   
[16] "GRADO_LONGITUD"     "MIN_LONGITUD"       "SEGUNDO_LONGITUD"  
[19] "LATITUD"            "LONGITUD"          
policru<- subset(pro, select=c(4,5))
names(policru)
[1] "CODIGO"   "POLIGONO"
colnames(policru)<- c("Proc", "POLIGONO")
names(policru)
[1] "Proc"     "POLIGONO"
dftalla5 <- merge(policru, tt, by="Proc")
head(dftalla5)
  Proc POLIGONO Long Year
1    0        0   75 2010
2    0        0   75 2010
3    0        0   63 1997
4    0        0   64 1997
5    0        0   64 1997
6    0        0   98 2009

Comparaciòn de serie historica de las series

Un boxplot total de tallas medias por años y por poligono

Boxplot por todos los años

n <-ggplot(data=dftalla5, aes(x=Year, y=Long, group=Year, fill=Year)) +
    geom_boxplot( show.legend = FALSE, outlier.size = 0.05) +
    scale_fill_viridis_c(alpha=0.6) +
    #geom_jitter(size=0.4, alpha=0.2) +
    facet_wrap(.~POLIGONO, ncol = 3)+
    theme_bw() +
    geom_hline(yintercept = 65, color = "red")+
    scale_x_continuous(breaks = seq(from = 1996, to = 2022, by = 1))+
    theme(axis.text.x = element_text(angle = 90, hjust = 2))+
    ggtitle('Boxplot de tallas medias de Loco congl 2') +
    ylab('Longitud (mm.)')+
    xlab('Años')+
    coord_flip()
n

Ahora voy a sacar tallas medias por año y por POLIGONO para graficar.

meanproc <-dftalla5 %>% 
  group_by(POLIGONO, Year) %>%
  summarise(avg=mean(Long))
head(meanproc)
# A tibble: 6 × 3
# Groups:   POLIGONO [1]
  POLIGONO  Year   avg
     <int> <dbl> <dbl>
1        0  1996  70.9
2        0  1997  76.2
3        0  2001  87.6
4        0  2002  94.8
5        0  2003  98.1
6        0  2004  99.9
dim(meanproc)
[1] 283   3
glimpse(meanproc)
Rows: 283
Columns: 3
Groups: POLIGONO [13]
$ POLIGONO <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1…
$ Year     <dbl> 1996, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2…
$ avg      <dbl> 70.88372, 76.21957, 87.56881, 94.83152, 98.13441, 99.87218, 9…
mn<-ggplot(meanproc, aes(Year, POLIGONO, size=avg, fill= avg))+
    geom_point(alpha=0.5, shape=21, show.legend = T) +
    scale_size(range = c(-4,10)) +
    scale_x_continuous(breaks = seq(from = 1996, to = 2022, by = 1))+
    scale_y_continuous(breaks = seq(from = 1, to = 13, by = 1))+
    guides(fill = guide_legend(reverse=F))+
    theme_bw()+ 
    ylab("") +
    xlab("") +
    coord_flip()
mn

Ahora voy a sacar tallas medias por año y por POLIGONO IFOP para graficar.

meanpifop <-dftalla5 %>% 
  group_by(POLIGONO, Year) %>%
  summarise(avg=mean(Long))
head(meanpifop)
# A tibble: 6 × 3
# Groups:   POLIGONO [1]
  POLIGONO  Year   avg
     <int> <dbl> <dbl>
1        0  1996  70.9
2        0  1997  76.2
3        0  2001  87.6
4        0  2002  94.8
5        0  2003  98.1
6        0  2004  99.9
dim(meanpifop)
[1] 283   3
glimpse(meanpifop)
Rows: 283
Columns: 3
Groups: POLIGONO [13]
$ POLIGONO <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1…
$ Year     <dbl> 1996, 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2…
$ avg      <dbl> 70.88372, 76.21957, 87.56881, 94.83152, 98.13441, 99.87218, 9…
mnif <- ggplot(meanpifop, 
               aes(Year, POLIGONO, size=avg, fill= avg))+
    geom_point(alpha=0.5, shape=21, show.legend = T) +
    scale_size(range = c(-4,8)) +
    scale_x_discrete(breaks = seq(from = 1996, 
                                  to = 2022, by = 2))+
    scale_y_discrete(breaks = seq(from = 1, to = 13, 
                                  by = 1))+
    guides(fill = guide_legend(reverse=F))+
    theme_bw()+ 
    ylab("") +
    xlab("") +
    coord_flip()
mnif

Prepara los vectores para sumar a los .dat del año 2020

Un plot a traves de los años de las estructuras de tallas con la linea roja de TMEL (Talla Minima Extracción Legal) de 65 mm de diametro de testa.

Zona X Norte

tallaxnorte <- dftalla5 %>% 
  filter(POLIGONO==1 | POLIGONO ==2)  %>% 
  filter(Year==2022)
tallaxnorte$cat_long <- as.numeric(as.character(cut(x = tallaxnorte$Long, breaks = seq(40,138,2), 
                                                  labels = seq(40,136,2), right = FALSE)))
ttn <- table(tallaxnorte$Year, tallaxnorte$cat_long)

tail(ttn, 10)

# A su vez puedo generar el archivo por separado
write.csv(ttn, "tallanorte22.csv", sep = ",", row.names = TRUE)

Zona X Sur

tallaxsur <- dftalla5 %>% 
  filter(POLIGONO == 4 | POLIGONO == 5 |
                     POLIGONO == 6 | POLIGONO == 13)  %>% 
  filter(Year==2022)
tallaxsur$cat_long <- as.numeric(as.character(cut(x = tallaxsur$Long, breaks = seq(40,138,2), 
                                                  labels = seq(40,136,2), right = FALSE)))
tts <- table(tallaxsur$Year, tallaxsur$cat_long)

tail(tts, 10)

# A su vez puedo generar el archivo por separado
write.csv(tts, "tallasur22.csv", sep = ",", row.names = TRUE)

Zona XI

tallaxi <- dftalla5 %>% 
  filter(POLIGONO == 7 |
                   POLIGONO == 8 |
                   POLIGONO == 9|
                   POLIGONO == 10 |
                   POLIGONO == 11|
                   POLIGONO == 12)  %>% 
  filter(Year==2022)
tallaxi$cat_long <- as.numeric(as.character(cut(x = tallaxi$Long, breaks = seq(40,138,2), 
                                                  labels = seq(40,136,2), right = FALSE)))
ttxi <- table(tallaxi$Year, tallaxi$cat_long)

ttxi

# A su vez puedo generar el archivo por separado
write.csv(ttxi, "tallaxi22.csv", sep = ",", row.names = TRUE)

Mapa de tallas medias

names(pro)
 [1] "ACTIVA.INACTIVA"    "X1.seg.y.2..otros"  "USADA_EN"          
 [4] "CODIGO"             "POLIGONO"           "REGION"            
 [7] "NOMBRE_PROCEDENCIA" "OBSERVACIONES_1"    "PUERTO_1"          
[10] "PUERTO_2"           "PUERTO_3"           "PUERTO_4"          
[13] "GRADO_LATITUD"      "MIN_LATITUD"        "SEGUNDO_LATITUD"   
[16] "GRADO_LONGITUD"     "MIN_LONGITUD"       "SEGUNDO_LONGITUD"  
[19] "LATITUD"            "LONGITUD"          
procru<- subset(pro, select=c(4,19, 20))
names(procru)
[1] "CODIGO"   "LATITUD"  "LONGITUD"
colnames(procru)<- c("PROCED", "LATITUD", "LONGITUDD")
names(procru)
[1] "PROCED"    "LATITUD"   "LONGITUDD"
tageo9622 <- merge(ta9622, procru, by="PROCED")

Tallas engrilladas

ahora prceso los datos de tallas dentro de la grilla Grid2. debo sacar los datos NA

chi <- raster::getData("GADM", country = "CHL", level = 0)
chi@data
  GID_0 NAME_0
1   CHL  Chile
#
e <- extent(-77,-72,-46,-41)
#e2 <- extent(-70,-35,-68,-60)
rc <- crop(chi, e)
proj4string(rc) <- CRS("+init=epsg:4326")
rc3 <- st_as_sf(rc) # par dejarlo en formato geom_sf

# Saco bathymetria
bat <- getNOAA.bathy(-77,-72,-46,-41, res = 1)
Grid2<- rc3 %>% #rc3 es el plot base original linea 487
  sf::st_make_grid(cellsize = c(0.125,0.125)) %>% # para que quede cuadrada
  sf::st_cast("MULTIPOLYGON") %>%
  sf::st_sf() %>% # objeto en spatial feature
  dplyr::mutate(cellid = row_number()) 

# la pruebo

ggplot() +
  geom_contour(data = bat,
               aes(x=x, y=y, z=z),
               breaks=c(0, -10, -20, -50, -100, -200, -1000),
               linewidth=0.2,
               colour="grey")+
  geom_sf(data = Grid2, 
          color="grey",  
          fill=NA, 
          size=0.3) +
  geom_sf(data = rc3,  fill=NA)+
  theme_minimal()

#transformar los datos en un sf object. Aqui ojo por q no cambié el nombre de las tallas =LONGITUD

df3 <- st_as_sf(tageo9622, coords = c("LONGITUDD", "LATITUD"),  
                  crs = 4326) 
# join data to grid; make sure to join points to grid
# the first object drives the output geometry
result2 <- Grid2 %>%
  st_join(df3) %>% 
  group_by(cellid, ANO_ARR) %>% 
  summarize(meanL = mean(LONGITUD)) %>% 
  filter(! is.na(meanL))
ma <- ggplot() +
  geom_sf(data=result2,  aes(fill = cut(meanL,
                                        breaks = seq(40, 120, by = 10))),
          color=NA) +
  geom_contour(data = bat,
               aes(x=x, y=y, z=z),
               breaks=c(0, -10, -20, -50, -100, -200, -1000),
               linewidth=0.2,
               colour="grey")+
  geom_sf(data = rc3,  fill=NA)+
   scale_fill_brewer(type = "qual",
                    labels =  seq(40, 100, by = 10), # if you must
                    palette = "Spectral",
                    name = "Tallas Medias") +
  theme_bw()+
  coord_sf(crs = 4326)+
  labs(x = "Longitude", y = "Latitude")
ma