---
title: "Volcano plot"
author: "Valerio Licursi"
date: "2025-06-04"
format: html
editor: visual
---

### Exercise: Creating a Volcano Plot Using `ggplot2`

#### Objective:
Learn how to create and interpret a volcano plot for visualizing differentially expressed genes (DEGs) from gene expression data using the `ggplot2` package in R.

#### Prerequisites:
- Basic knowledge of R and data manipulation
- `tidyverse` and `ggplot2` packages installed (`install.packages(c("tidyverse", "ggplot2"))`)

### Exercise Steps:

#### 1. Install and Load Necessary Packages
Ensure you have the necessary packages installed and loaded.

```{r}
library(tidyverse)
library(ggplot2)
library(ggrepel)
library(readxl)
```

#### 2. Load Gene Expression Data
For this exercise, we'll use a gene expression dataset stored in an Excel file. Typically, you would have results from a differential expression analysis.

```{r}
data <- read_excel("../_files_module_1/rnaseq.xlsx")
head(data)
```
##### Set the thresholds for *log2 Fold Change* and *pvalues*

```{r}
logfc_cutoff <- log2(1.5)
fdr_cutoff <- 0.05

# use ifelse() creating a new variable
data$significant <- ifelse(test = data$log2FC > logfc_cutoff & data$pvalue < fdr_cutoff, 
                           yes = "Upregulated", 
                           no = ifelse(data$log2FC < -logfc_cutoff & data$pvalue < fdr_cutoff, 
                                       yes = "Downregulated", 
                                       no = "Not modulated" )
                           )
head(data)
```


#### 3. Create the Volcano Plot
Use `ggplot2` to create a volcano plot. Points are colored based on their significance.

```{r}
# Create the volcano plot
ggplot(data, aes(x = log2FC, y = -log10(pvalue), color = significant)) +
  geom_point(alpha = 0.4, size = 1.5) +
  scale_color_manual(values = c("blue3", "grey", "red")) +
  theme_minimal() +
  labs(title = "Volcano Plot",
       x = "Log2 Fold Change",
       y = "-log10(p-value)",
       color = "Significant")
```

#### 4. Customize the Volcano Plot
Add more customization to enhance the plot, such as labeling the most significant points.

```{r}
# Label the most significant points
top_genes <- data %>%
  filter(adj_pvalue < 0.01) %>%
  arrange(adj_pvalue) %>%
  head(10)

ggplot(data, aes(x = log2FC, y = -log10(pvalue), color = significant)) +
  geom_point(alpha = 0.4, size = 1.5) +
  geom_text_repel(data = top_genes, aes(label = Gene_Symbol),
                  size = 3, box.padding = 0.5) +
  scale_color_manual(values = c("blue3", "grey", "red")) +
  theme_minimal() +
  labs(title = "Volcano Plot",
       x = "Log2 Fold Change",
       y = "-log10(p-value)",
       color = "Significant")
```

#### 5. Save the Plot
Save the plot to a file for use in presentations or reports.

```{r}
ggsave("volcano_plot.png", width = 8, height = 6)
```

### Additional Customizations:
- **Threshold Lines**: Add horizontal and vertical lines to indicate thresholds for significance.

```{r}
# Add threshold lines for log fold change and p-value
ggplot(data, aes(x = log2FC, y = -log10(pvalue), color = significant)) +
  geom_point(alpha = 0.4, size = 1.5) +
  geom_text_repel(data = top_genes, aes(label = Gene_Symbol),
                  size = 3, box.padding = 0.5) +
  scale_color_manual(values = c("blue3", "grey", "red")) +
  geom_hline(yintercept = -log10(0.05), linetype = "dashed", color = "black") +
  geom_vline(xintercept = c(-logfc_cutoff, logfc_cutoff), linetype = "dashed", color = "black") +
  theme_minimal() +
  labs(title = "Volcano Plot",
       x = "Log2 Fold Change",
       y = "-log10(p-value)",
       color = "Significant")

```
#### 6. Save the Plot with thresholds

```{r}
# Save the customized plot
ggsave("volcano_plot_with_thresholds.png", width = 8, height = 6)

```

### Exercise for you: Use `adjusted pvalues` instead of `pvalues` for the volcano plot

```{r}

```

---

### References:
- [ggplot2 documentation](https://ggplot2.tidyverse.org/)
