简体   繁体   English

如何通过因素为素食主义者的稀有曲线上色?

[英]How to colour lines in vegan's rarecurve by factors?

Im trying to plot a rarefaction curve for each of my 20 samples - and colour the lines by the respective treatment type (5) using rarecurve in vegan R. 我试图绘制我的20个样本中每个样本的稀疏曲线-并使用vegan R中的rarecurve曲线按相应的处理类型(5)对线条进行rarecurve

I have grouped the columns together that are replicates of the same treatment types as a factor: X80CC,X09CC,X39F,X83F,X1850 , and tried to assign colours based on this and input into the plot.. However I havent been able to achieve this - and all colours end up random. 我将相同处理类型的重复列分组为一个因子: X80CC,X09CC,X39F,X83F,X1850 ,并尝试基于此分配颜色并将其输入到绘图中。但是,我无法实现这-所有颜色最终都是随机的。

What is the best way to colour these lines based on the assigned groups/factors? 根据分配的组/因素为这些线着色的最佳方法是什么?

Is there an obvious error im not seeing here? 我在这里看不到明显的错误吗?

Thank you. 谢谢。

library(vegan)

coerce multiple columns = factors: 强制多列=因素:

X80CC<-as.factor(c("DNA10_prerarefy4$X1939F.1980CC", 
                   "DNA10_prerarefy4$X1939F.1980CC.1", 
                   "DNA10_prerarefy4$X1939F.1980CC.2", 
                   "DNA10_prerarefy4$X1939F.1980CC.3"))

X09CC<-as.factor(c("DNA10_prerarefy4$X1939F.2009CC", 
                   "DNA10_prerarefy4$X1939F.2009CC.1", 
                   "DNA10_prerarefy4$X1939F.2009CC.2", 
                   "DNA10_prerarefy4$X1939F.2009CC.3"))

X39F<-as.factor(c("DNA10_prerarefy4$X1939F", "DNA10_prerarefy4$X1939F.1", 
                  "DNA10_prerarefy4$X1939F.2", "DNA10_prerarefy4$X1939F.3"))

X83F<-as.factor(c("DNA10_prerarefy4$X1939.1983F",  "DNA10_prerarefy4$X1939.1983F.1", 
                  "DNA10_prerarefy4$X1939.1983F.2", "DNA10_prerarefy4$X1939.1983F.3"))

X1850<-as.factor(c("DNA10_prerarefy4$X1850F", "DNA10_prerarefy4$X1850F.1", 
                   "DNA10_prerarefy4$X1850F.2", "DNA10_prerarefy4$X1850F.3"))

treatments = colours: 处理=颜色:

cols <- c("darkred"=X80CC, "forestgreen" = X09CC, "darkblue" = X39F, 
          "pink" = X1850, "orange" = X83F)

OTU_rarefy4<-t(DNA10_prerarefy4)

