使用R绘制花瓣图_2020-11-10

Python020

使用R绘制花瓣图_2020-11-10,第1张

library(rio)

library(plotrix)

venn_data_index <- list.files(path = "./", pattern = "^venn_data")

venn_data <- import(venn_data_index)

sample_id <- colnames(venn_data)

otu_id <- unique(venn_data[,1])

otu_id <- otu_id[otu_id != '']

core_otu_id <- otu_id

otu_num <- length(otu_id)

## 3.遍历数据文件获取数据

for (i in 2:ncol(venn_data)) {

  otu_id <- unique(venn_data[,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)

ellipse_col <- c('#6181BD4E','#F348004E','#64A10E4E','#9300264E','#464E044E','#049a0b4E','#4E0C664E','#D000004E','#FF6C004E','#FF00FF4E','#c7475b4E','#00F5FF4E','#BDA5004E','#A5CFED4E','#f0301c4E','#2B8BC34E','#FDA1004E','#54adf54E','#CDD7E24E','#9295C14E')

## 5.创建绘图函数:flower_plot()

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 = 'white')

dev.off()

pdf('flower.pdf', width = 15, height = 15)

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 = 'white')

dev.off()

1、r(θ)=a*coskθ或r(θ)=asinkθ。

2、如果k是整数,当k是奇数时那么曲线将会是k个花瓣,当k是偶数时曲线将是2k个花瓣。

3、如果k为非整数,将产生圆盘(disc)状图形,且花瓣数也为非整数。注意:该方程不可能产生4的倍数加2(如2,6,10??)个花瓣。变量a代表玫瑰线花瓣的长度。以上就是matlab中求内外花瓣线的方法。