For循环分配变量,而我想用qplot(R)绘制一些东西

时间:2016-06-28 12:49:03

标签: r ggplot2

所以我有以下for循环:

for (Count in 1:19){
  png(paste0(colnames(fdd$rawCounts)[Count], ".pdf"))
  qplot(y = log2(fdd$rawCounts[,Count]), main = colnames(fdd$rawCounts)[Count])
  dev.off()
}

这应该简单地绘制一些计数数据,我从这里开始:

structure(c(11L, 3L, 12L, 8L, 15L, 2L, 5L, 2L, 8L, 7L, 6L, 10L, 
6L, 1L, 7L, 4L, 2L, 1L, 3L, 0L, 4L, 4L, 2L, 5L, 8L, 0L, 13L, 
4L, 10L, 7L, 2L, 1L, 2L, 4L, 7L, 7L, 14L, 4L, 25L, 17L, 14L, 
16L, 4L, 2L, 5L, 5L, 5L, 2L, 9L, 5L, 11L, 8L, 1L, 4L, 10L, 8L, 
8L, 7L, 9L, 5L, 9L, 15L, 14L, 11L, 16L, 8L, 11L, 4L, 3L, 6L, 
3L, 0L, 6L, 3L, 4L, 6L, 1L, 4L, 11L, 11L, 12L, 6L, 2L, 6L, 7L, 
9L, 22L, 8L, 13L, 7L, 6L, 1L, 4L, 5L, 6L, 2L, 4L, 2L, 6L, 7L, 
3L, 2L, 6L, 3L, 3L, 2L, 5L, 5L, 9L, 2L, 6L, 5L, 4L, 2L), .Dim = c(6L, 
19L), .Dimnames = structure(list(feature = c("chr10:100000001-100000500", 
"chr10:10000001-10000500", "chr10:1000001-1000500", "chr10:100000501-100001000", 
"chr10:100001-100500", "chr10:100001001-100001500"), sample = c("K562_FAIRE_Acla_4hr_1", 
"K562_FAIRE_Acla_4hr_2", "K562_FAIRE_Daun_4hr_1", "K562_FAIRE_Daun_4hr_2", 
"K562_FAIRE_Etop_4hr_1", "K562_FAIRE_Etop_4hr_2", "K562_FAIRE_untreated", 
"FAIRE.seq_K562_2MethylDoxo_A", "FAIRE.seq_K562_2MethylDoxo_B", 
"FAIRE.seq_K562_Ctr_A", "FAIRE.seq_K562_Ctr_B", "FAIRE.seq_K562_Doxo_10uM_4hrs_A", 
"FAIRE.seq_K562_Doxo_10uM_4hrs_B", "FAIRE.seq_K562_Epirubicin_A", 
"FAIRE.seq_K562_Epirubicin_B", "FAIRE.seq_K562_MTX_40uM_4hrs_A", 
"FAIRE.seq_K562_MTX_40uM_4hrs_B", "FAIRE.seq_K562_MTX_5uM_4hrs_A", 
"FAIRE.seq_K562_MTX_5uM_4hrs_B")), .Names = c("feature", "sample"
)))

现在,如果我尝试绘制数据,它会给我一个名为Count的变量,值为19L。虽然我预计会绘制19个地块。为什么会这样?

谢谢!

1 个答案:

答案 0 :(得分:3)

这对我有用:

for (Count in 1:19){
  pdf(paste0(colnames(df)[Count], ".pdf"))
  print(qplot(y = log2(df[, Count]), main = colnames(df)[Count]))
  dev.off()
}

一些变化:

  • 我将您的数据读作df。事实证明它是一个矩阵,所以我相应地调整了子集。
  • 我还将qplot函数包含在print函数中,该函数强制创建数字。
  • 最后,我将png功能切换为pdf,因为这似乎是您尝试根据paste0结果创建的文件。