![](https://news.xinpengboligang.com/upload/keji/c0605e9795e27f74d37997760cd390d7.jpeg)
大傢好,我是半島小鐵盒!上期內容分享了如何使用python來繪制火山圖,本期內容為大傢分享的是如何使用R語言的ggplot2繪制好看的火山圖。往下看,三分鐘教會你!
1.加載繪圖數據
導包,加載基因差異表達數據,重定義列名:
library(ggplot2)
library(tidyverse)
library(ggrepel)
library(latex2exp)
library(RColorBrewer)
# 讀取rawdata
deg_data <- read.csv('https://raw.githubusercontent.com/jiangtao639/ExampleDataRepository/master/volcano_data.csv',
header=TRUE, row.names = 1)
# 修改列名
data <- deg_data[c('row', 'log2FoldChange', 'pvalue', 'padj')]
colnames(data) <- c('geneName', 'log2FoldChange',
'pvalue', 'FDR')
2.數據預處理
分別設置差異倍數和顯著性的閾值為2和0.5,添加差異表達水平標簽
log2FC_tresh <- 1
FDR_tresh <- 0.05
# 添加差異表達水平標簽, 按照FDR升序
data <- data %>% mutate(data, label =
ifelse(log2FoldChange >= log2FC_tresh & FDR < FDR_tresh, 'Up',
ifelse(log2FoldChange <= -log2FC_tresh & FDR < FDR_tresh, 'Down',
'Normal'))) %>%
arrange(FDR)
添加要註釋的核心基因
# 這裡註釋前10個
data$annotation <- ''
data[1:10, c("annotation")] <- data[1:10, c("geneName")]
view(data)
![](https://news.xinpengboligang.com/upload/keji/0ecc44e18f0c8092194dc9fdbb556daf.jpeg)
3.基礎繪圖
最樸素的繪圖,什麼參數都不調整。感覺都比seaborn畫的好看
p1 <- ggplot(data=data, mapping=aes(x=log2FoldChange, y=-log10(FDR)))
geom_point(mapping=aes(color=label, fill=label),
alpha=0.8, size=1, shape=21)
geom_vline(xintercept = c(-log2FC_tresh,log2FC_tresh),
linetype ="dashed")
geom_hline(yintercept = -log10(FDR_tresh),
linetype ="dashed")
theme_bw()
![](https://news.xinpengboligang.com/upload/keji/43fb13591aa16e49a36372a19d231ab4.jpeg)
4.參數細調的精美繪圖
主要調整下坐標軸標簽、圖例位置和字體以及背景, 添加核心基因註釋
p2 <- p1
geom_text_repel(aes(label=annotation, color=label), size=3,
max.overlaps = 100,
key_glyph = draw_key_point)
labs(x= TeX("$Log_2 \\textit{FC} $"),
y= TeX("$-Log_{10} \\textit{FDR} $"),
title="VolcanoPlot")
theme(plot.title = element_text(hjust=0.5, size=15),
plot.background = element_rect(fill = "white",
color = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.99, 0.99),
legend.justification = c(1, 1),
legend.background = element_rect(fill = "white",
color = "grey"),
legend.title = element_text(size=15, hjust=0.5),
legend.text = element_text(size = 12),
)
guides(color=guide_legend("Level"),
fill=guide_legend("Level"))
是不是更好看了!
![](https://news.xinpengboligang.com/upload/keji/68793429a6ab20534f3723ea10fc4d29.jpeg)
修改配色
p3 <- p2
scale_fill_manual(values=c("#00AFBB","#999999","#FC4E07"))
scale_color_manual(values=c("#00AFBB","#999999","#FC4E07"))
p4 <- p2
scale_fill_brewer(palette = "Set1")
scale_color_brewer(palette = "Set1")
![](https://news.xinpengboligang.com/upload/keji/9f07d13dc35221391223c0f738c0d73f.jpeg)
5.繪制漸變火山圖
# 漸變火山圖
p <- ggplot(data=data, mapping=aes(x=log2FoldChange, y=-log10(FDR)))
geom_point(mapping=aes(color=-log10(FDR), fill=-log10(FDR), size=-log10(FDR)), alpha=0.8, shape=21)
scale_size_continuous(range=c(1,4))
geom_text_repel(aes(label=annotation, color=-log10(FDR)), size=3,
max.overlaps = 100, key_glyph = draw_key_point)
geom_vline(xintercept = c(-log2FC_tresh,log2FC_tresh), linetype ="dashed")
geom_hline(yintercept = -log10(FDR_tresh), linetype ="dashed")
labs(x= TeX("$Log_2 \\textit{FC} $"),
y= TeX("$-Log_{10} \\textit{FDR} $"),
title="VolcanoPlot")
theme_bw()
theme(plot.title = element_text(hjust=0.5, size=15),
plot.background = element_rect(fill = "white", color = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.99, 0.49),
legend.justification = c(1, 0.5),
legend.background = element_rect(fill = "white"),
legend.title = element_text(size=12, hjust=0.5),
legend.text = element_text(size = 12),
)
guides(fill=guide_colorbar(TeX("$-Log_{10} \\textit{FDR} $")),
color='none', size='none')
p1 <- p scale_fill_gradientn(values = seq(0,1,0.2),
colors = c("#39489f","#39bbec","#f9ed36","#f38466","#b81f25"))
scale_color_gradientn(values = seq(0,1,0.2),
colors = c("#17194e","#68bfe7","#f9ed36","#a22f27","#211f1f"))
![](https://news.xinpengboligang.com/upload/keji/a165736e0397e7605889a336b8857ed3.jpeg)
修改配色主題
p2 <- p scale_fill_gradientn(values = seq(0,1,0.2),
colors = rev(brewer.pal(8, "Spectral")))
scale_color_gradientn(values = seq(0,1,0.2),
colors = rev(brewer.pal(8, "Spectral")))
p2
![](https://news.xinpengboligang.com/upload/keji/db5695697c0025919f632ccf2e70bc54.jpeg)
總結 >>>
R語言繪圖實現起來要比python容易,強烈推薦使用ggplot2作為底層庫來繪圖,繪制的圖案更加符合美學!火山圖完結~