昨天的推文發(fā)出后底挫,好多人留言問如何添加顯著性標(biāo)記,今天的推文介紹下如何在簇狀柱形圖的基礎(chǔ)上添加顯著性標(biāo)記
添加顯著性標(biāo)記用到的是ggsignif
包中的geom_signif()
函數(shù)
在昨天推文的基礎(chǔ)上脸侥,如果是利用帶重復(fù)的原始數(shù)據(jù)作圖建邓,然后利用geom_signif()
函數(shù)作圖的時候我遇到了報(bào)錯,暫時還不知道如何解決睁枕。等找到原因了再來介紹官边。
查找資料的過程中找到了一篇論文
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
論文里有一幅圖
提供了代碼和數(shù)據(jù),今天的推文來學(xué)習(xí)一下他的代碼是如何寫的
首先是準(zhǔn)備數(shù)據(jù)
他這里是直接通過代碼輸入數(shù)據(jù)外遇,沒有將數(shù)據(jù)存儲到文件里
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
最后的數(shù)據(jù)如下
相當(dāng)于是用原始數(shù)據(jù)先算了平均值和標(biāo)準(zhǔn)差
這里的賦值符號他直接用的等于號=注簿,而沒有用<-
給因子變量指定水平
df$Genotype<- factor(df$Genotype, levels=(c("Wildtype", "APOE4")))
df$Material<- factor(df$Material, levels=(c("Shredded paper","Square", "Bedding", "Twist")))
加載需要的R包
library(ggplot2)
library(ggsignif)
簇狀柱形圖的代碼
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 score\n", 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 mice\n")+ #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
添加顯著性標(biāo)記
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
提示未知參數(shù),但是能夠出圖
這種還是需要自己用原始數(shù)據(jù)做統(tǒng)計(jì)分析跳仿,知道顯著性以后再往上添加
另外3組也是一樣的方式添加
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)
最后是添加第一組和另外三組的顯著性標(biāo)記
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()
函數(shù)起到的作用和annotate()
函數(shù)是差不多了诡渴,代碼比較繁瑣,而且還需要額外單獨(dú)做顯著性分析菲语。還需要仔細(xì)學(xué)習(xí)下geom_signif()
函數(shù)的用法玩徊,學(xué)習(xí)如何在簇狀柱形圖的基礎(chǔ)上自動添加顯著性檢驗(yàn)的結(jié)果
歡迎大家關(guān)注我的公眾號
小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號 主要分享:1、R語言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡單小例子谨究;2恩袱、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)、基因組學(xué)胶哲、群體遺傳學(xué)文獻(xiàn)閱讀筆記畔塔;3、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記鸯屿!