R 通过元数据为稀疏曲线着色(纯素包装)(phyloseq包装)
第一次在这里提问。顺便说一句,我在其他帖子中找不到这个问题的答案 无论如何。。。 我通过纯素软件包创建了一条稀疏曲线,我得到了一个非常混乱的图,图的底部有一个非常厚的黑条,它模糊了一些低多样性的采样线。 理想情况下,我想用我所有的第169行生成一个图;我可以将其减少到144,但制作一个合成图,按样本年着色,并为每个池塘绘制不同类型的线,即:2个样本年:2016年、2017年和3个池塘:1,2,5年。我使用phyloseq创建了一个包含所有数据的对象,然后将我的OTU丰度表与元数据分离为不同的对象jt=OTU表和sampledata=metadata。我当前的代码:R 通过元数据为稀疏曲线着色(纯素包装)(phyloseq包装),r,vegan,phyloseq,R,Vegan,Phyloseq,第一次在这里提问。顺便说一句,我在其他帖子中找不到这个问题的答案 无论如何。。。 我通过纯素软件包创建了一条稀疏曲线,我得到了一个非常混乱的图,图的底部有一个非常厚的黑条,它模糊了一些低多样性的采样线。 理想情况下,我想用我所有的第169行生成一个图;我可以将其减少到144,但制作一个合成图,按样本年着色,并为每个池塘绘制不同类型的线,即:2个样本年:2016年、2017年和3个池塘:1,2,5年。我使用phyloseq创建了一个包含所有数据的对象,然后将我的OTU丰度表与元数据分离为不同的对象
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
下面是一个如何使用ggplot绘制稀疏曲线的示例。我使用了bioconductor提供的软件包中的数据 要安装phyloseq:
source('http://bioconductor.org/biocLite.R')
biocLite('phyloseq')
library(phyloseq)
需要其他图书馆
library(tidyverse)
library(vegan)
数据:
用ggplot2绘图
ggplot(data = rare)+
geom_line(aes(x = raw.read, y = OTU, color = sample))+
scale_x_continuous(labels = scales::scientific_format())
素食区:
rarecurve(otu, step = 5, sample = 6000, label = T) #low step size because of low abundance
人们可以根据这一点制作一列额外的分组和颜色
下面是一个如何添加另一个分组的示例。假设您有一个如下表格:
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
让我们把它画成一个像样的情节
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()
哇!那太狡猾了!现在我明白了为什么人们认为R使用起来很痛苦。有没有一种简单的方法可以做到这一点?@Jari Oksanen:vegan的ReReecurve函数有col和lty的参数。如果不打算使用ggplot,可以指定这些参数并手动添加图例。
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
ggplot(data = rare)+
geom_line(aes(x = raw.read, y = OTU, color = sample))+
scale_x_continuous(labels = scales::scientific_format())
rarecurve(otu, step = 5, sample = 6000, label = T) #low step size because of low abundance
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
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()