美文网首页跟着SCI学作图
跟着Eu J Forest Res学作图:R语言ggplot2做

跟着Eu J Forest Res学作图:R语言ggplot2做

作者: 小明的数据分析笔记本 | 来源:发表于2021-11-07 16:52 被阅读0次

    今天的推文是回答B站关注者的一个问题

    他的问题是

    image.png

    我找到论文来看了一下

    image.png

    图片是

    image.png

    今天的推文我们试着来复现一下这个图

    首先是准备数据

    没有找到论文提供的原始数据,只能手动将其整理到表格里了。

    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()函数添加文本注释的办法作为标签,这样有了坐标位置添加横线表示分组就很方便了

    如果需要示例数据和代码的话需要给文章打赏1元,悄悄话备注数据代码,如果你打赏了没有收到我的回复数据代码下载链接,可以给文章留言催我

    欢迎大家关注我的公众号

    小明的数据分析笔记本

    小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

    相关文章

      网友评论

        本文标题:跟着Eu J Forest Res学作图:R语言ggplot2做

        本文链接:https://www.haomeiwen.com/subject/xrfezltx.html