Search code examples
rplotplotly

How to controll hovertemplate when using `text` as argument


I have a surface I want to display with plotly (it has to be a surface and not a 3D mesh because a 3D mesh is not appropiate so I must pass on a matrix not a data.frame).

For this I want to control the hoverinfo so it won't display the x, y, z coordinated but it does display x * y = ... and x + y = .... However I am encountering a problem with the hovertext argument. Which correctly displays the things I want but also displays the x, y, z coordinates. Also, I can't get "<extra></extra>" to work.

This is attempt so far:

library(plotly)

mat <- 0:10 %*% t(0:10)

plot_ly() |>
  
  add_surface(z = ~mat,
              
              hovertext = paste(
                "X*Y:", mat, 
                "<br>X+Y: ", rep(0:10, each = 11) + rep(0:10, 11),
                "<extra></extra>") |>
                matrix(ncol = 11),
              
              hovertemplate = text)

I would like to know:

  • How can I remove the x, y, z coordinates from the hovertemplate?
  • How can I use "<extra></extra>" argument when using hovertemplate = text?

EDIT: Following @Kat's comment I edited this post to reflect what I learned. Thanks Kat!

Thank you for your help!


Solution

  • Instead of text, use hovertext.

    library(plotly)
    
    mat <- 0:10 %*% t(0:10)
    
    plot_ly() |>
      add_surface(z = ~mat,               
                  hovertext = mat,  # hovertext to use in the template
                  hovertemplate = paste0("X: %{x}<br>Y: %{y}<br>",
                                         "Z: %{z}<br>Whatever: %{hovertext}",
                                         "<extra></extra>"))