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

Python012

使用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()

箱形图(Box-plot)是一种用作 显示一组数据分散情况 的统计图,因形状如箱子而得名。除了生信领域,该图在其他领域也经常被使用。主要用于反映原始数据分布的特征,并且可以进行多组数据分布特征的比较。箱形图能显示出一组数据的 最大值(Maximum)、最小值(Minimum)、中位数(Median)及上下四分位数(1st/3rd Quartile) ,同时还可以显示 逸出值(Outlier)

那么,这些值是如何被计算出来的呢?什么样的数据会被判定为逸出值呢?

第一四分位数(Q1) ,又称 较小四分位数 ,等于该样本中所有数值由小到大排列后第25%的数字。

第二四分位数 ,又称 中位数 ,等于该样本中所有数值由小到大排列后第50%的数字。

第三四分位数(Q3) 又称 较大四分位数 ,等于该样本中所有数值由小到大排列后第75%的数字。

逸出值 ,是根据四分位间距(interquartile range)进行计算的:

四分位间距 = Q3-Q1 = ΔQ

在区间 Q3+1.5ΔQ, Q1-1.5ΔQ 之外的值即被视为逸出值。

(1) 需要什么格式的数据

我们需要的数据只要两列,一列为x,一列为y。本次我们使用R中提供的iris数据。

这个数据共有5列,分别为花萼长度(Sepal.Length)、花萼宽度(Sepal.Width)、花瓣长度(Petal.Length)、花瓣宽度(Petal.Width)以及物种(Species)。

比如我们想要探究不同物种的花萼长度差异。

(2) 如何使用ggplot2做箱形图

利用ggplot2画图的核心命令是 geom_boxplot 。我们先来尝试做一个最最基础也是最丑的boxplot图。

可以看到不同的物种之间Sepal.Length有所不同,那么这种差异显著吗?

这个时候我们就需要做检验,那么如何可以直接把检验结果展示在图中呢。这个时候我们可以使用另一个 R包ggpubr

同时如果你的检验是成对的那么可以加上参数 paired=T ,如果你不想用默认的wilcox.test你可以将method改为其他的。比如我想要做一个成对的t检验:

stat_compare_means(aes(label = ..p.signif..),method="t.test",paired=T)

可以发现这里我没有添加comparisons参数,那么结果就是看三组是否存在两组间有显著差异。

具体的大家可以使用命令 ?stat_compare_means 查看帮助手册。

然后我们还可以修改颜色等等。

这样,一张简洁的Boxplot图就完成啦。