Sanshine
Sanshine

Reputation: 615

error while creating density plots

I want to create density plots of 45 samples but some samples will not be able to form a density because there are missing values. The error i`m experiencing is

Error in density.default(assayData(data.lair)$lair.predicted[, i], na.rm = TRUE) : need at least 2 points to select a bandwidth automatically

Does someone knows how to work arround this error within the function.

The names of the data are extracted to bitlist:

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

The function i`m using:

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

The data can be downloaded here: DATA

Upvotes: 2

Views: 7886

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

The obtained error message results from vectors containing not enough values. In your data, some vectors contain NA only.

For example, the second value of the index:

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

You can run your loop, if you don't create density plots for these vectors. Check whether the vectors contain a sufficient amount of data points. This is a slightly modified version of your code:

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

In some cases you will obtain a density plot for the second vector in your loop only.

Upvotes: 3

Related Questions