Estadística descriptiva

library(readxl)
library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.3.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(egg)
Loading required package: gridExtra

Attaching package: 'gridExtra'

The following object is masked from 'package:dplyr':

    combine
library(tidyverse)
library(ggplot2)
library(ggpmisc)
Warning: package 'ggpmisc' was built under R version 4.3.3
Loading required package: ggpp
Warning: package 'ggpp' was built under R version 4.3.3
Registered S3 methods overwritten by 'ggpp':
  method                  from   
  heightDetails.titleGrob ggplot2
  widthDetails.titleGrob  ggplot2

Attaching package: 'ggpp'

The following object is masked from 'package:ggplot2':

    annotate

Registered S3 method overwritten by 'ggpmisc':
  method                  from   
  as.character.polynomial polynom
#install.packages("broom")
library(broom)
Warning: package 'broom' was built under R version 4.3.3
library(ggplot2)
library(patchwork)
library(egg)
library(ggpubr)

Attaching package: 'ggpubr'

The following objects are masked from 'package:ggpp':

    as_npc, as_npcx, as_npcy

The following object is masked from 'package:egg':

    ggarrange
library(readxl)
library(tidyverse)
library(egg)
library(tidyverse)
library(ggplot2)


dat_clean=read.csv("dat_clean_modified_zscore_anchoveta.csv")


dat_clean$group <- factor(dat_clean$Subclass_n,      # Reordering group factor levels
                         levels = c("Moda 3.5 cm", "Moda 4 cm", "Moda 5 cm", "Moda 7.5 cm","Moda 10.5 cm", "Moda 11 cm", "Moda 12 cm","Moda 12.5 cm","Moda 13 cm","Moda 13.5 cm"),labels = c("3.5", "4", "5", "7.5","10.5","11","12","12.5","13","13.5"))

dat_clean$Banda <- factor(dat_clean$Banda,
  levels = c("35-45","45-90","90-170","170-260"),labels = c("35-45","45-90","90-170","170-260"))
Postlarva1 <- dat_clean %>%
  filter(group %in% c("3.5"))%>%
  mutate(Segmentacion=rep("3.5"))


Postlarva2 <- dat_clean %>%
  filter(group %in% c("4", "5"))%>%
  mutate(Segmentacion=rep("4-5"))

Juvenil <- dat_clean %>%
  filter(group %in% c("7.5", "10.5", "11"))%>%
  mutate(Segmentacion=rep("7.5-11"))

Adulto1 <- dat_clean %>%
  filter(group %in% c("12", "12.5", "13","13.5"))%>%
  mutate(Segmentacion=rep("12-13.5"))




dat_clean2=rbind(Postlarva1,Postlarva2,Juvenil,Adulto1)


dat_clean2$Segmentacion <- factor(dat_clean2$Segmentacion,
  levels = c("3.5","4-5","7.5-11","12-13.5"),labels = c("3.5","4 a 5","7.5 a 11","12 a 13.5"))

dat_clean2$Banda <- factor(dat_clean2$Banda,
  levels = c("35-45","45-90","90-170","170-260"),labels = c("35-45","45-90","90-170","170-260"))


dat=dat_clean2
descriptiva_segmentacion=dat_clean2 %>% 
  group_by(Segmentacion) %>%
  get_summary_stats(type = "common")  %>%
  dplyr::filter(variable=="Value")

descriptiva_segmentacion
# A tibble: 4 × 11
  Segmentacion variable     n   min   max median   iqr  mean    sd    se    ci
  <fct>        <fct>    <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 3.5          Value    12220 -77.7 -32.2  -54.8  9.18 -55.8  6.84 0.062 0.121
2 4 a 5        Value    64503 -81.1 -33.4  -59.3  8.61 -59.1  6.68 0.026 0.052
3 7.5 a 11     Value    39817 -82.8 -25.3  -57.8 10.3  -57.2  7.82 0.039 0.077
4 12 a 13.5    Value    36464 -80.1 -19.0  -49.7 12.3  -50.2  8.92 0.047 0.092
descriptiva_segmentacion_banda=dat_clean2 %>% 
  group_by(Segmentacion,Banda) %>%
  get_summary_stats(type = "common")  %>%
  dplyr::filter(variable=="Value")

