我有一系列向量,我想迭代地打印它们(到目前为止,我做了硬编码,但我想避免再次逐一进行)。 包含向量名称的变量的名称存储在“genes”(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))
“颜色”功能是根据值
指定颜色 但是,我不能让这个工作。它不会打印所有图形,只会打印几个,但它不会应用正确的颜色。谢谢:)
答案 0 :(得分:3)
如果您想制作这些类型的图表,我建议您切换到ggplot2
(或lattice
)。这有facet_wrap
和facet_grid
选项来处理这类事情。如果没有您身边可重现的示例,很难从ggplot2
用户文档中提供更多示例:
p <- qplot(price, data = diamonds, geom = "histogram", binwidth = 1000)
p + facet_wrap(~ color)
答案 1 :(得分: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
由于你说的两个缺失的载体。这是你期望的吗?