火山图 加标签 不遮蔽 R pheatmap
library(ggplot2)
#head(data) #查看数据类型,主要有P值,Fold change和基因ID即可。
inputFile="D:\\GZlab_W_AQY\\实验记录\\原始数据\\Pvalue_234\\p234.csv"
logFCfilter=0.8 #logFC过滤阈值
fdrFilter=0.1 #矫正后p值阈值ֵ
#setwd("D:\\Coding\\R_gzlab_docu\\PurchasefWeb\\111bioR\\19.vol") #设置工作目录
cut_off_pvalue = 0.1
#读取文件
rt=read.table(inputFile,sep=",",header=T,check.names=F,row.names = 1)
head(rt)
head(rt$logFC)
#定义显著性
#Significant=ifelse((rt$fdr<fdrFilter & abs(rt$logFC)>logFCfilter), ifelse(rt$logFC>logFCfilter,"Up","Down"), "Not")
Significant=ifelse((rt$pvalue<fdrFilter & abs(rt$logFC)>logFCfilter), ifelse(rt$logFC>logFCfilter,"Up","Down"), "Not")
# 1. 颜色区分上下调,加辅助线
#绘制火山图
p = ggplot(rt, aes(logFC, -log10(pvalue)))+
geom_point(aes(col=Significant))+
scale_color_manual(values=c("green", "black", "red"))+
labs(title = " ")+
# 辅助线
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) +
theme(plot.title = element_text(size = 16, hjust = 0.5, face = "bold"))
p=p+theme_bw()
p
# 2. 画简单全黑 散点
ggplot(data = rt, aes(x = logFC, y = -log10(pvalue))) +
geom_point(alpha=0.8, size = 1)
# 3. 颜色区分上下调,加FDR FC 辅助线
# 给数据加一列,上下调
data <- rt
data$change <- as.factor(ifelse(data$pvalue < 0.1 & abs(data$logFC) > 0.8,ifelse(data$logFC > 0.1,'UP','DOWN'),'NOT'))
head(data)
ggplot(data = data, aes(x = logFC, y = -log10(pvalue), color = change)) +
geom_point(alpha=0.8, size = 1) +
theme_bw(base_size = 15) +
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank()
) +
geom_hline(yintercept=2 ,linetype=4) +
geom_vline(xintercept=c(-1,1) ,linetype=4 ) +
scale_color_manual(name = "", values = c("red", "green", "black"), limits = c("UP", "DOWN", "NOT"))
# 4. 另外 加上标签
data$sign <- ifelse(data$fdr < 0.1 & abs(data$logFC) > 0.1,rownames(data),NA)
ggplot(data = data, aes(x = logFC, y = -log10(fdr), color = change)) +
geom_point(alpha=0.8, size = 1) +
theme_bw(base_size = 15) +
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank()
) +
geom_hline(yintercept=2 ,linetype=4) +
geom_vline(xintercept=c(-1,1) ,linetype=4 ) +
scale_color_manual(name = "", values = c("red", "green", "black"), limits = c("UP", "DOWN", "NOT")) +
geom_text(aes(label = sign), size = 3)
#4-2
data$sign <- ifelse(data$pvalue < 0.1 & abs(data$logFC) > 0.5,rownames(data),NA)
ggplot(data = data, aes(x = logFC, y = -log10(pvalue), color = change)) +
geom_point(alpha=0.8, size = 1) +
theme_bw(base_size = 15) +
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank()
) +
geom_hline(yintercept=2 ,linetype=4) +
geom_vline(xintercept=c(-1,1) ,linetype=4 ) +
scale_color_manual(name = "", values = c("red", "green", "black"), limits = c("UP", "DOWN", "NOT")) +
geom_text(aes(label = sign), size = 3)
# 5. 标签防遮蔽
data$sign2 <- ifelse(data$fdr < 0.005 & abs(data$logFC) > 2.5,rownames(data),NA)
data$sign2 <- ifelse(data$pvalue < 0.2 & data$logFC > 0.2,rownames(data),NA)
data$change <- as.factor(ifelse(data$pvalue < 0.2 & abs(data$logFC) > 0.2,ifelse(data$logFC > 0,'UP','DOWN'),'NOT'))
head(data)
head(data)
library(ggrepel)
ggplot(data = data, aes(x = logFC, y = -log10(pvalue), color = change)) +
geom_point(alpha=0.8, size = 2) +
theme_bw(base_size = 16) +
scale_x_continuous(limits = c(-4, 4))+
scale_y_continuous(limits = c(0, 5))+
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank()
) +
scale_color_manual(name = "", values = c("red", "green", "black"), limits = c("UP", "DOWN", "NOT")) +
geom_label_repel(aes(label=sign2), xlim=c(1.2, NA),segment.alpha=0.5,
size=4,max.overlaps=80 ,fontface="bold", color="#A0522D", box.padding=unit(0.35, "lines"), point.padding=unit(0.5, "lines"),
segment.colour = "grey50",segment.size=0.1,fill = alpha(c("white"),0.1))
#geom_label_repel(aes(label=sign2), xlim=c(1.5, NA),
# size=1,max.overlaps=50 ,fontface="bold", color="red", box.padding=unit(0.35, "lines"), point.padding=unit(0.5, "lines"),
# segment.colour = "grey50",segment.size=0.1)
#geom_text_repel(aes(label = sign), box.padding = unit(0.3, "lines"), point.padding = unit(0.4, "lines"), show.legend = F, size = 3)
# 6. 感兴趣的基因标出来
#读取文件
inputFile = "input2.txt"
data2=read.table(inputFile,sep="\t",header=T,check.names=F)
#data$LABEL <- list(1,3,5, 123, 567)
data2$change <- as.factor(ifelse(data$fdr < 0.01 & abs(data$logFC) > 1,ifelse(data$logFC > 1,'UP','DOWN'),'NOT'))
ggplot(data = data2, aes(x = logFC, y = -log10(fdr), color = change)) +
geom_point(alpha=0.8, size = 2) +
theme_bw(base_size = 15) +
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank()
) +
scale_color_manual(name = "", values = c("red", "green", "black"), limits = c("UP", "DOWN", "NOT")) +
geom_label_repel(aes(label=LABEL), fontface="bold", color="grey50", box.padding=unit(0.35, "lines"), point.padding=unit(0.5, "lines"), segment.colour = "grey50")
# 7. 可互动图
library(plotly)
p <- plot_ly(data,
x = ~logFC,
y = ~-log10(pvalue),
text = ~sign2,
type = 'scatter',
mode = 'markers'
)
p