Plotting functions

Function to generate volcano plots

plot_volcano <- function(df, column_with_log2FC, column_with_pval, log2FC.threshold, pval.threshold){
  
  #Generate label for the plot
  
  significant_points <- df %>% 
    select(FeatureID, {{column_with_log2FC}}, {{column_with_pval}}) %>% 
    filter(abs({{column_with_log2FC}}) > log2FC.threshold,
           -log10({{column_with_pval}}) > -log10(0.05)) %>% 
    pull(FeatureID)
  
  plot <- df %>%
    mutate(color4plot = case_when(FeatureID %in% significant_points & {{column_with_log2FC}} > 0 ~ 'upregulated',
                                  FeatureID %in% significant_points & {{column_with_log2FC}} < 0 ~ 'downregulated',
                                  TRUE ~ 'non-significant')) %>% 
    ggplot(aes(x = {{column_with_log2FC}},
               y = -log10({{column_with_pval}}))) +
    geom_point(aes(color = color4plot)) +
    scale_color_manual(values = c("upregulated" = 'red', 'non-significant' = 'gray', 'downregulated' = 'blue')) +
    geom_vline(xintercept = c(-{{log2FC.threshold}}, {{log2FC.threshold}}),
               linetype = 'dotted',
               size = 1,
               color = 'blue') +
    geom_hline(yintercept = -log10({{pval.threshold}}),
               linetype = 'dotted',
               size = 1,
               color = 'blue') +
    theme_bw() +
    labs(title = 'Volcano plot',
         x = expression("Log"[2]*" Fold Change"),
         y = expression("-Log"[10]*" pvalue")) +
    theme(plot.title = element_text(hjust = 0.5,
                                    face = 'bold'),
          plot.subtitle = element_text(hjust = 0.5,
                                       face = 'bold'),
          legend.title = element_blank())
  
  return(plot)
}
Ayala-Ortiz, Christian O, Jacob W Farriester, Carrie J Pratt, Anna K Goldkamp, Jessica Matts, W Wyatt Hoback, John E Gustafson, and Darren E Hagen. 2021. “Effect of Food Source Availability in the Salivary Gland Transcriptome of the Unique Burying Beetle Nicrophorus Pustulatus (Coleoptera: Silphidae).” Plos One 16 (9): e0255660.
Bray, Nicolas L, Harold Pimentel, Páll Melsted, and Lior Pachter. 2016. “Near-Optimal Probabilistic RNA-Seq Quantification.” Nature Biotechnology 34 (5): 525–27.
Love, Michael I, Wolfgang Huber, and Simon Anders. 2014. “Moderated Estimation of Fold Change and Dispersion for RNA-Seq Data with DESeq2.” Genome Biology 15 (12): 1–21.