This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R - Heatmap for categorical variables (color by row)

Hello, I have a metadata file with many categorical variables. I want to make like a heatmap (using R) so I can show my variables in a more compelling way in a slide for my students.

I tried ComplexHeatmap, but I could not get it to color each row separately.

For example (a smaller version of my metadata):

meta <- data.frame(tissue = c("liver", "liver", "heart, "heart", "lung"),
                               drug = c("PMX", "PMX", "PMX", "Ctr", "Ctr"),
                               age = c("12mo", "12mo", "6mo", "6mo", "6mo"),
                               model = c("mm10", "mm10", "mm10", "mm10", "mm10")

I would like a heatmap in which:

  • each column above (tissue, drug, age) is a row of the heatmap
  • each row would have its own legend
  • each row would have its own set of colors per factor

For example my output would be a heatmap:

  • row 1 (tissue) would have liver in green, heart in red, lung in yellow
  • row 2 (drug) would have PMX in blue and Ctr in gray
  • row3 would have 12mo in pink and 6mo in purple
  • row 4 would be all orange (mm10) If possible all rows separated by a white line, or somthing similar

Can someone please help?

Thank you!

complexheatmap heatmap r

1 answer

Looks like you are trying to plot annotations, you can use HeatmapAnnotation to give each row its own legend and color set, just plot an empty heatmap with the annotation. See here: https://jokergoo.github.io/ComplexHeatmap-reference/book/heatmap-annotations.html

Sorry, I am trying, but I cant plot an empty heatmap. It doesnt work... I am very new to R. Can you please help? Thanks

Sorry, you don't have to draw a heatmap at all, you can draw annotations, for instance:

ha = HeatmapAnnotation(foo = 1:10, bar = c(rep('a',5), rep('b', 5)))
draw(ha)

Log in to answer this question.