跟著Nature Communications學(xué)作圖:R語(yǔ)言ggplot2平滑曲線折線圖

論文

A highly conserved core bacterial microbiota with nitrogen-fixation capacity inhabits the xylem sap in maize plants

https://www.nature.com/articles/s41467-022-31113-w

本地pdf s41467-022-31113-w.pdf

數(shù)據(jù)代碼鏈接

https://github.com/PlantNutrition/Liyu

今天的推文我們重復(fù)一下論文中的Figure2e

image.png

關(guān)于平滑曲線,之前的推文有過(guò)介紹,可以參考

我們看下這個(gè)論文里是用什么代碼來(lái)實(shí)現(xiàn)的

示例數(shù)據(jù)集部分截圖

image.png

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

top_genus <- read.delim("data/20220616/top_genus.txt",
                       header=T, 
                       row.names=1, 
                       sep="\t",
                       stringsAsFactors = FALSE,
                       check.names = FALSE)

賦予因子水平

top_genus$Genus<-factor(
  top_genus$Genus,
  levels=c("Bacillus","Cronobacter",
           "Unclassified_Enterobacterales",
           "Klebsiella","Pantoea",
           "Pseudomonas","Rosenbergiella"), 
  labels = c("Bacillus","Cronobacter",
             "Unclassified_Enterobacterales",
             "Klebsiella","Pantoea",
             "Pseudomonas","Rosenbergiella"))

準(zhǔn)備配色

phy.cols <- c("#85BA8F", "#A3C8DC",
              "#349839","#EA5D2D",
              "#EABB77","#F09594","#2072A8")

普通折線圖的代碼

library(ggplot2)
p=ggplot(data=top_genus,
         aes(x=Compartment,y=RA,
             group=Genus,colour=Genus))+
  geom_point(size=3)+
  labs(x="Compartments", y="Relative abundance (%)")+
  geom_line()+
  scale_x_discrete(limits=c("RS","RE","VE","SE","LE","P","BS"))+
  scale_colour_manual(values=phy.cols) 
p
image.png

論文中提供的代碼沒(méi)有用BS的數(shù)據(jù),所以會(huì)有警告信息

image.png

平滑曲線他用到的是 ggalt包中的 geom_xspline函數(shù)

library(ggalt)
p2<-ggplot(data=top_genus,aes(x=Compartment,y=RA,
                              group=Genus,colour=Genus))+
  geom_point(size=3)+
  labs(x="Compartments", y="Relative abundance (%)")+
  geom_xspline(spline_shape = -0.5)+
  scale_x_discrete(limits=c("RS","RE","VE","SE","LE","P"))+
  scale_colour_manual(values=phy.cols) 
p2
image.png

拼圖

mytheme = theme_bw() +
  theme(axis.text.x = element_text(size = 8),axis.text.y = element_text(size = 8))+
  theme(axis.title.y= element_text(size=12))+theme(axis.title.x = element_text(size = 12))+
  theme(legend.title=element_text(size=5),legend.text=element_text(size=5))+theme(legend.position = "bottom")


library(patchwork)

p+mytheme + p2 + mytheme +
  plot_layout(guides = "collect")+
  plot_annotation(theme = theme(legend.position = "bottom"))
image.png

示例數(shù)據(jù)和代碼可以到論文中去下載,或者直接在公眾號(hào)后臺(tái)留言20220616獲取

歡迎大家關(guān)注我的公眾號(hào)

小明的數(shù)據(jù)分析筆記本

小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1、R語(yǔ)言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡(jiǎn)單小例子;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)、基因組學(xué)、群體遺傳學(xué)文獻(xiàn)閱讀筆記;3、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!

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

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

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