今天要模仿的圖片來自于論文 Core gut microbial communities are maintained by beneficial interactions and strain variability in fish翘单。期刊是 Nature microbiology
重復(fù)的圖片是Figure2中的直方圖
首先是模擬數(shù)據(jù)
直方圖的數(shù)據(jù)相對比較簡單轰胁,只需要準(zhǔn)備一列x和一列y即可
另存為csv格式罗心,存儲到Rstudio的工作目錄下回官。這邊我命名為 example_1.csv
讀入數(shù)據(jù)
df<-read.csv("example_1.csv",header=T)
df
最基本的直方圖
使用geom_col()函數(shù)
ggplot(df,aes(x=x,y=y))+
geom_col()
如果想要讓柱子緊挨著躯舔,去除柱子之間的空白亡电,可以加一個width=1
這個參數(shù)
ggplot(df,aes(x=x,y=y))+
geom_col(width = 1)
為邊框設(shè)置顏色用到的是color
參數(shù)
ggplot(df,aes(x=x,y=y))+
geom_col(width = 1,color="black")
更改柱子的填充顏色用到的是fill
參數(shù)
ggplot(df,aes(x=x,y=y))+
geom_col(width = 1,color="black",fill="grey")
更改x軸和y軸的標(biāo)題疚顷,可以用labs()
函數(shù)
ggplot(df,aes(x=x,y=y))+
geom_col(width = 1,color="black",fill="grey")+
labs(x="Niche width",y="Number of ESVs")
去掉灰色的背景,通過主題函數(shù)theme()
進(jìn)行設(shè)置
ggplot(df,aes(x=x,y=y))+
geom_col(width = 1,color="black",fill="grey")+
labs(x="Niche width",y="Number of ESVs")+
theme(panel.background = element_blank())
加上x軸和y軸的線條
ggplot(df,aes(x=x,y=y))+
geom_col(width = 1,color="black",fill="grey")+
labs(x="Niche width",y="Number of ESVs")+
theme(panel.background = element_blank(),
axis.line = element_line())
添加一條垂直的輔助線栈源,用到的是geom_vline()
函數(shù)
ggplot(df,aes(x=x,y=y))+
geom_col(width = 1,color="black",fill="grey")+
labs(x="Niche width",y="Number of ESVs")+
theme(panel.background = element_blank(),
axis.line = element_line())+
geom_vline(xintercept = 4,lty="dashed",color="red")
添加右側(cè)粉紅色的矩形框挡爵,用到的是geom_rect()
函數(shù)
ggplot(df,aes(x=x,y=y))+
geom_col(width = 1,color="black",fill="grey")+
labs(x="Niche width",y="Number of ESVs")+
theme(panel.background = element_blank(),
axis.line = element_line())+
geom_vline(xintercept = 4,lty="dashed",color="red")+
geom_rect(aes(xmin=4.02,xmax=4.5,ymin=0,ymax=1200),
fill="red",alpha=0.2)
最后是添加一些文本注釋,使用的是annotate()函數(shù)
ggplot(df,aes(x=x,y=y))+
geom_col(width = 1,color="black",fill="grey")+
labs(x="Niche width",y="Number of ESVs")+
theme(panel.background = element_blank(),
axis.line = element_line())+
geom_vline(xintercept = 4,lty="dashed",color="red")+
geom_rect(aes(xmin=4.02,xmax=4.5,ymin=0,ymax=1200),
fill="red",alpha=0.2)+
annotate("text",x=0,y=1250,label="Specialist microbes")+
annotate("text",x=4,y=1250,label="Generalist microbes")
歡迎大家關(guān)注我的公眾號
小明的數(shù)據(jù)分析筆記本