DN1
DN1

Reputation: 218

How to create an interactive heatmaply plot with custom text in R?

I have a dataset where I am plotting a heatmap to compare 7 groups. I also have in the data 2 columns with information that I want to include as hover text in an interactive heat map.

My data is 7 columns of groups I want to compare, and 2 columns of hover text information I want to add to my plot. The rows are log p-values that I am looking to compare the significance of between the groups.

Currently I am trying to use heatmaply to plot this but I'm having trouble setting the custom text - is there a way to set columns of data into the custom_text of heatmaply()? I can't find any examples that do this specifically.

Input example data:

df <- structure(list(Group1 = c(9.420318259, 5.801092847, 
4.890727291, 4.589825753, 4.836092781), Group2 = c(14.57805564, 
8.798453748, 7.982599836, 7.951599435, 10.81418654), Group3 = c(14.49131554, 
7.975284646, 8.258878348, 7.922657108, 13.3205827), Group4 = c(11.44447147, 
6.208332721, 6.529806574, 4.882623805, 10.69676399), Group5 = c(22.86835197, 
10.94297858, 7.197041788, 9.237584441, 12.70083108), Group6 = c(10.62687539, 
6.458410247, 7.461916094, 6.308454021, 12.39464562), Group7 = c(11.09404106, 
6.420303272, 6.821000583, 5.0727153, 11.13903127), Gene_Overlap = c(37L, 
14L, 14L, 13L, 22L), Mean_GB_Score = c(0.798, 0.788, 0.81, 0.879, 
0.805)), row.names = c("Cardiac Hypertrophy", 
"Cellular Effects of Adrenaline", "Metastasis Signaling", 
"Hormone Signaling", "Estrogen Receptor Signaling"
), class = "data.frame")

Code I'm trying to use:

groups <- as.matrix(df[,1:7])

heatmaply(groups, custom_hovertext = df[[Gene_Overlap]], scale_fill_gradient_fun = ggplot2::scale_fill_gradient2(
  low = "pink", 
  high = "red"))

Upvotes: 0

Views: 872

Answers (1)

Iaroslav Domin
Iaroslav Domin

Reputation: 2718

In the description for custom_hovertext parameter you can read that it should be a matrix of the same dimensions as the input, i.e. a matrix with 5 rows and 7 columns.

So first we would need to construct such matrix:

library(dplyr)

labels <- 
  df %>% 
  mutate(label = paste(
    "Gene Overlap:", Gene_Overlap,
    "\nMean_GB_Score:", Mean_GB_Score
  )) %>% 
  # this writes contens of the new `label` column
  # in place of all the Group columns
  transmute(across(Group1:Group7, ~label)) %>% 
  as.matrix()

And then we can use it in heatmaply

library(heatmaply)
heatmaply(
  groups,
  custom_hovertext = labels,
  scale_fill_gradient_fun = ggplot2::scale_fill_gradient2(low = "pink", high = "red")
)

enter image description here

Upvotes: 1

Related Questions