ggplot分组散点图-坐标轴截断-添加四分位图-显著性检验
2022-06-10 本文已影响0人
KS科研分享与服务
近日在《The new england journal o f medicine》杂志看到一篇文章的图,如下,这种图应该是用GraphPad prism做的,图的特点是散点统计图,仔细观察中间还展示了平均值和四分位数,坐标轴也是截断的。这里我们使用R来做一下。
image.png(Reference:A Novel Circulating MicroRNA for the Detection of Acute Myocarditis)
示例数据及注释代码已上传群文件!
首先读入数据,包含表达值和分组:
setwd("E:/生物信息学/ggplot坐标轴截断")
A <- read.csv("Exp.csv", header = T)
library(ggplot2)
library(forcats)
library(ggpubr)
A$GeneSymbol <- as.factor(A$GeneSymbol)
A$GeneSymbol <- fct_inorder(A$GeneSymbol)
计算四分位数:
B <- A %>%
group_by(GeneSymbol) %>%
mutate(upper = quantile(S100A12, 0.75),
lower = quantile(S100A12, 0.25),
mean = mean(S100A12),
median = median(S100A12))
设置需要比较的分组:
my_comparisons1 <- list(c("Asymptomatic", "Mild"))
my_comparisons2 <- list(c("Asymptomatic", "Severe"))
my_comparisons3 <- list(c("Asymptomatic", "Critical"))
ggplot作图:
p <- ggplot(A, aes(GeneSymbol, S100A12,
shape=GeneSymbol, fill=GeneSymbol))+
geom_jitter(size=3, position = position_jitter(0.2))+
scale_shape_manual(values = c(21,24,25,22))+
scale_fill_manual(values=c("grey",
"#0073B5",
"#C9543B",
"#E59F3F"))+
geom_errorbar(data=B, aes(ymin = lower,
ymax = upper),width = 0.2,size=0.5)+
stat_summary(fun = "mean",
geom = "crossbar",
mapping = aes(ymin=..y..,ymax=..y..),
width=0.4,
size=0.3)+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line=element_line(colour="black"),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 14,angle = 45,
vjust = 1,hjust = 1,
color = 'black',face="bold"),
axis.text.y = element_text(size = 12, color = 'black'),
plot.title = element_text(hjust = 0.5,size=15,face="bold"),
legend.position = "NA")+
ggtitle("S100A2")+
stat_compare_means(method="t.test",hide.ns = F,
comparisons =c(my_comparisons1,my_comparisons2,my_comparisons3),
label="p.signif",
bracket.size=0.8,
size=6)
image.png
坐标轴截断,有很多函数可以实现,这里演示两种:
install.packages("gg.gap")
library(gg.gap)
gg.gap(plot=p,
segments=c(5,10),
ylim=c(0,850),
tick_width = c(1,100))
还有ggbreak:
install.packages("ggbreak")
library(ggbreak)
p+scale_y_cut(breaks = 5,
which = c(1,3),
scales = c(3,0.5),
space = 0.1)
image.png
总体可以,像文章中的要做很多数据的时候,可以使用循环作图。当然了,一般情况还是建议用prism做就可以了,因为还是比较方便!
看完了。觉得分享有用的话,帮忙点个赞,分享一下再走呗!
更多精彩请关注我的公众号《KS科研分享与服务》!