小提琴图保留了箱式图的优势,有效显示中位数,范围和变异性,简单美观可看性强,今天给大家分享下绘制小提琴图、箱线图、及散点图的基本做法。
读入数据
library(ggplot2)
library(patchwork)
data <- read.csv("DEGs_FC.csv",header=T)
head(data)
data$sampleA <- as.factor(data$sampleA) #指定因子
绘制
#箱线图
ggplot(data=data,aes(x=sampleA,
y=log2FoldChange,color=sampleA
))+
geom_boxplot() -> p1
#散点图
ggplot(data=data,aes(x=sampleA,
y=log2FoldChange,
color=sampleA))+
geom_jitter() -> p2
#小提琴图
ggplot(data=data,aes(x=sampleA,
y=log2FoldChange,
color=sampleA))+
geom_violin() ->p3
p1+p2+p3+plot_layout(nrow=1)#拼图
拼图效果
三图合一
cbPalette <- c("#6495ED", "#DA70D6", "#FF3030")
ggplot(data=data,aes(x=sampleA,
y=log2FoldChange,
color=sampleA))+
geom_jitter(alpha=0.2,
position=position_jitterdodge(jitter.width = 0.35,
jitter.height = 0,
dodge.width = 0.8))+
geom_boxplot(alpha=0.2,width=0.45,
position=position_dodge(width=0.8),
size=0.75,outlier.colour = NA)+
geom_violin(alpha=0.2,width=0.9,
position=position_dodge(width=0.8),
size=0.75)+
scale_color_manual(values = cbPalette)+
theme_classic() +
theme(legend.position="none") +
theme(text = element_text(size=16)) +
#ylim(0.0,1.3)+
ylab("log2Fold Change")+xlab("Treatment")+
theme(axis.text.x=element_text(vjust=1,size=20,face = "bold"))+
theme(axis.text.y=element_text(vjust=1,size=20,face = "bold"))
三图合一
网友评论