raivokolde / pheatmap

Pretty heatmaps
225 stars 83 forks source link

Annotation_col Error - subscript out of bounds #65

Closed TimJCooper closed 4 years ago

TimJCooper commented 4 years ago

Test dataset: https://www.dropbox.com/s/hy56owg4mfjuxol/Test_Data.RData?dl=0 Plotting this dataset runs as expected: pheatmap(t(pred.MDSC$scores), fontsize=8) Pheatmap However, if I then attempt to add annotation to it:

  anno<-data.frame(pred.MDSC$labels)
  pheatmap(t(pred.MDSC$scores), fontsize=8, annotation_col = anno)

It returns the error:

Error in annotation_colors[[colnames(annotation)[i]]] : 
  subscript out of bounds

Is this a bug, or am I doing something wrong with the annotation data structure?

sscall commented 2 years ago

I'm having the same issue now and can't figure out why it won't work. Would you mind posting an update to cover how you resolved the issue? Other sources I can find online all recommend ensuring that row names for the annotation and the matrix are equal to one another, but even after doing that, I'm getting the exact same error message.

mcanouil commented 1 year ago

@raivokolde Any chance of an update on this?

Here a reprex:

dta <- iris[, 1:4]
annotation <- iris[, "Species", drop = FALSE]
randoid <- rownames(iris)
pheatmap::pheatmap(
  mat = dta,
  annotation_row = annotation,
  labels_row = randoid
)
#> Error in annotation_colors[[colnames(annotation)[i]]] :
#>   subscript out of bounds
pheatmap::pheatmap(
  mat = dta[rownames(dta), ],
  annotation_row = annotation,
  labels_row = randoid
)
raivokolde commented 1 year ago

The problem here is that the pheatmap expects a matrix with row and column names as an input. Here, it is a data.frame instead. It will be converted to a matrix internally, but for some reason as.matrix(dta[rownames(dta), ]) has rownames and as.matrix(dta) doesn't. Not sure how to adjust the pheatmap code to avoid this problem though.

mcanouil commented 1 year ago

Two choices,