1

我有一系列向量,我想迭代地打印它们(到目前为止,我对它进行了硬编码,但我想避免一次又一次地这样做)。保存向量名称的变量的名称存储在“基因”(char)中,而我可以使用 ls() 查看变量(到目前为止没有什么新东西)每个向量的长度都是可变的,我想打印索引(x 轴)相对于值(y 轴)。我正在下载的数据来自 TCGA 数据库,可以通过他们的 R 包访问。到目前为止我所拥有的是:

library(cgdsr)

#create object

mycgds = CGDS("http://www.cbioportal.org/public-portal/")
genes <- c("AGPAT1", "CDIPT", "CDS1", "CEL", "CEPT1", "DGKA", "LCAT", "LCLAT1", "LPCAT3", "LPIN1", "LPL", "MOGAT3", "PEMT", "PISD", "PLA2G4B", "PLD1","PTDSS1", "PTDSS2", "ATX", "PLA2") # list of my gene names

# get z score about these genes for kidney cancer
data <- getProfileData(mycgds, genes, "kirc_tcga_pub_rna_seq_v2_mrna_median_Zscores", "kirc_tcga_pub_3way_complete" )

data <- t(data)

#set z-value cutoff (equivalent to p<0.05)
i <- -1.96
j <- 1.96

# select only samples that have values lower than i and higher than j

AGPAT1 <- data[1,which(data[1,] < i | data[1,] > j)]
CDIPT <- data[2,which(data[2,] < i | data[2,] > j)]
CDS1 <- data[3,which(data[3,] < i | data[3,] > j)]
CEL <- data[4,which(data[4,] < i | data[4,] > j)]
CEPT1 <- data[5,which(data[5,] < i | data[5,] > j)]
DGKA <- data[6,which(data[6,] < i | data[6,] > j)]
LCAT <- data[7,which(data[7,] < i | data[7,] > j)]
LCLAT1 <- data[8,which(data[8,] < i | data[8,] > j)]
LPCAT3 <- data[9,which(data[9,] < i | data[9,] > j)]
LPIN1 <- data[10,which(data[10,] < i | data[10,] > j)]
LPL <- data[11,which(data[11,] < i | data[11,] > j)]
MOGAT3 <- data[12,which(data[12,] < i | data[12,] > j)]
PEMT <- data[13,which(data[13,] < i | data[13,] > j)]
PISD <- data[14,which(data[14,] < i | data[14,] > j)]
PLA2G4B <- data[15,which(data[15,] < i | data[15,] > j)]
PLD1 <- data[16,which(data[16,] < i | data[16,] > j)]
PTDSS1 <- data[17,which(data[17,] < i | data[17,] > j)]
PTDSS2 <- data[18,which(data[18,] < i | data[18,] > j)]
ATX <- data[19,which(data[19,] < i | data[19,] > j)]
PLA2 <- data[20,which(data[20,] < i | data[20,] > j)]

#setup page for the 18graphs, not 20 cause there are no values for the last two
 par(mfrow= c(3,6)) 

   color <- function(x){
      ifelse(x > 1.96, "red", "green")
    }

    for (k in seq(1,length(ls()),1)) #iterate through ls()
      for(z in seq(1, length(genes),1)) #iterate through my list of genes
        if(genes[z] == ls()[k]) #if they match print them out
          plot(ls()[k], pch=15, col=color(ls()[k]), main =genes[z], ylim=c(-3,30))

“颜色”功能是根据值分配颜色

但是,我无法让它工作。它不会打印所有图形,而只会打印一些图形,并且不会应用正确的颜色。

谢谢 :)

4

2 回答 2

3

如果你想制作这些类型的图组,我真的建议切换到ggplot2(或lattice)。这具有处理此类事情的facet_wrap和选项。facet_grid如果没有您身边的可重复示例,很难给出ggplot2用户文档中的示例:

p <- qplot(price, data = diamonds, geom = "histogram", binwidth = 1000)
p + facet_wrap(~ color)

在此处输入图像描述

于 2013-04-18T12:49:30.943 回答
1

好的,很简单。你错过了get()。尝试

for (k in seq(1,length(ls()),1)) #iterate through ls()
  for(z in seq(1, length(genes),1)) #iterate through my list of genes
    if(genes[z] == ls()[k]) #if they match print them out
      plot(get(ls()[k]), pch=15, col=color(ls()[k]), main =genes[z], ylim=c(-3,30))

与您的确切问题无关,但这是您的循环的更清晰(更快)的版本

for (k in 1:length(ls())) #iterate through ls()
    if(any(geneFound <- genes == ls()[k])) #if they match print them out
      plot(get(ls()[k]), pch=15, col=color(ls()[k]), main =genes[geneFound],
           ylim=c(-3,30))

编辑:

更改col=color(ls()[k])col=color(get(ls()[k]))我得到了这个:

在此处输入图像描述

而这个错误:

Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

由于你说的两个缺失的向量。这是你所期望的吗?

于 2013-04-18T13:36:15.113 回答