使用ggplot2包绘制火山图时,在数据标签显示上出现问题。
代码如下:
// 火山图
remove(list = ls())
library(ggplot2)
library(ggrepel)
##导入文件
data <- read.csv(file = "C:/Users/luokai/Desktop/Deseq2.csv")
data <- na.omit(date)
##筛选出上调与下调
data$change <- factor(ifelse(data$padj < 0.05 & abs(data$log2FoldChange) >= 1,ifelse(data$log2FoldChange >= 1,"up","down"),"stable"),levels = c("up","down","stable"))
##作图
ggplot(data,aes(data$log2FoldChange,-1*log10(data$padj),color = change)) + geom_point() + scale_color_manual(values = c("#2a9d8f","#8d99ae80","#f8961e")) +xlim(-10,10) + ylim(0,150) + geom_hline(yintercept = -log10(0.05),linetype=4,size=0.8) +geom_vline(xintercept = c(-1,1),linetype=4,size=0.8) + theme(title = element_text(size = 18),text = element_text(size = 18)) +labs(x = "log2(foldchange)",y="-log10(padj)") + geom_text_repel(data = data[data$padj<0.05&abs(data$log2FoldChange)>7,],aes(label = X),size = 2)
运行作图命令后报错,提示Aesthetics长度必须为1或者与data相同:
错误: Aesthetics must be either length 1 or the same as the data (2): x and y
Run `rlang::last_error()` to see where the error occurred.
检查发现,在作图的第一行aes括号内映射出现错误,正确命令如下:
// 之前命令不变
##作图
ggplot(data,aes(x=log2FoldChange, y=-log10(padj),color = change)) + geom_point() + scale_color_manual(values = c("#2a9d8f","#8d99ae80","#f8961e")) +xlim(-10,10) + ylim(0,150) + geom_hline(yintercept = -log10(0.05),linetype=4,size=0.8) +geom_vline(xintercept = c(-1,1),linetype=4,size=0.8) + labs(x = "log2(foldchange)",y="-log10(padj)") + geom_text_repel(data = data[data$padj<0.05&abs(data$log2FoldChange)>7,],aes(label = X),size = 3)
这样就得到了火山图,并且geom_text_repel()函数也起到了作用。
图片如下: