今天学习画,分析转录组等数据时,最常见的火山图,其实就是优化的散点图而已,今天测试用plot实现,其实ggplot实现更方面,下篇测试如何用ggplot实现。
这个是我们今天测试的csv数据格式,就是普通的转录组数据的分析结果。
data <- read.csv("DEG.csv",row.names=1)
下面是没有调整之前,最基本的plot进行控制画的散点图。
plot(data$log2FoldChange,-log10(data$pvalue),pch = 16,cex = 0.5,xlim = c(-4,4), ylim = c(0,32),frame.plot = F,xlab = "log2FC", ylab = "-log10(Pvalue)", cex.axis = 1, cex.lab = 1.3)
其中,几个比较常见的几个参数控制:
pch //点的形状
frame.plot //是否显示图边框
cex //字符或者形状大小,表示绘图符号相对于默认大小的缩放倍数。默认大小为1,1.5表示放大为默认值的1.5倍,0.5表示缩小为默认值的50%。
xlab,ylab //X轴,y轴的名字
xlim,ylim //X轴,y轴的大小范围
下面,我们来添加参考线,一般选取FC的2倍,也就是1,-1,pvalue的0.05添加:
abline(h = -log10(0.05),lwd = 2, lty = 3)
abline(v = c(-1,1),lwd = 2, lty = 3)
其中:
lwd //一般来设置粗细
lty //设置形状类型
这样子,火山图的基本形状就出来了,下面来操作改变颜色,一般上升为红色,下降为蓝色,不变是灰色。
color <- rep("#999999",nrow(data))
color[data$pvalue <0.05 & data$log2FoldChange > 1] <- "#FC4E07"
color[data$pvalue <0.05 & data$log2FoldChange < -1] <- "#00AFBB"
#color[data$regulate=="Up"]<- "#FC4E07"
#color[data$regulate=="Down"]<- "#00AFBB" //当然,也可以通过用户指定的candidate来定义颜色
plot(data$log2FoldChange,-log10(data$pvalue),pch = 16,cex = 0.5,xlim = c(-4,4), ylim = c(0,32),
frame.plot = F,xlab = "log2FC", ylab = "-log10(Pvalue)", cex.axis = 1, cex.lab = 1.3,col = color)
abline(h = -log10(0.05),lwd = 2, lty = 3)
abline(v = c(-1,1),lwd = 2, lty = 3)
这里就是通过plot的col来控制颜色的
最后一步就是添加图例legend了。
plot(data$log2FoldChange,-log10(data$pvalue),pch = 16,cex = 0.5,xlim = c(-5,5), ylim = c(0,35),
frame.plot = F,xlab = "log2FC", ylab = "-log10(Pvalue)", cex.axis = 1, cex.lab = 1.3,col = color)
abline(h = -log10(0.05),lwd = 2, lty = 3)
abline(v = c(-1,1),lwd = 2, lty = 3)
legend(x = 2.5, y = 34, pch=19,legend = c("Up","Normal","Down"), col = c("#FC4E07","#999999","#00AFBB"),x.intersp = 1,y.intersp = 1)
其中:
x.intersp = 1, // 设置字与点之间的距离;
y.intersp = 1, // 设置点与点的高度差,相当于行距;
如果不想要边框的话:bty = "n" 可以实现
当然,我们也可以highlight一些gene,比如highlight前10行。
plot(data$log2FoldChange,-log10(data$pvalue),pch = 16,cex = 0.5,xlim = c(-5,5), ylim = c(0,35),
frame.plot = F,xlab = "log2FC", ylab = "-log10(Pvalue)", cex.axis = 1, cex.lab = 1.3,col = color)
abline(h = -log10(0.05),lwd = 2, lty = 3)
abline(v = c(-1,1),lwd = 2, lty = 3)
legend(x = 2.5, y = 34, pch=19,legend = c("Up","Normal","Down"), col = c("#FC4E07","#999999","#00AFBB"),
x.intersp = 1,y.intersp = 1,bty="n")
color[which(data[1:10,]$regulate == "Up")] = "#FC4E07"
color[which(data[1:10,]$regulate == "Down")] = "#00AFBB"
text(data$log2FoldChange[1:10],-log10(data$pvalue)[1:10],labels = data$row[1:10],adj = c(0,1.5),cex = 0.6,col = color)