論文是
Environmental factors shaping the gut microbiome in a Dutch population
數(shù)據(jù)和代碼的github主頁鏈接
https://github.com/GRONINGEN-MICROBIOME-CENTRE/DMP
這個也是數(shù)據(jù)代碼的下載鏈接戴甩,可以看目錄結構
https://zenodo.org/record/5910709#.YmAcp4VBzic
今天的推文重復一下論文中的figure2c
論文中提供的代碼自定義和一個很長的 函數(shù)符喝,好像是把統(tǒng)計檢驗和作圖全都綜合到一起了,但是我沒看明白其中統(tǒng)計檢驗的部分甜孤,這里就把作圖代碼單獨整理出來了协饲,統(tǒng)計檢驗的FDR值最后手動添加
首先是讀取數(shù)據(jù)
dfToPlot<-read.csv("dfToPlot.csv")
head(dfToPlot)
給x軸的變量賦予因子水平
dfToPlot$RELATIONSHIP.0 <- factor(dfToPlot$RELATIONSHIP.0,
levels=c("RND.PAIR","PARTNERS","PARENT_CHILD","SIBLINGS"))
這個因子水平主要是控制x軸的先后順序
加載ggplot2
library(ggplot2)
箱線圖
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_boxplot()
抖動散點圖
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_jitter()
小提琴圖
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_violin()
三個圖拼圖
library(ggplot2)
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_boxplot() -> p1
p1
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_jitter() -> p2
p2
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
geom_violin() -> p3
p3
將三個圖疊加到一張圖上
cbPalette <- c("#E69F00", "#CC79A7", "#56B4E9", "#009E73", "#CC79A7", "#F0E442", "#999999","#0072B2","#D55E00")
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
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("Bray-Curtis distance of Species")
最后是手動添加統(tǒng)計檢驗的文字
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
y=BC_Spec,
color=RELATIONSHIP.0))+
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("Bray-Curtis distance of Species")+
#scale_x_discrete(labels=c("A","B","C","D"))+
annotate("segment", x = 1-0.01, y = 1, xend = 2.01,lineend = "round",
yend = 1,size=1,colour="black",arrow = arrow(length = unit(0.02, "npc")))+
annotate("segment", x = 2.01, y = 1, xend = 0.99,lineend = "round",
yend = 1,size=1,colour="black",arrow = arrow(length = unit(0.02, "npc")))+
annotate("text", x=1.5,y=1.01,
label=expression("**"~"FDR"~2.41%*%10^-10),vjust=0)
他這里的雙箭頭的處理方式是把一個單箭頭添加兩次
制作封面圖
p4+p4+scale_color_manual(values = cbPalette[5:8])
今天推文的示例數(shù)據(jù)和代碼可以在公眾號后臺留言
20220505
獲取
歡迎大家關注我的公眾號
小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號 主要分享:1畏腕、R語言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡單小例子;2茉稠、園藝植物相關轉錄組學描馅、基因組學、群體遺傳學文獻閱讀筆記而线;3铭污、生物信息學入門學習資料及自己的學習筆記!