ropensci / iheatmapr

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

"add_row_annotation" , how to assign custom colors #22

Open josruirod opened 6 years ago

josruirod commented 6 years ago

Hello, I'm writing because I've been using iheatmapr a lot with very good results, but lately I've been struggling to assign custom colors to add_row_annotation. I've been using: add_row_annotation(data_heatmap, data.frame("Clusters" = data_df$cluster), side = "left", colors = list("Clusters"=c("darkcyan","purple", "darkgreen","darkred", "black","gray","orange")))

Where data_heatmap is a nice iheatmap, with 4 columns, and data_df$cluster contains 6 group names (one per row of the heatmap, length is nrow) I've been plotting like this without any problem, but lately I have encountered an issue. Colors of the groups are taken from the color list but not in the order I desire. I cannot get this to work, even assigning the names of the group to each color in the vector. Also, when using some color names these are not recognized nor used.

Now, cheking the documentation, I've seen the color argument explained as "list of color palettes, with one color per annotation column name". So I've been probably using this feature wrong, giving it color names instead of palettes and thinking about my custom groups instead of the columns (and maybe it was working previously because I was using 4 groups and I had 4 columns in the heatmap, and now that I have 4 columns and 6 custom groups is not working)

Being able to assign custom colors to the row annotation would be quite important to me, as I'm adding subplots and column annotation using my custom groups. Hopefully you'll have some insight about this

Thank you so much for your support!

AliciaSchep commented 6 years ago

Hi @josruirod, what are the values of data_df$cluster? Are they numbers, strings, or factors? I would recommend setting as factors, and making sure order of the levels is the ordering you want. Otherwise the code snippet looks right as far as I can tell (ok to give vector of colors instead of palette). If the clusters are character vector, they will get converted to factor and the default factor order will control the ordering... by doing the conversion to factor yourself, you have the opportunity to control the levels and order (see forcats package for useful functions for working with factors).

josruirod commented 6 years ago

Thank you for your suggestions. data_df$cluster were stings. Converting the strings to factors was indeed the answer. I've been able to make it work

Thank you for your time