跟著Eu J Forest Res學(xué)作圖:R語言ggplot2做柱形圖的時候給坐標軸添加分組間隔

今天的推文是回答B(yǎng)站關(guān)注者的一個問題

他的問題是

image.png

我找到論文來看了一下

image.png

圖片是

image.png

今天的推文我們試著來復(fù)現(xiàn)一下這個圖

首先是準備數(shù)據(jù)

沒有找到論文提供的原始數(shù)據(jù),只能手動將其整理到表格里了。

image.png

這里用qq里面的截圖工具識別圖片文字還挺方便的


image.png

完整代碼

library(readxl)
library(ggplot2)

df<-read_excel("abcd.xlsx")
df
df$y<-factor(df$y,
             levels = rev(unique(df$y)))

df$group<-factor(df$group,
                 levels = rev(unique(df$group)))

ggplot(data=df,aes(x=x,y=y,fill=group))+
  geom_bar(stat = "identity",
           position = position_dodge(0.9),
           width = 0.8)+
  scale_fill_manual(values = c("#4472c4","#ed7d31"))+
  geom_text(aes(x+0.2,y,label=x),size=3,
            position = position_dodge(0.9))+
  theme_void()+
  theme(legend.position = "bottom",
        legend.justification = c(0.5,0),
        legend.title = element_blank(),
        legend.key.size = unit(2,'mm'),
        legend.text = element_text(size=10),
        plot.margin = unit(c(1,1,2,1),'mm'))+
  geom_text(aes(x=-0.1,y=y,label=y),hjust=1)+
  xlim(-11,5)+
  geom_vline(xintercept = 0,color="grey")+
  annotate(geom = "segment",
           x=0,xend=-11,y=0.4,yend=0.4,
           color="grey")+
  annotate(geom = "segment",
           x=0,xend=-11,y=2.5,yend=2.5,
           color="grey")+
  annotate(geom = "segment",
           x=0,xend=-11,y=5.5,yend=5.5,
           color="grey")+
  annotate(geom = "segment",
           x=0,xend=-11,y=12.5,yend=12.5,
           color="grey")+
  annotate(geom = "segment",
           x=0,xend=-11,y=16.5,yend=16.5,
           color="grey")+
  annotate(geom = "text",
           x=-11,y=1.5,label="Others",angle=90)+
  annotate(geom = "text",
           x=-11,y=4,label="Provisioning",angle=90)+
  annotate(geom = "text",
           x=-11,y=9,label="Cultural",angle=90)+
  annotate(geom = "text",
           x=-11,y=14.5,label="Regulationg",angle=90) -> p
pdf(file = "outp.pdf",
    width = 14,height = 6,
    family = "serif")
print(p)
dev.off()
image.png

整體的思路就是Y軸的坐標軸標簽去掉,用geom_text()函數(shù)添加文本注釋的辦法作為標簽,這樣有了坐標位置添加橫線表示分組就很方便了

如果需要示例數(shù)據(jù)和代碼的話需要給文章打賞1元,悄悄話備注數(shù)據(jù)代碼,如果你打賞了沒有收到我的回復(fù)數(shù)據(jù)代碼下載鏈接,可以給文章留言催我

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

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

小明的數(shù)據(jù)分析筆記本 公眾號 主要分享:1、R語言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡單小例子;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)、基因組學(xué)、群體遺傳學(xué)文獻閱讀筆記;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)容