跟著Nature學(xué)作圖:R語言ggplot2畫誤差線展示廣義線型模型的比值比(odds ratio)

論文是

Environmental factors shaping the gut microbiome in a Dutch population

數(shù)據(jù)和代碼的github主頁鏈接

https://github.com/GRONINGEN-MICROBIOME-CENTRE/DMP

這個也是數(shù)據(jù)代碼的下載鏈接,可以看目錄結(jié)構(gòu)

https://zenodo.org/record/5910709#.YmAcp4VBzic

今天的推文重復(fù)一下論文中的 figureS3c 誤差線圖

image.png

部分示例數(shù)據(jù)集如下

image.png

讀取數(shù)據(jù)集

disease_enterotype<-readr::read_csv("newdataset/FigureS3c.csv")

根據(jù)P值的大小給數(shù)據(jù)集增加一列表示分組

disease_enterotype$Sig=NA
disease_enterotype$Sig[disease_enterotype$Pvalue<0.5]="Significant"
disease_enterotype$Sig[disease_enterotype$Pvalue>=0.5]="Non-Significant"

這里的p值我選擇0.5是因為論文中的數(shù)據(jù)是模擬數(shù)據(jù),沒啥實際意義。自己真實的數(shù)據(jù)集需要酌情考慮

根據(jù)Odds ratio的大小排序

disease_enterotype=disease_enterotype[order(disease_enterotype$OR),]

刪除帶有缺失值的行

disease_enterotype=na.omit(disease_enterotype)

賦予因子水平

disease_enterotype$Disease=factor(disease_enterotype$Disease,levels = disease_enterotype$Disease)

作圖代碼

library(ggplot2)
g <- ggplot(disease_enterotype, aes(x = OR, y = Disease,color=Sig)) + 
  geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed") + 
  geom_errorbarh(aes(xmax = `CI_up`, xmin = `CI_low`), size = .3, 
                 height = 0.2) +
  geom_point(size = 3.5,shape=7) +
  theme_bw()+ theme(panel.grid.minor = element_blank()) +
  ylab("") + xlab("Odds ratio")+scale_color_lancet()
print(g)
image.png

論文中的圖x軸還有一些注釋信息,可以出圖后借助其他軟件編輯,如果先用代碼實現(xiàn)的話可以借助annotate_custom()函數(shù),上一篇推文由關(guān)于這個函數(shù)的介紹,可以找來參考

論文中figureS3的布局,上面已經(jīng)有兩個圖,下面這個圖可能改成水平方向的會美觀一點

g+coord_flip()+
  theme(legend.position = "top",
        axis.text.x = element_text(angle=90,
                                   hjust=1,vjust=0.5))
image.png

示例數(shù)據(jù)和代碼可以在公眾號后臺回復(fù) 20220525 獲取

歡迎大家關(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í)筆記!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容