descriptiva_segmentacion_banda
# A tibble: 16 × 12
   Banda  Segmentacion variable     n   min   max median   iqr  mean    sd    se
   <fct>  <fct>        <fct>    <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
 1 35-45  3.5          Value      527 -77.6 -37.0  -62.0 18.7  -58.8 10.1  0.441
 2 45-90  3.5          Value     2063 -77.7 -32.2  -62.7 17.5  -59.9  9.6  0.211
 3 90-170 3.5          Value     4140 -74.4 -34.2  -55.4 10.0  -55.5  6.43 0.1  
 4 170-2… 3.5          Value     5490 -76.8 -41.2  -54.0  5.54 -54.2  4.32 0.058
 5 35-45  4 a 5        Value     2892 -71.0 -35.8  -52.8  9.15 -53.1  6.48 0.121
 6 45-90  4 a 5        Value    11051 -76.8 -33.4  -57.3  8.66 -57.1  6.56 0.062
 7 90-170 4 a 5        Value    21169 -81   -33.9  -59.0  8.84 -58.9  6.66 0.046
 8 170-2… 4 a 5        Value    29391 -81.1 -37.9  -60.5  7.88 -60.7  6.19 0.036
 9 35-45  7.5 a 11     Value     1845 -81.6 -28.7  -48.5  9.65 -49.0  7.7  0.179
10 45-90  7.5 a 11     Value     7110 -82.8 -25.3  -55.4  9.98 -55.0  7.79 0.092
11 90-170 7.5 a 11     Value    11952 -77.4 -25.9  -57.2  9.63 -56.7  7.29 0.067
12 170-2… 7.5 a 11     Value    18910 -80.2 -29.1  -59.6  9.63 -59.0  7.40 0.054
13 35-45  12 a 13.5    Value     2311 -80.1 -26.5  -46.3  9.48 -46.1  7.31 0.152
14 45-90  12 a 13.5    Value     5051 -80.1 -19.3  -47.8 12.2  -49.1  9.07 0.128
15 90-170 12 a 13.5    Value    15850 -74.4 -19.0  -49.8 12.0  -49.7  8.69 0.069
16 170-2… 12 a 13.5    Value    13252 -78.2 -29.4  -50.8 13.4  -52.1  8.99 0.078
# ℹ 1 more variable: ci <dbl>
library(ggplot2)

ggplot(dat)+
  geom_boxplot(alpha=1,size=0.45, aes(color=Segmentacion,y = Value, x=Segmentacion, alpha=0.5), show.legend = F)+
  theme_presentation(base_size = 12) +
  ylab("Sv (dB)")+

        scale_color_manual(name="Longitud (cm)",values =c("blue","yellow","orange","green"))+
  #scale_fill_manual(values =c("#5f5f5f","#000080","#008000","#ff00bf","#a6533c"))+ 
 #scale_fill_viridis_d(option = "C",direction = -1,begin = 0.30,end = 1,alpha = 0.5)+
  labs(x="Categoría")+
  scale_x_discrete()+
  theme(legend.position = "top")+
  theme(panel.grid.major.y = element_line(color = "gray", linetype = "dashed"))

ggplot(dat)+
  geom_boxplot(alpha=1,size=0.45, aes(color=Segmentacion,y = Value, x=Segmentacion, alpha=0.5), show.legend = F)+
  theme_presentation(base_size = 12) +
  ylab("Sv (dB)")+
      scale_color_manual(name="Longitud (cm)",values =c("blue","yellow","orange","green"))+
  #scale_fill_manual(values =c("#5f5f5f","#000080","#008000","#ff00bf","#a6533c"))+ 
 #scale_fill_viridis_d(option = "C",direction = -1,begin = 0.30,end = 1,alpha = 0.5)+
  labs(x="Categoría")+
  scale_x_discrete()+
  theme(legend.position = "top")+
  theme(panel.grid.major.y = element_line(color = "gray", linetype = "dashed"))+
  facet_wrap(~Banda)

Figuras

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

ggplot(dat, aes(y = Value, x=as.numeric(Frequency)))+
  geom_line(alpha=0.5, aes(color=Segmentacion),lwd=1, show.legend = F)+
  geom_smooth(size=1,method = "gam", color="black", show.legend = F)+
  theme_presentation(base_size = 12) +
  xlab("Frecuencia (kHz)") +
  ylab("Sv (dB)")+
  scale_x_continuous(breaks = c(38,70,90,120,170,200,260))+
  geom_vline(xintercept = c(38,45,90,170,260),linetype = c("dashed"),color="gray")+
  geom_hline(yintercept = c(-80,-70,-60,-50,-40,-30,-20),linetype = c("dashed"),color="gray")+
  
      scale_color_manual(name="Longitud (cm)",values =c("blue","yellow","orange","green"))+
  
