ropensci / iheatmapr

Complex, interactive heatmaps in R
https://docs.ropensci.org/iheatmapr
Other
267 stars 35 forks source link

Adding empty annotation causes an error #77

Open halhen opened 3 years ago

halhen commented 3 years ago

Adding a covariate with all NA:s causes an error Error in discrete_colors: x >= 1 is not TRUE when plotting.

The error can either be reproduced by adding an annotation with all NA_character_, or by trying a factor with all NA and no levels (this latter case perhaps being a bit weird, but happened to me from working around #75 ).

A workaround is to make the all-NA variable a factor with a dummy level (see example below). Happily, no legend is printed when I do this.

Reproducible example, tested in v 0.4.12.

library(iheatmapr)

A <- matrix(rnorm(6), ncol = 3)
colnames(A) <- c('S1', 'S2', 'S3')
rownames(A) <- c('G1', 'G2')

annotation_0_levels <- data.frame(var = factor(c(NA, NA, NA), levels = c()))
annotation_1_level <- data.frame(var = factor(c(NA, NA, NA), levels = c('')))

# Works
main_heatmap(A) %>%
  add_col_annotation(annotation_1_level)

# Breaks
main_heatmap(A) %>%
  add_col_annotation(annotation_0_levels)
AliciaSchep commented 3 years ago

Seems like the handling of NAs for these functions needs to be examined and maybe adjusted. What is the behavior you would expect for giving all NA values?

halhen commented 3 years ago

I'd expect the annotation to appear in the plot with all white (NA) colors and the annotation label printed. As for legends/colorbars, I don't have a preference between how the dummy example works (no colors, therefore no legend) or to have a title only legend (for consistency). To save space and since I can argue for it too making sense I'd probably cast a 60:40 vote for the "don't show a legend".