跟著Nature microbiology學(xué)畫圖~R語言ggplot2畫柱形圖

今天要模仿的圖片來自于論文 Core gut microbial communities are maintained by beneficial interactions and strain variability in fish。期刊是 Nature microbiology

image.png

今天重復(fù)的圖片是Figure3中的柱形圖


image.png

首先是第一個(gè)小圖:柱形圖,誤差棒,顯著性P值

第一步是模擬數(shù)據(jù)
image.png

數(shù)據(jù)是三列:第一列用來做X,第二列做Y,第三列做誤差條

讀入數(shù)據(jù)
df1<-read.csv("Figure3_d.csv",header=T)
ggplot2基本的柱形圖,利用分組信息填充顏色
library(ggplot2)
ggplot(df1,aes(x=group,y=value))+
  geom_col(aes(fill=group),color="black")
image.png
接下來是簡單地美化,包括調(diào)整配色,擴(kuò)大y軸范圍,去掉灰色背景,柱子貼底,去掉圖例,更改x和y軸的標(biāo)題,添加總標(biāo)題,添加輔助線,添加誤差線等等。

下面的代碼就不詳細(xì)介紹了,爭取錄制一期視頻來介紹

ggplot(df1,aes(x=group,y=value))+
  geom_hline(yintercept = 0.5,lty="dashed")+
  geom_hline(yintercept = 1,lty="dashed")+
  geom_col(aes(fill=group),color="black")+
  theme_bw()+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(),
        plot.title = element_text(hjust=0.5),
        legend.position = "none")+
  scale_y_continuous(expand = c(0,0),
                     limits = c(0,1.5))+
  scale_x_discrete(labels=c("Positive\ninteractions","Negative\ninteractions"))+
  annotate("segment",x=1,y=0.8,xend=1,yend=1)+
  annotate("segment",x=2,y=0.4,xend=2,yend=0.5)+
  labs(x=NULL,
       y="Absolute fold change\nin growth from co-cultures\ncompared to monocultures",
       title = "Average growth fold change in\nco-cultures")+
  annotate("segment",x=1.1,y=1.2,xend=1.9,yend=1.2)+
  annotate("segment",x=1,y=1.15,xend=1.1,yend=1.2)+
  annotate("segment",x=1.9,y=1.2,xend=2,yend=1.15)+
  annotate("text",x=1.5,y=1.3,label="P=0.0006")+
  scale_fill_manual(values = c("#ff8080","#90bff9"))
image.png

接下來是第二個(gè)小圖:有正值和負(fù)值的柱形圖

第一步還是構(gòu)造數(shù)據(jù)
x<-1:28
y<-sample(-100:150,28,replace = F)
df2<-data.frame(x,y)
df2$group<-ifelse(df2$y>0,"A","B")
基本的柱形圖
df2$x<-factor(df2$x)
ggplot(df2,aes(x,y))+
  geom_col(aes(fill=group),color="black")
image.png
接下來是美化:包括去掉背景,更改配色,調(diào)整x軸標(biāo)簽的角度等等
df2$group<-factor(df2$group,
                  labels = c("Synergistic interactions",
                             "Non-synergistic interactions"))
ggplot(df2,aes(x,y))+
  geom_hline(yintercept = -50,lty="dashed")+
  geom_hline(yintercept = 50,lty="dashed")+
  geom_hline(yintercept = 100,lty="dashed")+
  geom_col(aes(fill=group),color="black")+
  theme_bw()+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(),
        axis.text.x = element_text(angle = 90,hjust=0.5,
                                   vjust = 0.5),
        plot.title = element_text(hjust = 0.5),
        legend.position = "bottom",
        legend.title = element_blank())+
  scale_y_continuous(expand=c(0,0),
                     limits=c(-100,150),
                     breaks = c(-100,-50,0,50,100,150))+
  labs(x="Pairwise interactions",
       y="Percentage change from\nmonoculture",
       title = "Synergistic versus non-synergistic\ninteractions")+
  scale_fill_manual(values = c("#ff8080","#90bff9"))
image.png
最后是拼圖
library(cowplot)
pdf("Rplot11.pdf",width = 8,,height = 4)
plot_grid(p1,p2,ncol = 2,nrow=1,labels = c("d","e"))
dev.off()
image.png

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

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容