Search code examples
rdplyrplotlyheatmapggplotly

I need a make a heatmap by plotly that shows more information


I'd like to be able to make an R plotly heatmap that in addition to show the information about z, I will have more information which will be text information.

Here's what I mean: I have this data set dataset:

library(plotly)
m <- matrix(c(NA, 1,3, 2, NA, 4, 3, 0, NA), nrow = 3, ncol = 3)

fig <- plot_ly(
    x = c("Epilepsy", "PTSD", "Depression"), y = c("Dizziness", "Slowed Thinking/ Decision-making
", "Hearing/Noise Sensitivity
"),
    z = m, type = "heatmap"
)

This gives me this output: This is a Plotly heatmap

However, I need something more when I click each part I have this variable too:

p <- matrix(c(NA, "XX","YY", "TT", NA, "ST", "PO", "IU", NA), nrow = 3, ncol = 3)

As you can see, this is not numeric and it just provides more information concerning the association.


Solution

  • You could add your matrix p to the text attribute, i.e.

    fig <- plot_ly(
      x = x, 
      y = y,
      z = m, 
      type = "heatmap",
      text = p
    )
    

    complete code

    library(plotly)
    m <- matrix(c(NA, 1,3, 2, NA, 4, 3, 0, NA), nrow = 3, ncol = 3)
    p <- matrix(c(NA, "XX","YY", "TT", NA, "ST", "PO", "IU", NA), nrow = 3, ncol = 3)
    
    fig <- plot_ly(
      x = x, 
      y = y,
      z = m, 
      type = "heatmap",
      text = p
    )
    fig
    

    enter image description here