R语言可视化(十一):火山图绘制
11.火山图绘制
清除当前环境中的变量
rm(list=ls())
设置工作目录
setwd("C:/Users/Dell/Desktop/R_Plots/11volcano/")
读取示例数据
data <- read.table("demo_volcano.txt",header = T,
check.names = F,row.names = 1,sep="\t")
# 查看示例数据
head(data)
## LOG_count WT0_count LOG_rpkm WT0_rpkm log2_Ratio(WT0/LOG)
## Unigene0000003 17243 17525 931.5677 942.4515 0.01675776
## Unigene0000004 65 101 4.1156 6.3657 0.62921640
## Unigene0000005 909 984 36.8859 39.7458 0.10773298
## Unigene0000006 1376 1082 74.7026 58.4714 -0.35342732
## Unigene0000007 121 73 15.6800 9.4163 -0.73569337
## Unigene0000008 221 308 13.1401 18.2287 0.47223542
## Pvalue FDR significant
## Unigene0000003 0.5149484880 0.6733042200 no
## Unigene0000004 0.0066439000 0.0178825370 no
## Unigene0000005 0.1259729560 0.2246500490 no
## Unigene0000006 0.0000000117 0.0000000686 no
## Unigene0000007 0.0007419290 0.0024460860 no
## Unigene0000008 0.0003037820 0.0010721500 no
base plot函数绘制火山图
attach(data)
# 基础火山图
plot(x=`log2_Ratio(WT0/LOG)`,y=-1*log10(FDR))
# 添加颜色和标题
plot(x=`log2_Ratio(WT0/LOG)`,y=-1*log10(FDR),
type = "p", pch=20, col=significant,
main="Volcano plot",
xlab="log2(FC)",ylab="-log10(FDR)")
# 添加水平线和垂直线
abline(v=c(-1,1),lty=2,lwd = 2,col="red")
abline(h=-log10(0.05),lty=2,lwd=2,col="blue"
# 添加图例
legend("topright", inset = 0.01, title = "Significant", c("yes","no"),
pch=c(16,16),col = c("red","black"))
# 添加gene注释信息
gene_selected <- c("Unigene0034898","Unigene0038455","Unigene0003997",
"Unigene0026444","Unigene0039482","Unigene0028163"
)
data_selected <- data[gene_selected,]
text(x=data_selected$`log2_Ratio(WT0/LOG)`,y=-1*log10(data_selected$FDR),
labels = rownames(data_selected),col="red",adj = 0.5)
detach(data)
ggplot2包绘制火山图
library(ggplot2)
head(data)
## LOG_count WT0_count LOG_rpkm WT0_rpkm log2_Ratio(WT0/LOG)
## Unigene0000003 17243 17525 931.5677 942.4515 0.01675776
## Unigene0000004 65 101 4.1156 6.3657 0.62921640
## Unigene0000005 909 984 36.8859 39.7458 0.10773298
## Unigene0000006 1376 1082 74.7026 58.4714 -0.35342732
## Unigene0000007 121 73 15.6800 9.4163 -0.73569337
## Unigene0000008 221 308 13.1401 18.2287 0.47223542
## Pvalue FDR significant
## Unigene0000003 0.5149484880 0.6733042200 no
## Unigene0000004 0.0066439000 0.0178825370 no
## Unigene0000005 0.1259729560 0.2246500490 no
## Unigene0000006 0.0000000117 0.0000000686 no
## Unigene0000007 0.0007419290 0.0024460860 no
## Unigene0000008 0.0003037820 0.0010721500 no
# 基础火山图
ggplot(data,aes(`log2_Ratio(WT0/LOG)`,-log10(FDR))) + geom_point()
# 添加颜色和标题
ggplot(data,aes(`log2_Ratio(WT0/LOG)`,-log10(FDR),color=significant)) +
geom_point() +
labs(title="Volcano plot",x=expression(log[2](FC)), y=expression(-log[10](FDR)))
# 更改颜色,主题,添加水平线和垂直线,去掉网格线
p <- ggplot(data,aes(`log2_Ratio(WT0/LOG)`,-log10(FDR),color=significant)) +
geom_point() + theme_bw() +
labs(title="Volcano plot",x=expression(log[2](FC)), y=expression(-log[10](FDR))) +
scale_color_manual(values = c("blue","red")) +
geom_vline(xintercept=-1, linetype=2, colour="gray30") +
geom_vline(xintercept=1, linetype=2, colour="gray30") +
geom_hline(yintercept=-log(0.05), linetype=2, colour="gray30") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
p
# 添加基因注释信息
library(ggrepel)
p + geom_text_repel(data=data_selected,
aes(label=rownames(data_selected)))
p + geom_label_repel(data=data_selected,
aes(label=rownames(data_selected)))
ggpubr包绘制火山图
library(ggpubr)
data$FDR <- -log10(data$FDR)
data$FC <- data$`log2_Ratio(WT0/LOG)`
head(data)
## LOG_count WT0_count LOG_rpkm WT0_rpkm log2_Ratio(WT0/LOG)
## Unigene0000003 17243 17525 931.5677 942.4515 0.01675776
## Unigene0000004 65 101 4.1156 6.3657 0.62921640
## Unigene0000005 909 984 36.8859 39.7458 0.10773298
## Unigene0000006 1376 1082 74.7026 58.4714 -0.35342732
## Unigene0000007 121 73 15.6800 9.4163 -0.73569337
## Unigene0000008 221 308 13.1401 18.2287 0.47223542
## Pvalue FDR significant FC
## Unigene0000003 0.5149484880 0.1717887 no 0.01675776
## Unigene0000004 0.0066439000 1.7475709 no 0.62921640
## Unigene0000005 0.1259729560 0.6484935 no 0.10773298
## Unigene0000006 0.0000000117 7.1636759 no -0.35342732
## Unigene0000007 0.0007419290 2.6115283 no -0.73569337
## Unigene0000008 0.0003037820 2.9697445 no 0.47223542
# 基础火山图
ggscatter(data,x="FC",y="FDR")
# 添加颜色,标题,坐标轴标签
ggscatter(data,x="FC",y="FDR",size = 1.5,
color = "significant", palette = c("#BBBBBB","#CC0000"),
title = "Volcano plot",xlab = "log2(FC)",ylab = "-log10(FDR)")
# 添加水平线和垂直线,标题居中
ggscatter(data,x="FC",y="FDR",size = 1.5,
color = "significant", palette = c("#BBBBBB","#CC0000"),
title = "Volcano plot",xlab = "log2(FC)",ylab = "-log10(FDR)") +
geom_vline(xintercept=c(-1,1), linetype=2, colour="gray30") +
geom_hline(yintercept=-log(0.05), linetype=2, colour="gray30") +
theme(plot.title = element_text(hjust = 0.5))
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] ggpubr_0.2.1 magrittr_1.5 ggrepel_0.8.1 ggplot2_3.2.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.5 knitr_1.23 tidyselect_0.2.5 munsell_0.5.0
## [5] colorspace_1.4-1 R6_2.4.0 rlang_0.4.7 stringr_1.4.0
## [9] dplyr_0.8.3 tools_3.6.0 grid_3.6.0 gtable_0.3.0
## [13] xfun_0.8 withr_2.1.2 htmltools_0.3.6 yaml_2.2.0
## [17] lazyeval_0.2.2 digest_0.6.20 assertthat_0.2.1 tibble_2.1.3
## [21] ggsignif_0.5.0 crayon_1.3.4 purrr_0.3.2 glue_1.3.1
## [25] evaluate_0.14 rmarkdown_1.13 labeling_0.3 stringi_1.4.3
## [29] compiler_3.6.0 pillar_1.4.2 scales_1.0.0 pkgconfig_2.0.2
END