theme(legend.title=element_blank())+
  facet_wrap(~Segmentacion, ncol=5)

  #facet_wrap(c("Year", "Class), labeller = "label_both")


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

ggplot(dat, aes(y = Value, x=as.numeric(Frequency)))+
  #geom_line(alpha=0.5, aes(color=N_Catch_Year),lwd=3)+
  geom_smooth(size=1,method = "gam", aes(color=Segmentacion), show.legend = F)+

  theme_presentation(base_size = 12) +
  xlab("Frecuencia (kHz)") +
  ylab("Sv (dB)")+
  
  scale_x_continuous(breaks = c(38,70,90,120,170,200,260))+
  geom_vline(xintercept = c(38,45,90,170,260),linetype = c("dashed"),color="gray")+
  geom_hline(yintercept = c(-80,-70,-60,-50,-40,-30,-20),linetype = c("dashed"),color="gray")+
  
      scale_color_manual(name="Longitud (cm)",values =c("blue","yellow","orange","green"))+
  theme(legend.title=element_blank())+
  facet_wrap(~Segmentacion, ncol=5)

  #facet_wrap(c("Year", "Class), labeller = "label_both")


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


ggplot(dat, aes(y = Value, x=as.numeric(Frequency)))+
  geom_line(alpha=0.5, aes(color=Segmentacion),lwd=1, show.legend = F)+
  geom_smooth(size=1,method = "lm", color="black", show.legend = F)+
  stat_regline_equation()+
  theme_presentation(base_size = 12) +
  
  xlab("Frecuencia (kHz)") +
  ylab("Sv (dB)")+
  
  scale_x_continuous(breaks = c(38,70,90,120,170,200,260))+
  geom_vline(xintercept = c(38,45,90,170,260),linetype = c("dashed"),color="gray")+
  geom_hline(yintercept = c(-80,-70,-60,-50,-40,-30,-20),linetype = c("dashed"),color="gray")+

  
      scale_color_manual(name="Longitud (cm)",values =c("blue","yellow","orange","green"))+
  
theme(legend.title=element_blank())+
  facet_wrap(~Segmentacion,ncol=5)

  #facet_wrap(c("Year", "Class), labeller = "label_both")


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

ggplot(dat, aes(y = Value, x=as.numeric(Frequency)))+
  #geom_line(alpha=0.5, aes(color=N_Catch_Year),lwd=3)+
  geom_smooth(size=1,method = "lm", aes(color=Segmentacion),show.legend = F)+
  stat_regline_equation()+
  theme_presentation(base_size = 12) +
  xlab("Frecuencia (kHz)") +
  ylab("Sv (dB)")+
  
  scale_x_continuous(breaks = c(38,70,90,120,170,200,260))+
  geom_vline(xintercept = c(38,45,90,170,260),linetype = c("dashed"),color="gray")+
  geom_hline(yintercept = c(-80,-70,-60,-50,-40,-30,-20),linetype = c("dashed"),color="gray")+
  
      scale_color_manual(name="Longitud (cm)",values =c("blue","yellow","orange","green"))+
  #ggtitle("")+
  theme(legend.title=element_blank())+
  facet_wrap(~Segmentacion, ncol=5)

  #facet_wrap(c("Year", "Class), labeller = "label_both")

Regresión en dB

ggplot(dat, aes(y = Value, x=as.numeric(Frequency)))+
  geom_line(alpha=0.5, aes(color=Segmentacion),lwd=1.5, show.legend = F)+
  geom_smooth(size=1,method = "lm", color="black", show.legend = F)+
    xlab("Frecuencia (kHz)") +
  ylab("Sv (dB)")+
  
  scale_x_continuous(breaks = c(38,70,90,120,170,200,260))+
  geom_vline(xintercept = c(38,45,90,170,260),linetype = c("dashed"),color="gray")+
  geom_hline(yintercept = c(-80,-70,-60,-50,-40,-30,-20),linetype = c("dashed"),color="gray")+
      scale_color_manual(name="Longitud (cm)",values =c("blue","yellow","orange","green"))+
  stat_regline_equation(size=10)+
  theme_presentation() +
  facet_wrap(~Segmentacion, ncol=2)

Regresion en dominio lineal

