按元数据着色稀疏曲线(纯素包)(phyloseq包)

时间:2017-11-11 05:46:32

标签: r vegan phyloseq

第一次提问者在这里。我无法在其他帖子中找到这个问题的答案(love stackexchange,btw)。

总之... 我通过素食包创建了一条稀疏曲线,我得到了一个非常混乱的情节,在情节的底部有一个非常厚的黑条,这掩盖了一些低多样性的样本线。 理想情况下,我想生成一个包含所有线条的图(169;我可以将其减少到144)但是制作一个复合图形,按样本年着色并为每个池塘制作不同类型的线条(即:2个样本年份: 2016年,2017年和3个池塘:1,2,5)。我使用了phyloseq来创建一个包含我所有数据的对象,然后将我的OTU丰度表与我的元数据分离成不同的对象(jt = OTU表和sampledata =元数据)。我目前的代码:

 jt <- as.data.frame(t(j)) # transform it to make it compatible with the proceeding commands
rarecurve(jt
          , step = 100
          , sample = 6000
          , main = "Alpha Rarefaction Curve"
          , cex = 0.2
          , color = sampledata$PondYear)

# A very small subset of the sample metadata
                  Pond    Year
F16.5.d.1.1.R2     5      2016
F17.1.D.6.1.R1     1      2017
F16.1.D15.1.R3     1      2016
F17.2.D00.1.R2     2      2017

enter image description here

1 个答案:

答案 0 :(得分:0)

以下是如何使用ggplot绘制稀疏曲线的示例。我使用了bioconductor提供的phyloseq包中提供的数据。

安装phyloseq:

source('http://bioconductor.org/biocLite.R')
biocLite('phyloseq')
library(phyloseq)

需要其他库

library(tidyverse)
library(vegan)

数据:

mothlist <- system.file("extdata", "esophagus.fn.list.gz", package = "phyloseq")
mothgroup <- system.file("extdata", "esophagus.good.groups.gz", package = "phyloseq")
mothtree <- system.file("extdata", "esophagus.tree.gz", package = "phyloseq")
cutoff <- "0.10"
esophman <- import_mothur(mothlist, mothgroup, mothtree, cutoff)

提取OTU表,转置并转换为数据框

otu <- otu_table(esophman)
otu <- as.data.frame(t(otu))
sample_names <- rownames(otu)

out <- rarecurve(otu, step = 5, sample = 6000, label = T)

现在你有一个列表,每个元素对应一个样本:

稍微清理一下清单:

rare <- lapply(out, function(x){
  b <- as.data.frame(x)
  b <- data.frame(OTU = b[,1], raw.read = rownames(b))
  b$raw.read <- as.numeric(gsub("N", "",  b$raw.read))
  return(b)
})

标签列表

names(rare) <- sample_names

转换为数据框:

rare <- map_dfr(rare, function(x){
  z <- data.frame(x)
  return(z)
}, .id = "sample")

让我们看看它的样子:

head(rare)
  sample       OTU raw.read
1      B  1.000000        1
2      B  5.977595        6
3      B 10.919090       11
4      B 15.826125       16
5      B 20.700279       21
6      B 25.543070       26

用ggplot2绘图

ggplot(data = rare)+
  geom_line(aes(x = raw.read, y = OTU, color = sample))+
  scale_x_continuous(labels =  scales::scientific_format())

enter image description here

素食主义情节:

rarecurve(otu, step = 5, sample = 6000, label = T) #low step size because of low abundance

enter image description here

可以根据该列添加一组分组和颜色。

以下是添加其他分组的示例。让我们假设您有一个表格:

groupings <- data.frame(sample = c("B", "C", "D"),
                       location = c("one", "one", "two"), stringsAsFactors = F)
groupings
  sample location
1      B      one
2      C      one
3      D      two

其中样本根据另一个特征进行分组。您可以使用lapplymap_dfr覆盖groupings$sample并标记rare$location

rare <- map_dfr(groupings$sample, function(x){ #loop over samples
  z <- rare[rare$sample == x,] #subset rare according to sample 
  loc <- groupings$location[groupings$sample == x] #subset groupings according to sample, if more than one grouping repeat for all
  z <- data.frame(z, loc) #make a new data frame with the subsets
  return(z)
})

head(rare)
  sample       OTU raw.read loc
1      B  1.000000        1 one
2      B  5.977595        6 one
3      B 10.919090       11 one
4      B 15.826125       16 one
5      B 20.700279       21 one
6      B 25.543070       26 one

让我们从这个

中获得一个体面的情节
ggplot(data = rare)+
  geom_line(aes(x = raw.read, y = OTU, group = sample, color = loc))+
  geom_text(data = rare %>% #here we need coordinates of the labels
              group_by(sample) %>% #first group by samples
              summarise(max_OTU = max(OTU), #find max OTU
                        max_raw = max(raw.read)), #find max raw read
              aes(x = max_raw, y = max_OTU, label = sample), check_overlap = T, hjust = 0)+
  scale_x_continuous(labels =  scales::scientific_format())+
  theme_bw()

enter image description here