# Instalar paquetes si no están disponibles
if (!requireNamespace("pacman", quietly = TRUE)) install.packages("pacman")
library("pacman")
## Warning: package 'pacman' was built under R version 4.2.3
# Cargar paquetes
p_load("pheatmap",
"RColorBrewer",
"ggplot2",
"dplyr",
"FactoMineR", # Para PCA
"factoextra") # Para visualización de PCA
library(vroom) # Para leer datos rápidamente
Datos_qPCR <- vroom("https://raw.githubusercontent.com/ManuelLaraMVZ/Heatmaps/refs/heads/main/Ejemplo%206x4.csv")
## Rows: 8 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Gene, Condition
## dbl (6): Control_1, Control_2, Control_3, Tratamiento_1, Tratamiento_2, Trat...
##
## ℹ 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_qPCR
## # A tibble: 8 × 8
## Gene Condition Control_1 Control_2 Control_3 Tratamiento_1 Tratamie…¹ Trata…²
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Gen_1 Target 31.0 29.3 29.3 26.7 25.8 27.2
## 2 Gen_2 Target 30.1 29.1 29.5 28.8 26.3 27.2
## 3 Gen_3 Target 30.3 29.0 29.9 27.6 26.6 28.0
## 4 Gen_4 Target 25.4 26.5 25.5 32.1 28.7 30.8
## 5 Gen_5 Target 29.2 28.5 26.8 29.3 30.3 29.8
## 6 Gen_6 Target 27.9 27.9 27.8 30.6 29.9 29.7
## 7 Ref_1 Reference 26.0 24.9 25.5 24.6 25.6 25.2
## 8 Ref_2 Reference 25.9 24.1 25.0 24.9 25.1 25.9
## # … with abbreviated variable names ¹Tratamiento_2, ²Tratamiento_3
Ref_gen_prom <- Datos_qPCR %>%
filter(Condition == "Reference") %>%
select(-1,-2) %>%
summarise(across(everything(), mean, na.rm = TRUE))
Ref_gen_prom
## # A tibble: 1 × 6
## Control_1 Control_2 Control_3 Tratamiento_1 Tratamiento_2 Tratamiento_3
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 25.9 24.5 25.2 24.8 25.3 25.6
DCt <- Datos_qPCR %>%
filter(Condition == "Target") %>%
select(-2) %>% # Mantener Gene
mutate(across(-1, ~ -(. - Ref_gen_prom[[cur_column()]][[1]]), .names = "DCt_{.col}")) %>%
select(Gene, starts_with("DCt_"))
head(DCt)
## # A tibble: 6 × 7
## Gene DCt_Control_1 DCt_Control_2 DCt_Control_3 DCt_Tratamie…¹ DCt_T…² DCt_T…³
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Gen_1 -5.06 -4.81 -4.07 -1.93 -0.466 -1.60
## 2 Gen_2 -4.18 -4.57 -4.27 -4.06 -1.03 -1.66
## 3 Gen_3 -4.39 -4.51 -4.64 -2.78 -1.31 -2.40
## 4 Gen_4 0.543 -1.99 -0.295 -7.32 -3.40 -5.21
## 5 Gen_5 -3.25 -4.05 -1.52 -4.51 -4.94 -4.17
## 6 Gen_6 -1.95 -3.39 -2.58 -5.77 -4.62 -4.11
## # … with abbreviated variable names ¹DCt_Tratamiento_1, ²DCt_Tratamiento_2,
## # ³DCt_Tratamiento_3
library(tibble) # Para column_to_rownames
miRNA_scaled <- DCt %>%
column_to_rownames(var = "Gene") %>%
scale(center = TRUE, scale = TRUE) %>%
as.data.frame()
# Escalar para que los valores estén entre -2 y 2
miRNA_scaled <- miRNA_scaled * 2
head(miRNA_scaled)
## DCt_Control_1 DCt_Control_2 DCt_Control_3 DCt_Tratamiento_1
## Gen_1 -1.955055 -1.7446042 -1.354102 2.5112385
## Gen_2 -1.093947 -1.2935813 -1.581784 0.3403755
## Gen_3 -1.301984 -1.1783013 -2.009708 1.6466580
## Gen_4 3.481844 3.5965362 2.997433 -2.9804692
## Gen_5 -0.191901 -0.3094469 1.583660 -0.1141984
## Gen_6 1.061043 0.9293975 0.364501 -1.4036044
## DCt_Tratamiento_2 DCt_Tratamiento_3
## Gen_1 2.227612 2.106222
## Gen_2 1.642266 2.034587
## Gen_3 1.354608 1.052326
## Gen_4 -0.791582 -2.670643
## Gen_5 -2.380496 -1.300499
## Gen_6 -2.052409 -1.221992
color_palette <- colorRampPalette(c("#150f44", "white", "#fbe104" ))(100) #e45e58"
pheatmap(miRNA_scaled,
color = color_palette,
cluster_rows = TRUE,
cluster_cols = TRUE,
show_rownames = T, #Nombres genes
show_colnames = TRUE,
fontsize_row = 8,
fontsize_col = 14,
border_color = "black",
main = "Heatmap de expresión de miRNAs",
fontface_row = "bold")
pca_result <- prcomp(t(miRNA_scaled), center = TRUE, scale. = TRUE) # PCA sobre datos transpuestos
summary(pca_result)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6
## Standard deviation 2.277 0.7830 0.32267 0.31401 0.01889 1.062e-16
## Proportion of Variance 0.864 0.1022 0.01735 0.01643 0.00006 0.000e+00
## Cumulative Proportion 0.864 0.9661 0.98351 0.99994 1.00000 1.000e+00
fviz_eig(pca_result, addlabels = TRUE, barfill = "steelblue", barcolor = "black")
# Crear un dataframe con los scores de las muestras en los primeros dos componentes
pca_df <- as.data.frame(pca_result$x)
pca_df$Sample <- rownames(pca_df) # Agregar nombres de muestra
# Graficar los dos primeros componentes principales con ejes en el origen
pca_plot <- ggplot(pca_df, aes(x = PC1, y = PC2, label = Sample)) +
geom_point(size = 4, aes(color = Sample)) +
geom_text(vjust = -0.5, size = 3) +
geom_hline(yintercept = 0, linetype = "solid", color = "black", linewidth= 1.5) +
geom_vline(xintercept = 0, linetype = "solid", color = "black", linewidth= 1.5) +
labs(title = "PCA de expresión de miRNAs", x = "PC1", y = "PC2") +
theme_minimal()
pca_plot
¡Ahora el análisis incluye PCA para una mejor interpretación de los datos! 🎉
pca_result_genes <- prcomp(miRNA_scaled, center = TRUE, scale. = TRUE) # PCA sobre los genes
summary(pca_result_genes)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6
## Standard deviation 2.2857 0.74666 0.39132 0.24689 0.06404 1.013e-16
## Proportion of Variance 0.8707 0.09292 0.02552 0.01016 0.00068 0.000e+00
## Cumulative Proportion 0.8707 0.96364 0.98916 0.99932 1.00000 1.000e+00
fviz_eig(pca_result_genes, addlabels = TRUE, barfill = "steelblue", barcolor = "black")
## 🎯 Gráfico de PCA (biplot de genes) con nombres
# Crear un dataframe con los scores de los genes en los primeros dos componentes
pca_df_genes <- as.data.frame(pca_result_genes$x)
pca_df_genes$Gene <- rownames(pca_df_genes) # Agregar nombres de genes
# Graficar los genes en el espacio PCA con etiquetas
pca_plot_genes <- ggplot(pca_df_genes, aes(x = PC1, y = PC2, label = Gene)) +
geom_point(size = 4, aes(color = Gene), show.legend = FALSE) + # Ocultar leyenda de genes
geom_text(vjust = -0.5, size = 3) + # Mostrar nombres de genes
geom_hline(yintercept = 0, linetype = "solid", color = "black", linewidth= 1.5) +
geom_vline(xintercept = 0, linetype = "solid", color = "black", linewidth= 1.5) +
labs(title = "PCA de expresión de miRNAs (Genes)", x = "PC1", y = "PC2") +
theme_minimal()
pca_plot_genes