花瓣图

作者: 医学小白学生信 | 来源:发表于2021-01-27 15:19 被阅读0次

    花瓣图需要准备如下的文件

    flower.txt
    #读入作图文件,预处理,获取样本名称,统计共有元素个数等
    flower_dat <- read.delim('flower.txt', header = T, sep = '\t', stringsAsFactors = F, check.names = F)
    sample_id <- colnames(flower_dat)
    otu_id <- unique(flower_dat[,1])
    otu_id <- otu_id[otu_id != '']
    core_otu_id <- otu_id
    otu_num <- length(otu_id)
    
    for (i in 2:ncol(flower_dat)) {
      otu_id <- unique(flower_dat[,i])
      otu_id <- otu_id[otu_id != '']
      core_otu_id <- intersect(core_otu_id, otu_id)
      otu_num <- c(otu_num, length(otu_id))}
    core_num <- length(core_otu_id)
    
    #下文中绘制椭圆的draw.ellipse()、draw.circle()等命令,需要plotrix包实现
    library(plotrix)
    
    #定义备选颜色(示例数据20个样本,所以我设置了20种颜色)
    ellipse_col <- c('#6181BD4E','#F348004E','#64A10E4E','#9300264E','#464E044E','#049a0b4E','#4E0C664E','#D000004E','#FF6C004E','#FF00FF4E','#c7475b4E','#00F5FF4E','#BDA5004E','#A5CFED4E','#f0301c4E','#2B8BC34E')
    
    #构建作图函数(参考自大神博客 https://www.cnblogs.com/xudongliang/p/7884667.html)
    flower_plot <- function(sample, otu_num, core_otu, start, a, b, r, ellipse_col, circle_col) {
      par( bty = 'n', ann = F, xaxt = 'n', yaxt = 'n', mar = c(1,1,1,1))
      plot(c(0,10),c(0,10),type='n')
      n   <- length(sample)
      deg <- 360 / n
      res <- lapply(1:n, function(t){
        draw.ellipse(x = 5 + cos((start + deg * (t - 1)) * pi / 180),
                     y = 5 + sin((start + deg * (t - 1)) * pi / 180),
                     col = ellipse_col[t],
                     border = ellipse_col[t],
                     a = a, b = b, angle = deg * (t - 1))
        text(x = 5 + 2.5 * cos((start + deg * (t - 1)) * pi / 180),
             y = 5 + 2.5 * sin((start + deg * (t - 1)) * pi / 180),
             otu_num[t])
        
        if (deg * (t - 1) < 180 && deg * (t - 1) > 0 ) {
          text(x = 5 + 3.3 * cos((start + deg * (t - 1)) * pi / 180),
               y = 5 + 3.3 * sin((start + deg * (t - 1)) * pi / 180),
               sample[t],
               srt = deg * (t - 1) - start,
               adj = 1,
               cex = 1
          )
        } else {
          text(x = 5 + 3.3 * cos((start + deg * (t - 1)) * pi / 180),
               y = 5 + 3.3 * sin((start + deg * (t - 1)) * pi / 180),
               sample[t],
               srt = deg * (t - 1) + start,
               adj = 0,
               cex = 1
          )
        }           
      })
      draw.circle(x = 5, y = 5, r = r, col = circle_col, border = NA)
      text(x = 5, y = 5, paste('Core:', core_otu))
    }
    
    #调用上述函数作图,视情况修改参数
    png('flower.png', width = 1500, height = 1500, res = 200, units = 'px')
    flower_plot(sample = sample_id, otu_num = otu_num, core_otu = core_num,
                start = 90, a = 0.5, b = 2, r = 1, ellipse_col = ellipse_col, circle_col = '#00AAFFFF')
    dev.off()
    #注:参数a和b用于设置花瓣椭圆的尺寸,ellipse_col用于设置花瓣椭圆的颜色;参数r用于设置中心圆圈尺寸,circle_col用于设置中心圆圈的颜色
    
    

    相关文章

      网友评论

          本文标题:花瓣图

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