How to show text from a selected tile in a Plotly heatmap in a coupled datatable upon clicking or hovering?

I made a heatmap of differential gene expression between subclusters of cells from a data.frame diffexp, using ggplot and ggplotly().

enter image description here

Every tile represents the number of genes that are differentially expressed between a pair of subclusters (shown as total in the tooltip).

cluster.heatmap <- 
  ggplot(diffexp) +
  geom_tile(aes(x = cluster_A, y = cluster_B, fill = total, 
    text = paste0("up_genes: ", up_genes)), 
    colour="white", size=0.05) +
  scale_fill_viridis_c(begin=0.1, na.value="grey20") +
  ggtitle("Differentially Expressed Genes Between Subcluster Pairs")

ggplotly(cluster.heatmap)

I added the column which contains the list of up_genes to the tooltip via the text = paste0("up_genes: ", up_genes) parameter (columns diffexp$up_genes, diffexp$down_genes contain the actual lists of genes).

However, as you can see in the image, in most cases, this list is rather large and does not fit in the tooltip.

Therefore, I think the best solution is to have a coupled datatable which shows the list of genes, similar to what is shown here:

enter image description here

This coupled table would show the dataframe row for the pair of cluster_A and cluster_B tile selected.

Any idea how to achieve this?

Dataframe diffexp looks like this:

Thank you.

1 Like