R 数据可视化 02 | 火山图

一. 示例数据准备

链接:https://pan.baidu.com/s/1niWjcaJOuimO4NQHzHmvIA
提取码:q6am
mark

文件说明

示例数据,其中数据均为虚拟数据,与实际生物学过程无关

文件名:dataset_volcano.txt

列分别为基因 (gene),差异倍数(logFC),t-test的P值(P.Value)

二. 环境需求

Rstudio:
如果系统中没有 Rstudio,先下载安装:https://www.rstudio.com/products/rstudio/download/#download

ggplot2包:

如果没有安装该R包,执行以下代码:

install.packages('ggplot2'install.packages('ggplot2')

三. 绘制火山图

1. 火山图

mark

# 执行前设置====================================
# 清空暂存数据
rm(list=ls())
# 载入R包
library(ggplot2)
# 设置工作目录
setwd("E:/R/WorkSpace/baimoc/visualization")


# 整理数据集====================================
# 参数'./resource/dataset.txt',表示载入E:/R/WorkSpace/baimoc/visualization/resource/dataset_heatmap.txt
dataset <- read.table('resource/dataset_volcano.txt',header = TRUE)

# 设置pvalue和logFC的阈值
cut_off_pvalue = 0.0000001
cut_off_logFC = 1
# 根据阈值分别为上调基因设置‘up’,下调基因设置‘Down’,无差异设置‘Stable’,保存到change列
# 这里的change列用来设置火山图点的颜色
dataset$change = ifelse(dataset$P.Value < cut_off_pvalue & abs(dataset$logFC) >= cut_off_logFC,
ifelse(dataset$logFC> cut_off_logFC ,'Up','Down'),
'Stable')
# 绘制火山图====================================
ggplot(
#设置数据
dataset,
aes(x = logFC,
y = -log10(P.Value),
colour=change)) +
geom_point(alpha=0.4, size=3.5) +
scale_color_manual(values=c("#546de5", "#d2dae2","#ff4757"))+

# 辅助线
geom_vline(xintercept=c(-1,1),lty=4,col="black",lwd=0.8) +
geom_hline(yintercept = -log10(cut_off_pvalue),lty=4,col="black",lwd=0.8) +

# 坐标轴
labs(x="log2(fold change)",
y="-log10 (p-value)")+
theme_bw()+

# 图例
theme(plot.title = element_text(hjust = 0.5),
legend.position="right",
legend.title = element_blank()
# 执行前设置====================================
# 清空暂存数据
rm(list=ls())
# 载入R包
library(ggplot2)
# 设置工作目录
setwd("E:/R/WorkSpace/baimoc/visualization")


# 整理数据集====================================
# 参数'./resource/dataset.txt',表示载入E:/R/WorkSpace/baimoc/visualization/resource/dataset_heatmap.txt
dataset <- read.table('resource/dataset_volcano.txt',header = TRUE)

# 设置pvalue和logFC的阈值
cut_off_pvalue = 0.0000001
cut_off_logFC = 1
# 根据阈值分别为上调基因设置‘up’,下调基因设置‘Down’,无差异设置‘Stable’,保存到change列
# 这里的change列用来设置火山图点的颜色
dataset$change = ifelse(dataset$P.Value < cut_off_pvalue & abs(dataset$logFC) >= cut_off_logFC,
ifelse(dataset$logFC> cut_off_logFC ,'Up','Down'),
'Stable')
# 绘制火山图====================================
ggplot(
#设置数据
dataset,
aes(x = logFC,
y = -log10(P.Value),
colour=change)) +
geom_point(alpha=0.4, size=3.5) +
scale_color_manual(values=c("#546de5", "#d2dae2","#ff4757"))+

# 辅助线
geom_vline(xintercept=c(-1,1),lty=4,col="black",lwd=0.8) +
geom_hline(yintercept = -log10(cut_off_pvalue),lty=4,col="black",lwd=0.8) +

# 坐标轴
labs(x="log2(fold change)",
y="-log10 (p-value)")+
theme_bw()+

# 图例
theme(plot.title = element_text(hjust = 0.5),
legend.position="right",
legend.title = element_blank()
)
2. 标记基因的火山图

mark

# 执行前设置====================================
# 清空暂存数据
rm(list=ls())
# 载入R包
library(ggplot2)
library(ggrepel)
# 设置工作目录
setwd("E:/R/WorkSpace/baimoc/visualization")


# 整理数据集====================================
# 参数'./resource/dataset.txt',表示载入E:/R/WorkSpace/baimoc/visualization/resource/dataset_heatmap.txt
dataset <- read.table('resource/dataset_volcano.txt',header = TRUE)

cut_off_pvalue = 0.0000001
cut_off_logFC = 1
dataset$change = ifelse(dataset$P.Value < cut_off_pvalue & abs(dataset$logFC) >= cut_off_logFC,
ifelse(dataset$logFC> cut_off_logFC ,'Up','Down'),
'Stable')
p<-ggplot(
#设置数据
dataset,
aes(x = logFC,
y = -log10(P.Value),
colour=change)) +
geom_point(alpha=0.4, size=3.5) +
scale_color_manual(values=c("#546de5", "#d2dae2","#ff4757"))+

# 辅助线
geom_vline(xintercept=c(-1,1),lty=4,col="black",lwd=0.8) +
geom_hline(yintercept = -log10(cut_off_pvalue),lty=4,col="black",lwd=0.8) +

# 坐标轴
labs(x="log2(fold change)",
y="-log10 (p-value)")+
theme_bw()+

# 图例
theme(plot.title = element_text(hjust = 0.5),
legend.position="right",
legend.title = element_blank()
)

# 将需要标记的基因放置在label列
# 这里设置logFC值大于5的差异基因来标记
# !!!需要注意的是标记的基因不能太多,Rstudio容易卡死
dataset$label = ifelse(dataset$P.Value < cut_off_pvalue & abs(dataset$logFC) >= 5, as.character(dataset$gene),"")
p+geom_text_repel(data = dataset, aes(x = dataset$logFC,
y = -log10(dataset$P.Value),
label = label),
size = 3,box.padding = unit(0.5, "lines"),
point.padding = unit(0.8, "lines"),
segment.color = "black",
show.legend = FALSE# 执行前设置====================================
# 清空暂存数据
rm(list=ls())
# 载入R包
library(ggplot2)
library(ggrepel)
# 设置工作目录
setwd("E:/R/WorkSpace/baimoc/visualization")


# 整理数据集====================================
# 参数'./resource/dataset.txt',表示载入E:/R/WorkSpace/baimoc/visualization/resource/dataset_heatmap.txt
dataset <- read.table('resource/dataset_volcano.txt',header = TRUE)

cut_off_pvalue = 0.0000001
cut_off_logFC = 1
dataset$change = ifelse(dataset$P.Value < cut_off_pvalue & abs(dataset$logFC) >= cut_off_logFC,
ifelse(dataset$logFC> cut_off_logFC ,'Up','Down'),
'Stable')
p<-ggplot(
#设置数据
dataset,
aes(x = logFC,
y = -log10(P.Value),
colour=change)) +
geom_point(alpha=0.4, size=3.5) +
scale_color_manual(values=c("#546de5", "#d2dae2","#ff4757"))+

# 辅助线
geom_vline(xintercept=c(-1,1),lty=4,col="black",lwd=0.8) +
geom_hline(yintercept = -log10(cut_off_pvalue),lty=4,col="black",lwd=0.8) +

# 坐标轴
labs(x="log2(fold change)",
y="-log10 (p-value)")+
theme_bw()+

# 图例
theme(plot.title = element_text(hjust = 0.5),
legend.position="right",
legend.title = element_blank()
)

# 将需要标记的基因放置在label列
# 这里设置logFC值大于5的差异基因来标记
# !!!需要注意的是标记的基因不能太多,Rstudio容易卡死
dataset$label = ifelse(dataset$P.Value < cut_off_pvalue & abs(dataset$logFC) >= 5, as.character(dataset$gene),"")
p+geom_text_repel(data = dataset, aes(x = dataset$logFC,
y = -log10(dataset$P.Value),
label = label),
size = 3,box.padding = unit(0.5, "lines"),
point.padding = unit(0.8, "lines"),
segment.color = "black",
show.legend = FALSE)

四. 保存为图片

  • 这里可导出像素图和PDF,也可拷贝到PS调整

    mark

  • 选择合适的文件格式,调整合适长宽,印刷或投稿选PDF,TIFF,EPS就好

  • 文件默认存储在刚刚设置的工作目录里

    mark

五. 详细参数设置说明

1. 设置工作目录
setwd("E:/R/WorkSpace/baimoc/visualization"setwd("E:/R/WorkSpace/baimoc/visualization")

在R的执行过程中,为了方便,需要指定一个获取文件和输出文件所在的目录,这样就不需要每次设置全路径,只需要指定相对目录

setwd("E:/R/WorkSpace/baimoc/visualization") 的意思就是设置工作目录为E:/R/WorkSpace/baimoc/visualization

2. 载入数据
dataset <- read.table('resource/dataset_volcano.txt',header = TRUEdataset <- read.table('resource/dataset_volcano.txt',header = TRUE)

因为工作目录已经设置,如果要获取E:/R/WorkSpace/baimoc/visualization/resource/dataset_volcano.txt文件,那么就只需要设置相对路径resource/dataset_volcano.txt`

读取到的原始数据如下:

mark

3. 设置阈值
cut_off_pvalue = 0.0000001
cut_off_logFC = 1
dataset$change = ifelse(dataset$P.Value < cut_off_pvalue & abs(dataset$logFC) >= cut_off_logFC,
ifelse(dataset$logFC> cut_off_logFC ,'Up','Down'),
'Stable'cut_off_pvalue = 0.0000001
cut_off_logFC = 1
dataset$change = ifelse(dataset$P.Value < cut_off_pvalue & abs(dataset$logFC) >= cut_off_logFC,
ifelse(dataset$logFC> cut_off_logFC ,'Up','Down'),
'Stable')

根据阈值分别为上调基因设置‘up’,下调基因设置‘Down’,无差异设置‘Stable’,保存到change列
这里的change列用来设置火山图点的颜色:

mark

4.设置标记基因
dataset$label = ifelse(dataset$P.Value < cut_off_pvalue & abs(dataset$logFC) >= 5, as.character(dataset$gene),""dataset$label = ifelse(dataset$P.Value < cut_off_pvalue & abs(dataset$logFC) >= 5, as.character(dataset$gene),"")

将需要标记的基因放置在label列, 这里设置logFC值大于5的差异基因来标记
需要注意的是标记的基因不能太多,Rstudio容易卡死

mark