有了R语言的基础,以及ggplot2绘图基础,我们的生信常用分析图形的绘制就可以提上日程了!本系列,师兄就开始带着大家一起学习如何用R语言绘制我们自己的各种分析图吧!
由于本系列的所有分析代码均为师兄细心整理和详细注释而成的!欢迎点赞、收藏、转发!
您的支持是我持续更新的最大动力!
系列内容包括:
- 各种类型的热图你学会了吗?
- 普通热图
- 环形热图
- 解锁火山图真谛!
- plot函数就能画火山图?
- 高级函数绘制火山图--ggplot2、ggpurb
- 经典富集分析及气泡图、柱状图绘制
- 气泡图绘制
- 柱状图绘制
- 富集分析圈图
- 富集分析弦图
- 绘制一张可以打动审稿人的桑基图
- 生存分析 -- KM曲线图
- 基础PCA图
- 云雨图
- 韦恩图
- 环形互作网络图
- 相互作用网络图
- 聚类树美化
- 富集分析气泡图进阶版
- mantel test相关性图
- 词云图
- 瀑布图
- 森林图
- 曼哈顿图
- 哑铃图
- 三线表
- 嵌套圈图
- 列线图
- 蜂群图
- 箱线图+贝塞尔曲线
- 矩阵散点图
- 等等,想到再继续补充!!!
本期火山图效果展示
效果展示R自带函数plot就能画火山图?
##############
data <- read.csv("DEG.csv",row.names = 1)
color <- rep("#999999",nrow(data))
color[data$pvalue <0.05 & data$log2FoldChange > 1] <- "#FC4E07"
color[data$pvalue <0.05 & data$log2FoldChange < -1] <- "#00AFBB"
par(oma = c(0,2,0,0))
plot(data$log2FoldChange,-log10(data$pvalue),pch = 16,cex = 0.5,
xlim = c(-4,4), ylim = c(0,32), col = color, frame.plot = F,
xlab = "log2FC", ylab = "-log10(Pvalue)", cex.axis = 1, cex.lab = 1.3)
# 添加参考线:
abline(h = -log10(0.05),lwd = 2, lty = 3) # lwd设置线的宽度,lty设置线的类型;
abline(v = c(-1,1),lwd = 2, lty = 3) # lwd设置线的宽度,lty设置线的类型;
# 添加图例
legend(x = 3, y = 32, legend = c("Up","Normal","Down"),
bty = "n", # 去除边框
pch = 19,cex = 1, # 设置点的样式和大小
x.intersp = 0.3, # 设置字与点之间的距离;
y.intersp = 0.3, # 设置点与点的高度差,相当于行距;
col = c("#FC4E07","#999999","#00AFBB"))
# 添加标签:
color = c()
color[which(data[1:10,]$regulate == "Up")] = "#FC4E07"
color[which(data[1:10,]$regulate != "Up")] = "#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)
- 将上述代码封装成函数,以后调用,就可以一键出图:
# 包装函数:
# 调整1: xlim和ylim得去掉
# 调整2: 修改图例的位置
plotVoc <- function(data){
color <- rep("#999999",nrow(data))
color[data$pvalue <0.05 & data$log2FoldChange > 1] <- "#FC4E07"
color[data$pvalue <0.05 & data$log2FoldChange < -1] <- "#00AFBB"
par(oma = c(0,2,0,0))
plot(data$log2FoldChange,-log10(data$pvalue),pch = 16,cex = 0.5,
col = color, frame.plot = F,
xlab = "log2FC", ylab = "-log10(Pvalue)", cex.axis = 1, cex.lab = 1.3)
# 添加参考线:
abline(h = -log10(0.05),lwd = 2, lty = 3) # lwd设置线的宽度,lty设置线的类型;
abline(v = c(-1,1),lwd = 2, lty = 3) # lwd设置线的宽度,lty设置线的类型;
# 添加图例
legend(x = 3, y = max(-log10(data$pvalue)), legend = c("Up","Normal","Down"),
bty = "n", # 去除边框
pch = 19,cex = 1, # 设置点的样式和大小
x.intersp = 0.3, # 设置字与点之间的距离;
y.intersp = 0.3, # 设置点与点的高度差,相当于行距;
col = c("#999999", "#FC4E07","#00AFBB"))
# 添加标签:
color = c()
color[which(data[1:10,]$regulate == "Up")] = "#FC4E07"
color[which(data[1:10,]$regulate != "Up")] = "#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)
}
data <- read.csv("DEG2.csv",row.names = 1)
plotVoc(data)
效果1
高级函数绘制火山图--ggplot2、ggpurb
- 使用ggplot2绘制:
library(ggplot2)
data <- read.csv("DEG.csv",row.names = 1)
#################
# ggplot2绘制火山图
data$label <- c(rownames(data)[1:10],rep(NA,nrow(data) - 10))
ggplot(data,aes(log2FoldChange,-log10(pvalue),color = regulate)) +
xlab("log2FC") +
geom_point(size = 0.6) +
scale_color_manual(values=c("#00AFBB","#999999","#FC4E07")) +
geom_vline(xintercept = c(-1,1), linetype ="dashed") +
geom_hline(yintercept = -log10(0.05), linetype ="dashed") +
theme(title = element_text(size = 15), text = element_text(size = 15)) +
theme_classic() +
geom_text(aes(label = label),size = 3, vjust = 1,hjust = -0.1)
- 使用ggpurb的scatter函数也可以画:
#############
# 使用ggpurb绘制火山图:
library(ggpubr)
data$pvalue <- -log10(data$pvalue)
ggscatter(data,
x = "log2FoldChange",
y = "pvalue",
ylab="-log10(P.value)",
size=0.6,
color = "regulate",
label = rownames(data),
label.select = rownames(data)[1:10],
repel = T,
palette = c("#00AFBB", "#999999", "#FC4E07")) +
geom_hline(yintercept = 1.30,linetype ="dashed") +
geom_vline(xintercept = c(-1,1),linetype ="dashed")
效果2
往期优秀图形目录
渐变火山图 气泡图+相关性热图复杂提琴图
复杂热图 复杂散点图
复杂热图02 甘特图 百分比柱状图 箱线图美化 弦图 mantel test图 瀑布图 曼哈顿图 KEGG富集图 哑铃图
以上内容仅为群内部分内容,不代表全部。详细目录请看下链接!
网友评论