美文网首页
跟着Nature microbiology学作图:R语言ggpl

跟着Nature microbiology学作图:R语言ggpl

作者: 小明的数据分析笔记本 | 来源:发表于2021-10-30 19:44 被阅读0次

    论文

    Evolutionary origins of the SARS-CoV-2 sarbecovirus lineage responsible for the COVID-19 pandemic

    image.png

    本地文件 s41564-020-0771-4.pdf

    代码和数据下载链接

    https://github.com/plemey/SARSCoV2origins

    今天的推文我们来重复一下论文中的 Figure 2 中的簇状柱形图

    image.png

    之前的推文也介绍过R语言ggplot2做簇状柱形图的代码,这个图和之前的比较常规的簇状柱形图的区别在于多了一个贴近坐标轴的分组线段,今天的推文主要介绍的是这个的实现办法

    有一个R包是ggh4x,这里有函数可以直接控制坐标轴的范围

    没有找到原文的原始数据,直接自己随便构造一个数据

    image.png

    加载需要用到的R包

    library(readxl)
    library(ggplot2)
    library(ggh4x)
    
    

    读取数据

    
    df<-read_excel("Figure2.xlsx")
    df
    

    最普通的簇状柱形图

    ggplot(data=df,aes(x=x,y=y))+
      geom_col(aes(fill=group),
               position = position_dodge(0.4),
               width = 0.3)
    
    image.png

    设置柱子的先后顺序

    df$x<-factor(df$x,
                 levels = c("NTD","CTD","Variable loop","S1 3 end","S2"))
    
    df$group<-factor(df$group,
                     levels = c("Pangolin Guangdong 2019",
                                "Pangolin Guangxi 2017 (P5L)",
                                "Yunnan 2013 (RaTG13)"))
    

    简单美化

    ggplot(data=df,aes(x=x,y=y))+
      geom_col(aes(fill=group),
               position = position_dodge(0.4),
               width = 0.3,
               show.legend = F)+
      scale_y_continuous(expand = c(0,0),
                         limits = c(-0.1,1))+
      theme_minimal()+
      theme(panel.grid = element_blank(),
            axis.line.y = element_line(),
            axis.ticks.y = element_line())+
      labs(x="loop removed",
           y="Genetic distance from human\nSARS-CoV-2\n(substitutions per site)")+
      annotate(geom = "segment",
               x=0.6,y=0,xend = 5.4,yend = 0)+
      scale_fill_manual(values = c("#ff7a17",
                                   "#808284",
                                   "#1db280"))+
      # theme(#plot.margin = unit(c(0.5,0.1,2,0.5),"cm"),
      #       
      #       axis.text.x = element_text(margin = 
      #                                    margin(2,0,0,0,unit = "cm")))+
      guides(y=guide_axis_truncated(trunc_lower = 0,
                                    trunc_upper = 1)) -> p1
    p1
    
    image.png

    最后是添加表示分组的柱子

    
     p1+ annotate(geom = "segment",
               x=0.6,xend=1.4,
               y=-0.05,yend = -0.05,
               size=5,
               color="#c2abd3")+
      annotate(geom = "segment",
               x=1.6,xend=2.4,
               y=-0.05,yend = -0.05,
               size=5,
               color="#d899be")+
      annotate(geom = "segment",
               x=2.6,xend=3.4,
               y=-0.05,yend = -0.05,
               size=5,
               color="#d5d6e9")+
      annotate(geom = "segment",
               x=3.6,xend=4.4,
               y=-0.05,yend = -0.05,
               size=5,
               color="#d899be")+
      annotate(geom = "segment",
               x=4.6,xend=5.4,
               y=-0.05,yend = -0.05,
               size=5,
               color="#f8a5c9")
    
    image.png

    欢迎大家关注我的公众号

    小明的数据分析笔记本

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

    相关文章

      网友评论

          本文标题:跟着Nature microbiology学作图:R语言ggpl

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