如何按因素为素食主义者的稀有曲线中的线条着色?

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

我正在尝试为我的 20 个样本中的每一个绘制稀疏曲线 - 并使用 vegan R 中的 rarecurve 根据各自的处理类型 (5) 为线条着色。

我已将相同处理类型的复制列分组在一起作为一个因素:X80CC,X09CC,X39F,X83F,X1850,并尝试根据此分配颜色并输入到绘图中。但是我无法做到做到这一点 - 所有颜色最终都是随机的。

根据分配的 groups/factors 为这些线条着色的最佳方法是什么?

我在这里没有看到明显的错误吗?

谢谢。

library(vegan)

强制多列=因子:

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"))

治疗=颜色:

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

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")`

数据例如:

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))

我有点不确定这是否是您要查找的内容,因为您的代码不是最有意义的,但我得到的是您想要用组合列绘制 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")

编辑:请求是基于分组的 20 行颜色。

我对 rarecurve 不是很熟悉,所以很可能有比这更好的答案。当你最终需要的是一个按照你想要的颜色的列顺序排列的列表。在下面的代码中,我使用 grepl 进行模式识别,因为分组由列名称中的字符串部分标识,并使用 case_when 匹配所有可能性。 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")