ggplot(dat, aes(y = Value_linear, x=as.numeric(Frequency)))+
  geom_line(alpha=0.5, aes(color=Segmentacion),lwd=1.5, show.legend = F)+
  geom_smooth(size=1,method = "lm", color="black", show.legend = F)+
  
      scale_color_manual(name="Longitud (cm)",values =c("blue","yellow","orange","green"))+
  stat_regline_equation(size=7)+
  theme_presentation() +
    facet_wrap(~Segmentacion, ncol=2, scale="free")

library(seewave)
Warning: package 'seewave' was built under R version 4.3.3

Attaching package: 'seewave'
The following object is masked from 'package:lubridate':

    duration
The following object is masked from 'package:readr':

    spec
10*log10(1/100000)
[1] -50
10*log10(1.7/100000000)
[1] -77.69551
10^(-80/10)
[1] 1e-08
w=ggplot(dat, aes(y = Value, x=as.numeric(Frequency),color=Segmentacion) )+
  #geom_line(alpha=0.1, aes(color=group),lwd=1, show.legend = T)+
  geom_smooth(size=2.25,method = "lm", show.legend = T,se = T)+
  scale_y_continuous(limits = c(-65,-45))+
        scale_color_manual(name="Longitud (cm)",values =c("blue","yellow","orange","green"))+
  #stat_regline_equation()+
  theme_presentation(base_size = 14) +
  xlab("Frecuencia (kHz)") +
  ylab("Sv (dB)")+
  scale_x_continuous(breaks = c(38,70,90,120,170,200,260))+
  geom_vline(xintercept = c(38,45,90,170,260),linetype = c("dashed"),color="gray")+
  geom_hline(yintercept = c(-70,-60,-50,-40),linetype = c("dashed"),color="gray")+
  #scale_color_viridis_d(option = "C")+
  theme(legend.title=element_blank())+
   theme(panel.grid.major.y = element_line(color = "gray", linetype = "dashed"))+
       theme(panel.grid.major.x = element_line(color = "gray", linetype = "dashed"))



k=ggplot(dat, aes(y = Value, x=as.numeric(Frequency),color=Segmentacion) )+
  #geom_line(alpha=0.1, aes(color=group),lwd=1, show.legend = T)+
  geom_smooth(size=2.25,method = "gam", show.legend = F,se=T)+
        scale_color_manual(name="Longitud (cm)",values =c("blue","yellow","orange","green"))+
  #stat_regline_equation()+
  theme_presentation(base_size = 14) +
  xlab("Frecuencia (kHz)") +
  ylab("Sv (dB)")+
  scale_y_continuous(limits = c(-65,-45))+
  scale_x_continuous(breaks = c(38,70,90,120,170,200,260))+
  geom_vline(xintercept = c(38,45,90,170,260),linetype = c("dashed"),color="gray")+
  geom_hline(yintercept = c(-70,-60,-50,-40),linetype = c("dashed"),color="gray")+
  #scale_color_viridis_d(option = "C")+
  theme(legend.title=element_blank())+
   theme(panel.grid.major.y = element_line(color = "gray", linetype = "dashed"))+
     theme(panel.grid.major.x = element_line(color = "gray", linetype = "dashed"))


library(cowplot)

# Crear las gráficas w y k (código que proporcionaste)

# Obtener la leyenda de una de las gráficas (por ejemplo, w)
legend_w <- get_legend(w)

# Combinar las dos gráficas y agregar la leyenda
combined_plot <- plot_grid(
  k + theme(legend.position = "none"),  # Ocultar la leyenda de la gráfica w
  w + theme(legend.position = "none"),  # Ocultar la leyenda de la gráfica k
  legend_w,
  ncol = 3, rel_heights = c(1, 1, 1),rel_widths = c(1,1,0.35),  # Ajustar las alturas relativas
  labels = c("(a)", "(b)", ""),  # Etiquetas de enumeración
  align = "h"  # Alinear horizontalmente las partes
)

# Ajustar el tamaño de la leyenda
combined_plot <- combined_plot + theme(
  legend.text = element_text(size = 19),  # Tamaño del texto de la leyenda
  legend.title = element_text(size = 19)  # Tamaño del título de la leyenda
)

# Imprimir la figura combinada
print(combined_plot)

library(coin)
Warning: package 'coin' was built under R version 4.3.3
Loading required package: survival
oneway_test(Value ~ group, data = dat)

    Asymptotic K-Sample Fisher-Pitman Permutation Test

data:  Value by
     group (3.5, 4, 5, 7.5, 10.5, 11, 12, 12.5, 13, 13.5)
chi-squared = 34310, df = 9, p-value < 2.2e-16