答疑|火山图加文字标签
后台有几个同学对之前的教程跟着CELL学作图|1.火山图提出了几点疑问:
- 能不能直接用ggplot来画
- 如何给自己想要的基因/蛋白加上文字标签
今天简单的解答一下。
2
示例数据为跟着CELL学作图|1.火山图里面的数据~
开始作图
rm(list = ls())
setwd("F:/HJH/mzbj/cell/2021.4.12")
library(RColorBrewer)
library(ggrepel) #标签用
df <- read.csv("df.csv",row.names = 1) #导入数据,第一列作为行名
#确定是上调还是下调,用于给图中点上色
df$threshold = factor(ifelse(df$P_value < 0.05 & abs(df$fd) >= 0.25, ifelse(df$fd >= 0.25 ,'Up','Down'),'NoSignifi'),levels=c('Up','Down','NoSignifi'))
df$gene <- row.names(df) #添加一列基因名,以便备注
ggplot(df,aes(x=fd,y= -log10(P_value),color=threshold))+
geom_point(data = df[df$P_value<0.05&abs(df$fd)>0.25,],size = 5)+
geom_point(data = df[df$P_value>0.05|abs(df$fd)<0.25,],size = 4)+
scale_color_manual(values=c("#4393C3","#00000033","#FC4E2A"))+#确定点的颜色
geom_text_repel(
data = df[df$P_value<0.05&abs(df$fd)>1,],
aes(label = gene),
size = 4.5,
color = "black",
segment.color = "black", show.legend = FALSE )+#添加关注的点的基因名
ylab('-log10 (P_value)')+#修改y轴名称
xlab('log2 (FoldChange)')+#修改x轴名称
geom_vline(xintercept=c(-0.25,0.25),lty=3,col="black",lwd=0.5) +#添加横线|logFoldChange|>0.25
geom_hline(yintercept = -log10(0.05),lty=3,col="black",lwd=0.5) +#添加竖线padj<0.05
theme_classic( # 主题设置,这个是无线条主题
base_line_size = 1 # 坐标轴的粗细
)+
theme(axis.title.x = element_text(size = 15,
color = "black",
face = "bold"),
axis.title.y = element_text(size = 15,
color = "black",
face = "bold",
vjust = 1.9,
hjust = 0.5,
angle = 90),
legend.title = element_blank(),
legend.text = element_text(color="black", # 设置图例标签文字
size = 10,
face = "bold"),
axis.text.x = element_text(size = 13, # 修改X轴上字体大小,
color = "black", # 颜色
face = "bold", # face取值:plain普通,bold加粗,italic斜体,bold.italic斜体加粗
vjust = 0.5, # 位置
hjust = 0.5,
angle = 0), #角度
axis.text.y = element_text(size = 13,
color = "black",
face = "bold",
vjust = 0.5,
hjust = 0.5,
angle = 0)
)
出图
210514_1备注
更详细的讲解明天会在我的B站:木舟笔记
更新一期视频。如果对代码的细节有疑问的,可以看一下视频。希望大家可以多多点赞在看!
网友评论