ropensci / iheatmapr

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

Is it possible to add cluster labels for groups or clusters outside of the color bar? #105

Closed mcsimenc closed 3 weeks ago

mcsimenc commented 1 month ago

For example (the dendrogram can be ignored),

image

Thank you!

mschilli87 commented 1 month ago

Could you provide some code with a test data set along what you get and what you want to add?

mcsimenc commented 1 month ago

Sure,

dataMtx = matrix(
  c(-0.24042198, -0.4827243, -0.5890574,
    -0.44618020, -0.2229746, -0.6070312,
    -0.36496995, -1.0019127, -0.6124369,
    -0.19344543, -0.3034678, -0.6152493,
    -0.25864576, -0.3891929, -0.6218928,
    -0.06264825, -0.5457967, -0.6256452,
    -0.28303764, -0.2699953, -0.6343910,
    -0.34085175, -0.6395037, -0.6402852,
    -0.17582419, -0.8963393, -0.6467896,
    0.08234632, -0.3888408, -0.7130511,
    -0.17237632, -0.4480809, -0.7246162,
    -0.49856171, -0.5796032, -0.7276262,
    -0.10212818, -1.0155922, -0.7842204,
    -0.41713489, -0.5469020, -0.8311977,
    -0.61267493, -0.8893021, -0.8315599,
    -0.09966118, -0.8801416, -0.8424582,
    -0.46215186, -0.6212762, -0.8767747,
    -0.24828783, -0.8690041, -0.8928232,
    -0.56224402, -0.5091945, -0.8998705,
    -0.69309404, -1.4366143, -0.9562496), 
  nrow = 20, byrow = TRUE)

iheatmap(dataMtx) %>% add_row_clusters(clusters = c(rep("A", 10), rep("B", 10)))

Gives:

image

What I'd like:

image
srsankhe commented 3 weeks ago

Here's a crude way of doing this, I would assume this would work mostly on Clusters that are provided by the user or with the kmeans clustering method. Might get tricky to do with hclust. One can always play around with the layout of the plot to prettify it

rownames(dataMtx) <- paste("Row:", 1:nrow(dataMtx))
colnames(dataMtx) <- paste("Column:", 1:ncol(dataMtx))

p <- iheatmap(dataMtx) %>% 
  add_row_clustering(
    method = 'kmeans', 
    k = 2,
    name = 'Clusters') %>%
  add_row_labels(
    side = 'right') %>%
  add_col_labels(
    side = 'bottom', 
    textangle = 0)

tickText <- p@plots@listData[['Clusters']]@data

tv1 <- median(which(tickText == 1))
tv2 <- median(which(tickText != 1))
tickVals <- c(tv1, tv2)

p <- add_row_labels(
  p,
  ticktext = unique(tickText),
  tickvals = tickVals,
  side = 'left')

p
Screenshot 2024-06-13 at 9 10 39 AM