curve: 曲线:

 rarecurveDNA10 <- rarecurve(OTU_rarefy4, step=1, label=TRUE, col = cols, 
                   xlab = "Sequencing depth (number of reads)", ylab = "No. Fungal OTUs")`

Data eg: 数据例如:

dput(DNA10_prerarefy4)

structure(list(X1939F.1980CC = c(4543L, 2303L, 1877L, 1612L, 1496L, 1198L,
          1116L, 893L, 761L), X1939F.1980CC.1 = c(4400L, 3228L, 9L, 23L, 
          546L, 0L, 946L, 1299L, 263L), X1939F.1980CC.2 = c(1564L, 131L, 0L, 
          0L, 584L, 0L, 914L, 0L, 366L), X1939F.1980CC.3 = c(3903L, 847L, 
          0L, 399L, 1025L, 0L, 898L, 0L, 2126L), X1939F.2009CC = c(4868L, 
          413L, 0L, 0L, 280L, 0L, 655L, 0L, 0L), X1939F.2009CC.1 = c(1703L, 
          143L, 0L, 0L, 142L, 0L, 148L, 0L, 2L), X1939F.2009CC.2 = c(1432L, 
          178L, 0L, 0L, 342L, 0L, 554L, 0L, 68L), X1939F.2009CC.3 = c(1641L, 
          172L, 0L, 1L, 294L, 0L, 194L, 108L, 204L), X1939F = c(3345L, 269L, 
          0L, 0L, 431L, 0L, 605L, 160L, 23L), X1939F.1 = c(1545L, 372L, 5L, 
          0L, 673L, 0L, 432L, 0L, 242L), X1939F.2 = c(4921L, 917L, 0L, 0L, 
          1464L, 0L, 790L, 0L, 782L), X1939F.3 = c(3192L, 302L, 11L, 2820L, 
          528L, 0L, 1113L, 182L, 0L), X1939.1983F = c(5673L, 1589L, 0L, 78L, 
          1123L, 0L, 808L, 3L, 53L), X1939.1983F.1 = c(4653L, 1457L, 0L, 3L, 
          768L, 0L, 1344L, 0L, 579L), X1939.1983F.2 = c(3485L, 498L, 0L, 
          53L, 892L, 0L, 542L, 0L, 390L), X1939.1983F.3 = c(5731L, 369L, 0L, 
          4L, 70L, 0L, 1126L, 0L, 114L), X1850F = c(9393L, 0L, 0L, 0L, 0L, 
          0L, 0L, 0L, 0L), X1850F.1 = c(3007L, 1162L, 0L, 1L, 1049L, 0L, 
          645L, 0L, 138L), X1850F.2 = c(3836L, 1094L, 0L, 1051L, 767L, 0L, 
          683L, 0L, 192L), X1850F.3 = c(6558L, 2367L, 0L, 104L, 1379L, 0L, 
          537L, 0L, 2014L)), class = "data.frame", row.names = c(NA, -9L))

I'm a bit unsure if this is what you are looking for because your code doesn't make the most sense, but what I got was that you want to plot the 5 lines with combined columns. 我不确定这是否是您要查找的内容,因为您的代码最没有意义,但是我得到的是您想用合并的列绘制5行。

# Putting the column names in quotes makes them strings; does not refer to the column and from 
#there using as.factor creates a list of 4 as a factor. Instead just stack the columns into a list.
X80CC <- c(DNA10_prerarefy4$X1939F.1980CC, DNA10_prerarefy4$X1939F.1980CC.1, 
           DNA10_prerarefy4$X1939F.1980CC.2, DNA10_prerarefy4$X1939F.1980CC.3)

X09CC <- c(DNA10_prerarefy4$X1939F.2009CC, DNA10_prerarefy4$X1939F.2009CC.1, 
           DNA10_prerarefy4$X1939F.2009CC.2, DNA10_prerarefy4$X1939F.2009CC.3)

X39F <- c(DNA10_prerarefy4$X1939F, DNA10_prerarefy4$X1939F.1, 
          DNA10_prerarefy4$X1939F.2, DNA10_prerarefy4$X1939F.3)

X83F <- c(DNA10_prerarefy4$X1939.1983F,  DNA10_prerarefy4$X1939.1983F.1, 
          DNA10_prerarefy4$X1939.1983F.2, DNA10_prerarefy4$X1939.1983F.3)

X1850 <- c(DNA10_prerarefy4$X1850F, DNA10_prerarefy4$X1850F.1, DNA10_prerarefy4$X1850F.2,
           DNA10_prerarefy4$X1850F.3)

# Join these row-wise to create a new data frame (and then you don't need to transpose).
OTU_rarefy4 <- cbind(X80CC, X09CC, X39F, X1850, X83F)

# Create a list of colours and they will be plotted in the order of the columns above
cols <- c("darkred", "forestgreen", "darkblue", "pink", "orange")

rarecurveDNA10 <- rarecurve(OTU_rarefy4, step=1, label=TRUE, col = cols, 
                            xlab = "Sequencing depth (number of reads)", ylab = "No. Fungal OTUs")

在此处输入图片说明

EDIT: Request is for 20 lines with colours based on groupings. 编辑:请求是基于分组的20行颜色。

I am not super familiar with rarecurve, and so there might very well be a much better answer than this. 我对稀有曲线并不十分熟悉,因此可能会有比这更好的答案。 When you need in the end is a list in the order of your columns of the colours that you want. 最后,当您需要时,将按所需颜色的列顺序列出。 In the code below I use grepl for pattern recognition since the grouping are identified by string portions in the column names and use case_when for matching all of the possibilities. 在下面的代码中,我使用grepl进行模式识别,因为分组是由列名称中的字符串部分标识的,并在匹配所有可能性时使用case_when。 The TRUE at the end of the case_when accounts for the X39F group since it contains the same characters as some of the other column names. case_when末尾的TRUE表示X39F组,因为它包含与某些其他列名相同的字符。

cols <- c()
for(i in 1:ncol(DNA10_prerarefy4)){
    cols[i] <- case_when(grepl('1980CC', colnames(DNA10_prerarefy4)[i]) ~ 'darkred',
                   grepl('2009CC', colnames(DNA10_prerarefy4)[i]) ~ 'forestgreen',
                   grepl('1983F', colnames(DNA10_prerarefy4)[i]) ~ 'darkblue',
                   grepl('X1850F', colnames(DNA10_prerarefy4)[i]) ~ 'pink',
                   TRUE ~ 'orange')
}

OTU_rarefy4 <- t(DNA10_prerarefy4)
rarecurveDNA10 <- rarecurve(OTU_rarefy4, step=1, label=TRUE, col = cols, 
                        xlab = "Sequencing depth (number of reads)", ylab = "No. Fungal OTUs")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM