环境初始化
rm(list=ls())
Sys.setenv(LANGUAGE = "en") #显示英文报错信息
options(stringsAsFactors = FALSE) #禁止chr转成factor
options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/")
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
### 安装包
cranpackage<-c("ggplot2","ggpubr")
for (pkg in cranpackage){
if (! require(pkg,character.only=T) ) {
install.packages(pkg,ask = F,update = F)
require(pkg,character.only=T)
}
}
# library(ggplot2)
# library(ggpubr)
读入差异分析后结果
load("04_火山图/volcano.Rdata")#通过将数据单独存储为R可以直接读取的Rdata文件,方便调用
data<-deg1
data$gene <- rownames(data)#在data变量中新创建一个列,名称为gene
colnames(data)
ml<-"adj.P.Val"
# ml<-"P.Value"
up<-length(data$gene[data[,ml]<0.05&data$logFC > 0.05])#注意,这种大小比较返回的是TRUE和FALSE,length()求的是长度
down<-length(data$gene[data[,ml]<0.05&data$logFC< c(-0.05) ])#adj.P.Val小于0.05而且logFC< c(-0.05)
画图
ggplot(data=data, aes(x=logFC, y =-log10(adj.P.Val))) +
## 画出变化倍数少的点
geom_point(data=subset(data,abs(data$logFC) <= 0.05),color="#8B8B83",alpha=0.3) +
## 画出logFC > 0.05的点
geom_point(data=subset(data,data[,ml]<0.05 & data$logFC > 0.05),color="#CD5555",alpha=0.4) +
## 画出1 < logFC < 2的点
# geom_point(data=subset(data,data[,ml]<0.05 & data$logFC > 1&data$logFC < 2),aes(size=abs(logFC)),color="#FA8072",alpha=0.4) +
## 画出logFC < -2的点
geom_point(data=subset(data,data[,ml]<0.05 & data$logFC < c(-0.05)),color="darkgreen",alpha=0.4) +
## 画出-2< logFC < -1的点
# geom_point(data=subset(data,data[,ml]<0.05 & data$logFC >c(-2)& data$logFC <c(-1)),aes(size=abs(logFC)),color="#6fae6f",alpha=0.4) +
## 画竖线
geom_vline(xintercept = c(0.05,-0.05),lty=2,lwd=0.6,alpha=0.8)+
geom_hline(yintercept = c(-log10(0.05)),lty=2,lwd=0.6,alpha=0.8)+
## 主题(这里面不设置主题),ylim是人为规定的
theme_bw()+
ylim(-2,80)+
scale_x_continuous(breaks=seq(-1, 1, 0.5)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x = element_text(size = 10,colour = "black"),
axis.text.y = element_text(size = 10,colour = "black"),
axis.title.x = element_text(size = 10,colour = "black"),
axis.title.y = element_text(size = 10,colour = "black"),
axis.line = element_line(colour = "black"))+
## 修改坐标轴
labs(x="Log2 (fold change)",y="-log10 (q-value)")+
## 去掉注释
theme(legend.position='none')
ggsave("04_火山图/vocalno.pdf",width = 4,height = 4.5)