Search code examples
rplotlyr-plotly

Adding hover annotations to a Heatmap plotly R


I'm currently using plotly's heatmap to make a risk matrix (if anyone knows a more efficient way to do it I'd be open to suggestions). Currently I add information about the different risks with annotations but I would like that instead of annotations it was a marker that contains the information when hovering over it, as in the image that I attached below.

Below I attach the code and an image of how I would like it to be.

I'll appreciate any suggestion, advice, etc. Thanks!

df.risk <- data.frame(
  Risk = paste0("R",1:5),
  Prob = runif(5),
  Cons = runif(5,1,5))
  
  m <- c(rep(0.1,4),0.5,rep(0.1,2),rep(0.5,3),0.1,rep(0.5,3),0.9,0.1,rep(0.5,2),rep(0.9,2),rep(0.5,2),rep(0.9,3))
  
  scale <- matrix(m, nrow = 5, ncol = 5)
  
  pal <- c("#1A9641",
           "#EFE90F",
           "#F44336")
  
  fig <- plot_ly(
    x = seq(1,16,by = 4),
    y = seq(0,1,by = 0.2),
    z = scale,
    type = "heatmap",
    colors = colorRamp(pal))
  
  fig <- fig  %>% 
    layout(xaxis = list(title = 'Cons'),
           yaxis = list(title = 'Prob'))
  
fig %>% 
  add_annotations(y = df.risk$Prob,
                  x = df.risk$Cons,
                  text = df.risk$Risk,
                  showarrow = FALSE) %>% hide_colorbar()

Reference


Solution

  • Let me know if this is what you were looking for. I changed your data in df.risk, so that the template was a bit more obvious. Other than that the content leading up to the plot_ly object remained as you presented it in your question.

    The data changes first

    df.risk <- data.frame(
      Risk = rep(c("Product rendered obsolete",
                    "Strict legal requirements",
                    "Sufficient human resources",
                    "Sufficient material resources",
                    "Sufficient manufacturing equipment",
                    "Sufficient sales",
                    "Reliable suppliers",
                    "Unknown/unintended costs exceeding ROI",
                    "Evidence technology will work",
                    "Sufficient market share long term"), 3),
      Prob = runif(10),
      Cons = runif(10,1,5)) 
    

    Then the initial creation of the plot_ly object.

    fig <- plot_ly(
      x = seq(1,16,by = 4),
      y = seq(0,1,by = 0.2),
      z = scale,
      hoverinfo = "none",
      type = "heatmap",
      colors = colorRamp(pal)) %>% 
      add_markers(
        inherit = F,
        x = ~Cons, y = ~Prob,
        data = df.risk,
        showlegend = F,
        text = ~Risk,
        color = I("white"), # I("transparent") or whatever color you prefer
        hovertemplate = paste0("%{text}<br><br>", # risks
                               "Probability: %{y:.0%}<br>", # prob % rounded
                               "Severity: %{x:.2f}", # severity, rounded
                               "<extra></extra>")) # no trace info
    
    fig  %>% 
      layout(xaxis = list(title = 'Consequences/Severity'),
             yaxis = list(title = 'Probability')) %>% hide_colorbar()
    

    This is what it looks like.

    enter image description here enter image description here