【问题标题】:error while creating density plots创建密度图时出错
【发布时间】:2012-09-03 04:23:20
【问题描述】:

我想创建 45 个样本的密度图,但有些样本将无法形成密度,因为存在缺失值。我遇到的错误是

密度错误.default(assayData(data.lair)$lair.predicted[, i], na.rm = TRUE) : 需要至少 2 个点来选择带宽 自动

有人知道如何在函数中解决这个错误吗?

将数据的名称提取到位列表中:

c("00_11242T1.bmp", "00_7T.bmp", "01_677T.bmp", "106T.bmp", "106TV.bmp", 
"108T.bmp", "108TV.bmp", "124T.bmp", "124TV.bmp", "40T.bmp", 
"4497T.bmp", "44T.bmp", "44TV.bmp", "511T.bmp", "511TV.bmp", 
"514T.bmp", "514TV.bmp", "56T.bmp", "92_11145T.bmp", "94_10917T1.bmp", 
"95_549T.bmp", "97_12714T1.bmp", "97_7999T.bmp", "97_8073T2.bmp", 
"99_2221T.bmp", "99_6669T.bmp", "99_7417T1.bmp", "99_7417T2.bmp", 
"R01_80418T2.bmp", "R01_81197T.bmp", "R02_80456T2.bmp", "R03_80356T.bmp", 
"R03_80586T.bmp", "R04_80227T.bmp", "R04_80577T.bmp", "R04_80584T.bmp", 
"R04_81371T.bmp", "R04_81372T1.bmp", "R04_81449T.bmp", "R05_80479T.bmp", 
"R05_80481T.bmp", "R05_80611T.bmp")

我正在使用的功能:

for( i in (1:ncol(data.lair))) {
  bmp(filename = bitlist[i], width = 1200, height = 1200, units = "px",bg = "white")
  par(mfrow=c(1,2))
  plot(density(assayData(data.lair)$lair.predicted[,i],na.rm=TRUE),main = bitlist[i])
  plot(density(assayData(data.lair)$predicted[,i],na.rm=TRUE),main = bitlist[i])
  cat(i,"\n")
  dev.off()
}

数据可以在这里下载: DATA

【问题讨论】:

  • @mrdwab 我可以在离线时下载它,但我会把数据放在我的公共文件夹中
  • 看来你也在使用一些特殊的包。你用的是什么包?当我尝试加载需要beadarraySNP 的数据时出现一些错误。还有其他人吗?您不能只发布一些复制您的问题的示例数据吗?
  • @mrdwab 哦,对不起,我以为它可以加载数据,包库(“beadarraySNP”)。足以加载数据,因为数据是一个多维数组,我不知道如何给出一个数据集来重现已经给出的数据。

标签: r for-loop plot


【解决方案1】:

获得的错误消息是由于向量包含的值不足。在您的数据中,某些向量仅包含 NA

例如索引的第二个值:

i <- 2
dat <- assayData(data.lair)$lair.predicted[ , i]
any(!is.na(dat))
# [1] FALSE

如果您不为这些向量创建密度图,您可以运行循环。检查向量是否包含足够数量的数据点。这是您的代码稍作修改的版本:

for( i in (1:ncol(data.lair))) {
  bmp(filename = bitlist[i], width = 1200, height = 1200, units = "px",bg = "white")
  if (sum(!(is.na(assayData(data.lair)$lair.predicted[,i]))) > 1) {
    par(mfrow=c(1,2))
    plot(density(assayData(data.lair)$lair.predicted[,i],na.rm=TRUE))  
  } 
  plot(density(assayData(data.lair)$predicted[,i],na.rm=TRUE))  
  cat(i,"\n")
  dev.off()
}

在某些情况下,您只会获得循环中第二个向量的密度图。

【讨论】:

  • 感谢您的回答,有时您没有意识到它可能有多简单 :) 但为什么它只创建 1 个密度图?你知道为什么吗?
  • 我敢打赌,我将位列表中的第一个 I 更改为 J,同时将它用于另一个功能:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
  • 2013-06-29
相关资源
最近更新 更多