How to add more data to tooltip in R plotly (besides the `text` argument)

Is there a way to customize the hover text in a contour plot in R plotly using data from sources other that the text argument? Here is a minimal reproducible example:

library(plotly)

x <- y <- 0:100
z <- outer(X = x, Y = y, function(x, y) 2*x + 3*y)
sum <- outer(X = x, Y = y, function(x, y) x + y)
product <- outer(X = x, Y = y, function(x, y) x * y)
difference <- outer(X = x, Y = y, function(x, y) x - y)

plot_ly(
  type = "contour",
  x = x,
  y = y,
  z = z,
  text = sum,
  hovertemplate = "x is %{x}<br>y is %{y}<br>z is %{z}<br>sum is %{text}<extra></extra>"
)

The text argument allows me to display the sum data. I would also like to display the product and difference data; however, I am unsure how to go about it.

The following question has an answer (using the annotations argument), which does not work with the contour trace: https://stackoverflow.com/questions/42170476/custom-hoverinfo-text-on-plotly-r-chart-that-is-different-to-the-text-paramete

This question also has an answer; however, the user relies on ggplot2: https://stackoverflow.com/questions/50024089/r-plotly-map-hover-text-reading-from-2-data-frames

Finally, I learned about the customdata argument, but it requires me to code in JavaScript (https://plotly-r.com/supplying-custom-data.html). There seems to be a way to NOT use JS with this argument, but I only have evidence that it works for sankey diagrams (https://github.com/plotly/plotly.js/pull/4621).

Your help will be very much appreciated.

library(plotly)

x <- y <- 0:100
z <- outer(X = x, Y = y, function(x, y) 2*x + 3*y)
# sum <- outer(X = x, Y = y, function(x, y) x + y)
# product <- outer(X = x, Y = y, function(x, y) x * y)
# difference <- outer(X = x, Y = y, function(x, y) x - y)

txt <- as.character(outer(X = x, Y = y, function(x, y) 
  glue::glue("sum is {x+y}<br>product is {x*y}<br>difference is {x-y}")))

plot_ly(
  type = "contour",
  x = x,
  y = y,
  z = z,
  text = txt,
  hovertemplate = "x is %{x}<br>y is %{y}<br>z is %{z}<br>%{text}<extra></extra>"
)

image

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.