if(!require(pacman))
  install.packages("pacman")
## Loading required package: pacman
library("pacman")
p_load("vroom",
       "ggplot2",
       "ggrepel",
       "dplyr",
       "tidyr",
       "plotly")
Datos_melting <- vroom(file = "https://raw.githubusercontent.com/ManuelLaraMVZ/resultados_PCR_practica/refs/heads/main/Disoci_ejemplo1.csv")
## Rows: 55 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (6): Temperature, A1, B1, C1, D1, E1
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Datos_melting
## # A tibble: 55 × 6
##    Temperature    A1    B1    C1    D1     E1
##          <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>
##  1        65    1.96  1.98  2.11  2.11 0.0869
##  2        65.5  1.94  1.99  2.06  2.08 0.0834
##  3        66    1.89  1.93  2.03  2.07 0.0932
##  4        66.5  1.86  1.89  1.98  2.02 0.0676
##  5        67    1.82  1.89  1.94  1.98 0.0738
##  6        67.5  1.77  1.83  1.90  1.93 0.0766
##  7        68    1.74  1.78  1.86  1.87 0.0686
##  8        68.5  1.70  1.76  1.79  1.84 0.0689
##  9        69    1.65  1.71  1.79  1.80 0.0665
## 10        69.5  1.60  1.64  1.75  1.76 0.0744
## # ℹ 45 more rows
Datos_melting2 <- Datos_melting %>% 
  mutate(Temperatura = Temperature, JFRW = A1, JRL = B1, PPOF = C1, JTT = D1, AALE = E1) %>% 
  select(-1:-6)

Datos_melting2
## # A tibble: 55 × 6
##    Temperatura  JFRW   JRL  PPOF   JTT   AALE
##          <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>
##  1        65    1.96  1.98  2.11  2.11 0.0869
##  2        65.5  1.94  1.99  2.06  2.08 0.0834
##  3        66    1.89  1.93  2.03  2.07 0.0932
##  4        66.5  1.86  1.89  1.98  2.02 0.0676
##  5        67    1.82  1.89  1.94  1.98 0.0738
##  6        67.5  1.77  1.83  1.90  1.93 0.0766
##  7        68    1.74  1.78  1.86  1.87 0.0686
##  8        68.5  1.70  1.76  1.79  1.84 0.0689
##  9        69    1.65  1.71  1.79  1.80 0.0665
## 10        69.5  1.60  1.64  1.75  1.76 0.0744
## # ℹ 45 more rows
Datos_melting3 <- Datos_melting2 %>% 
  pivot_longer(cols = -Temperatura,
               names_to = "Muestras",
               values_to = "Fluorescencia")
Datos_melting3
## # A tibble: 275 × 3
##    Temperatura Muestras Fluorescencia
##          <dbl> <chr>            <dbl>
##  1        65   JFRW            1.96  
##  2        65   JRL             1.98  
##  3        65   PPOF            2.11  
##  4        65   JTT             2.11  
##  5        65   AALE            0.0869
##  6        65.5 JFRW            1.94  
##  7        65.5 JRL             1.99  
##  8        65.5 PPOF            2.06  
##  9        65.5 JTT             2.08  
## 10        65.5 AALE            0.0834
## # ℹ 265 more rows
Grafica_melting <- ggplot(Datos_melting3,
                          mapping = aes(x = Temperatura,
                                        y = Fluorescencia,
                                        color = Muestras))+
  geom_line (size = 1.5)+
  theme_classic() +
  labs(title = "Curvas de disociación RT-qPCR",
       subtitle = "Práctica 1, \nEquipo: JTT",
       caption = "Todas las muestras",
       x = "Temperatura (° C)",
       y = "Fluorescencia (u.a.)") +
  theme(
    axis.line = element_line(size = 1.2, color = "black"),
    axis.title = element_text(face = "bold"),              
    axis.text = element_text(face = "bold"),
    legend.title = element_text(face = "bold"),
    legend.text = element_text(face = "bold")) +
  scale_x_continuous(breaks = seq(min(Datos_melting3$Temperatura), max(Datos_melting3$Temperatura), by = 5))+
  scale_y_continuous(labels = scales::number_format(accuracy = 0.05),
                     breaks = seq(min(Datos_melting3$Fluorescencia), max(Datos_melting3$Fluorescencia), by = 0.25))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Grafica_melting

Grafica_melting_equipo <- ggplot(Datos_melting2,
                                 aes(x = Temperatura,
                                     y = JTT))+
  geom_line(size = 1.5, color = "#6287f1")+
   theme_classic() +
  labs(title = "Curvas de disociación RT-qPCR",
       subtitle = "Práctica 1, \nEquipo: JTT",
       caption = "Todas las muestras",
       x = "Temperatura (° C)",
       y = "Fluorescencia (u.a.)") +
  theme(
    axis.line = element_line(size = 1.2, color = "black"),
    axis.title = element_text(face = "bold"),              
    axis.text = element_text(face = "bold"),
    legend.title = element_text(face = "bold"),
    legend.text = element_text(face = "bold")) +
  scale_x_continuous(breaks = seq(min(Datos_melting3$Temperatura), max(Datos_melting3$Temperatura), by = 5))+
  scale_y_continuous(labels = scales::number_format(accuracy = 0.05),
                     breaks = seq(min(Datos_melting3$Fluorescencia), max(Datos_melting3$Fluorescencia), by = 0.25))

Grafica_melting_equipo

