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

时间:2019-02-06 00:34:58

标签: r vegan

我正在尝试绘制我的20个样本中每个样本的稀疏曲线-并使用rarecurve R中的vegan用相应的处理类型(5)对线进行着色。

我将列进行分组,这些列是与因子X80CC,X09CC,X39F,X83F,X1850相同的处理类型的重复,并尝试以此为基础分配颜色并将其输入到绘图中。但是,我无法实现此目的-并且所有颜色最终都是随机的。

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

在这里看不到明显的错误吗?

谢谢。

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

1 个答案:

答案 0 :(得分:0)

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

enter image description here

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

我对稀有曲线不是很熟悉,因此可能会有比这更好的答案。最后,当您需要时,将按所需颜色的列顺序列出。在下面的代码中,我使用grepl进行模式识别,因为分组是由列名称中的字符串部分标识的,并在匹配所有可能性时使用case_when。如果case_w_x包含与其他某些列名相同的字符,则为case_when时在case_w结尾的TRUE。

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

enter image description here