美文网首页R语言做图绘图技巧
ggplot2绘制弦图版物种组成图

ggplot2绘制弦图版物种组成图

作者: R语言数据分析指南 | 来源:发表于2021-09-07 22:19 被阅读0次

    常见的堆砌条形图版物种组成图看腻了,本节来介绍如何绘制弦图版的物种组成图

    加载R包

    library(tidyverse)
    library(circlize)
    

    数据清洗

    df <- read.delim("otu.xls",check.names = F) %>%
      dplyr::rename(otu=`OTU ID`) %>% 
      separate(taxonomy,
               into=c("domain","phylum","class",
                      "order","family","genus","species"),sep=";") %>%
       select(CAKB1:CAIF6,genus) %>% 
       drop_na() %>% 
       rowwise() %>%
       mutate(sum = sum(across(where(is.numeric)))) %>%
       arrange(desc(sum)) %>% 
       head(10) %>% 
       select(-sum) %>% pivot_longer(-genus) %>% 
       mutate_at(vars(-name,-value),~str_split(.,"__",simplify=TRUE)[,2]) %>%
       filter(genus != "uncultured") %>% 
       dplyr::rename(Sample=name) %>% 
       left_join(.,read.delim("group.xls"),by="Sample") %>% 
       select(-Sample) %>% select(genus,Group,value)
    

    定义布局

    circos.par(canvas.xlim=c(-1,1),canvas.ylim=c(-1,1.2),start.degree = 0)
    

    绘制圈圈

    set.seed(1234)
    chordDiagram(df,
                 link.sort = FALSE, 
                 link.decreasing = TRUE, 
                 transparency = 0.1, 
                 annotationTrack = "grid",
                 preAllocateTracks = list(track.height = .1))
    

    添加文本

    for(si in get.all.sector.index()) {
      xlim = get.cell.meta.data("xlim",sector.index = si,track.index = 1)
      ylim = get.cell.meta.data("ylim",sector.index = si,track.index = 1)
      circos.text(mean(xlim), ylim[1],labels = si,sector.index = si,
                  track.index = 1, 
                  facing = "clockwise", 
                  cex=0.8,
                  adj=c(0,.5),
                  niceFacing = T)
    }
    
    circos.clear()
    

    可以看到,绘制成弦图明显高端不少

    相关文章

      网友评论

        本文标题:ggplot2绘制弦图版物种组成图

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