Derivadas <- Datos_melting2 %>% 
  mutate(across(JFRW:AALE, ~ -c(NA, diff(.x) / diff(Datos_melting2$Temperatura)), .names = "d_{.col}")) %>% 
  select(-2: -6) %>% 
  slice(-1)

Derivadas
## # A tibble: 54 × 6
##    Temperatura d_JFRW    d_JRL   d_PPOF  d_JTT    d_AALE
##          <dbl>  <dbl>    <dbl>    <dbl>  <dbl>     <dbl>
##  1        65.5 0.0365 -0.0147   0.0940  0.0711  0.00703 
##  2        66   0.102   0.108    0.0614  0.0146 -0.0197  
##  3        66.5 0.0695  0.0883   0.112   0.108   0.0512  
##  4        67   0.0652  0.00898  0.0646  0.0772 -0.0123  
##  5        67.5 0.109   0.114    0.0903  0.0923 -0.00567 
##  6        68   0.0666  0.0994   0.0707  0.122   0.0162  
##  7        68.5 0.0774  0.0335   0.139   0.0558 -0.000754
##  8        69   0.0860  0.0979  -0.00165 0.0850  0.00488 
##  9        69.5 0.111   0.146    0.0908  0.0844 -0.0159  
## 10        70   0.0582  0.0279   0.130   0.0864  0.00967 
## # ℹ 44 more rows
Derivadas2 <- Derivadas %>% 
  pivot_longer(cols = -Temperatura,
               names_to = "Muestras",
               values_to = "Derivadas")
Derivadas2
## # A tibble: 270 × 3
##    Temperatura Muestras Derivadas
##          <dbl> <chr>        <dbl>
##  1        65.5 d_JFRW     0.0365 
##  2        65.5 d_JRL     -0.0147 
##  3        65.5 d_PPOF     0.0940 
##  4        65.5 d_JTT      0.0711 
##  5        65.5 d_AALE     0.00703
##  6        66   d_JFRW     0.102  
##  7        66   d_JRL      0.108  
##  8        66   d_PPOF     0.0614 
##  9        66   d_JTT      0.0146 
## 10        66   d_AALE    -0.0197 
## # ℹ 260 more rows
Grafica_melting <- ggplot(Derivadas2,
                          aes(x = Temperatura,
                              y = Derivadas,
                              color = Muestras)) +
  geom_line() +
  theme_classic() +
  labs(title = "Curvas de disociación RT-qPCR",
       subtitle = "Práctica 1, \nEquipo: JTT",
       caption = "Todas las muestras",
       x = "Temperatura (°C)",
       y = expression("-" * Delta * "F/" * Delta * "T")) +
  theme(
    axis.line = element_line(size = 1.2, color = "black"),
    axis.title = element_text(face = "bold"),              
    axis.text = element_text(face = "bold"),
    legend.title = element_text(face = "bold"),
    legend.text = element_text(face = "bold")
  ) +
  scale_x_continuous(breaks = seq(min(Derivadas2$Temperatura), max(Derivadas2$Temperatura), by = 5)) +
  scale_y_continuous(labels = scales::number_format(accuracy = 0.05),
                     breaks = seq(min(Derivadas2$Derivadas), max(Derivadas2$Derivadas), by = 0.25))

Grafica_melting

library(plotly)

Curva_3D <- plot_ly(Derivadas2,
                    x = ~Temperatura,
                    y = ~Derivadas,
                    z = ~Muestras,
                    color = ~factor(Muestras),
                    type = "scatter3d",
                    mode = "lines",
                    line = list(width = 6)) 


Curva_3D
Grafica_derivada_equipo <- ggplot(Derivadas,
                                  aes(x = Temperatura,
                                      y = d_JTT))+
  geom_line(size = 1.5, color = "#6287f1" )+
  theme_classic() +
  labs(title = "Curvas de la derivada de disociación RT-qPCR",
       subtitle = "Tejido: Corazón",
       caption = "Caption: Julieta Tavera",
       x = "Temperatura (° C)",
       y = expression("-" * Delta * "F/" * Delta * "T")) +
  theme(
    axis.line = element_line(size = 1.2, color = "black"),
    axis.title = element_text(face = "bold"),              
    axis.text = element_text(face = "bold"),
    legend.title = element_text(face = "bold"),
    legend.text = element_text(face = "bold")) +
  scale_x_continuous(breaks = seq(min(Derivadas$Temperatura), max(Derivadas$Temperatura), by = 5))+
  scale_y_continuous(labels = scales::number_format(accuracy = 0.05),
                     breaks = seq(min(Derivadas$d_JTT), max(Derivadas$d_JTT), by = 0.05))

Grafica_derivada_equipo

Peak_Tm <- max(Derivadas$d_JTT)

Peak_Tm
## [1] 0.738565
Tm <- Derivadas %>% 
  filter(d_JTT == Peak_Tm) %>% 
  select(1, d_JTT)

Tm
## # A tibble: 1 × 2
##   Temperatura d_JTT
##         <dbl> <dbl>
## 1          82 0.739
Etiqueta <- Grafica_derivada_equipo +
  geom_vline (xintercept = Tm$Temperatura, color = "#900C3F", size = 1, linetype = "dashed")+
  geom_label_repel(data = Tm,
                   aes(x = Temperatura,
                       y = d_JTT,
                       label = paste("Tm = ", round(Temperatura, 2), "° C")),
                   nudge_x = 3, 
                   max.overlaps = 100,
                   fill = "#9eb7fc",
                   color = "black")
Etiqueta