添加显著性标记用到的是
ggsignif
包中的geom_signif()
函数
在昨天推文的基础上,如果是利用带重复的原始数据作图,然后利用geom_signif()
函数作图的时候我遇到了报错,暂时还不知道如何解决。等找到原因了再来介绍。
查找资料的过程中找到了一篇论文
https://www.jove.com/t/60139/nest-building-behavior-as-an-early-indicator-behavioral-deficits Nest Building Behavior as an Early Indicator of Behavioral Deficits in Mice
(这个期刊好特殊,还提供做实验的视频)
论文里有一幅图
提供了代码和数据,今天的推文来学习一下他的代码是如何写的
首先是准备数据
他这里是直接通过代码输入数据,没有将数据存储到文件里
代码语言:javascript复制Genotype = c("Wildtype", "APOE4", "Wildtype", "APOE4", "Wildtype", "APOE4", "Wildtype", "APOE4")
Material = c("Shredded paper","Shredded paper","Square","Square","Bedding","Bedding","Twist", "Twist")
Mean = c(4.77,3.52, 2.57, 1.39, 2.73, 1.73, 2.63, 1.30)
se = c(0.16, 0.24, 0.36, 0.06, 0.25, 0.26, 0.29, 0.05)
df = data.frame(Genotype, Material, Mean, se)
df
最后的数据如下
相当于是用原始数据先算了平均值和标准差
这里的赋值符号他直接用的等于号=,而没有用<-
给因子变量指定水平
代码语言:javascript复制df$Genotype<- factor(df$Genotype, levels=(c("Wildtype", "APOE4")))
df$Material<- factor(df$Material, levels=(c("Shredded paper","Square", "Bedding", "Twist")))
加载需要的R包
代码语言:javascript复制library(ggplot2)
library(ggsignif)
簇状柱形图的代码
代码语言:javascript复制ggplot(df, aes(x=Material, y = Mean, fill=Genotype))
geom_bar(position=position_dodge(), stat="identity")
scale_fill_manual(values=c("#CCCCCC", "#666666", "#CCCCCC", "#666666", "#CCCCCC", "#666666", "#CCCCCC", "#666666"))
geom_errorbar(aes(ymin=Mean-se, ymax=Mean se),
width=0.2,
position=position_dodge(0.9)) #this line of code makes the bar that extends across the 3-nonsig materials
scale_y_continuous(name="Average scoren", expand=c(0,0))
geom_text(aes(x=1, y=5.9, label="Stretch It"), vjust=-1)
scale_x_discrete(name="nProvided materials")
ggtitle(" Averaged scores of nests constructed by wildtype or APOE4 micen") #added chunk of space to center title in jpeg file
theme(plot.title=element_text(lineheight=0.8, face="bold", hjust=0.5))
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
panel.background=element_blank(), axis.line=element_line(color="black")) -> p1
p1
添加显著性标记
代码语言:javascript复制p1
geom_signif(data=df,
aes(xmin=0.75, xmax=1.25, annotations="*", y_position=5.25),
textsize = 5, vjust = 0.05, tip_length = c(0.04, 0.2),
manual=TRUE)
会遇到警告信息Warning message: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position
提示未知参数,但是能够出图
这种还是需要自己用原始数据做统计分析,知道显著性以后再往上添加
另外3组也是一样的方式添加
代码语言:javascript复制p1
geom_signif(data=df,
aes(xmin=0.75, xmax=1.25, annotations="*", y_position=5.25),
textsize = 5, vjust = 0.05, tip_length = c(0.04, 0.2),
manual=TRUE)
geom_signif(data=df,
aes(xmin=1.75, xmax=2.25, annotations="*", y_position=3.30),
textsize = 5, vjust = 0.05, tip_length = c(0.04, 0.3),
manual=TRUE)
geom_signif(data=df,
aes(xmin=2.75, xmax=3.25, annotations="*", y_position=3.30),
textsize = 5, vjust = 0.05, tip_length = c(0.04, 0.2),
manual=TRUE)
geom_signif(data=df,
aes(xmin=3.75, xmax=4.25, annotations="*", y_position=3.30),
textsize = 5, vjust = 0.05, tip_length = c(0.04, 0.3),
manual=TRUE)
最后是添加第一组和另外三组的显著性标记
代码语言:javascript复制p1
geom_signif(data=df,
aes(xmin=0.75, xmax=1.25, annotations="*", y_position=5.25),
textsize = 5, vjust = 0.05, tip_length = c(0.04, 0.2),
manual=TRUE)
geom_signif(data=df,
aes(xmin=1.75, xmax=2.25, annotations="*", y_position=3.30),
textsize = 5, vjust = 0.05, tip_length = c(0.04, 0.3),
manual=TRUE)
geom_signif(data=df,
aes(xmin=2.75, xmax=3.25, annotations="*", y_position=3.30),
textsize = 5, vjust = 0.05, tip_length = c(0.04, 0.2),
manual=TRUE)
geom_signif(data=df,
aes(xmin=3.75, xmax=4.25, annotations="*", y_position=3.30),
textsize = 5, vjust = 0.05, tip_length = c(0.04, 0.3),
manual=TRUE)
geom_signif(data=df,
aes(xmin=1, xmax=3, annotations="**", y_position=5.7),
textsize = 5, vjust = -0.0000025, tip_length = c(0.04, 0.35),
manual=TRUE)
geom_segment(aes(x = 2, y = 3.98, xend = 4, yend = 3.98))
里geom_segment()
函数起到的作用和annotate()
函数是差不多了,代码比较繁琐,而且还需要额外单独做显著性分析。还需要仔细学习下geom_signif()
函数的用法,学习如何在簇状柱形图的基础上自动添加显著性检验的结果