Note: Unfortunately, I haven't solved your issue, but hopefully this information is helpful. Good luck!
Background: SVG
plotly
creates plots as Scalable Vector Graphics (<svg>
)
tooltips for <svg>
must be added as a child element named <title>
<title>
should be the first child element of its parent
new <svg>
elements need to be created in the SVG namespace
SVG Tooltip: Rectangle vs. Plotly x-axis
As a proof of concept, I added a <svg>
rectangle to your example. Using JS, I then added a <title>
element as the first child of the <rect>
element, which produces a tooltip on the rectangle.

However, following the same steps for the plotly
heatmap does not produce a tooltip on the x-axis title, although a <title>
element is added to the x-axis title container.

Code
library(shiny)
library(plotly)
x <- y <- 1:10
dat <- expand.grid(x=x, y=y)
dat <- transform(dat, z=x*y)
### Create a <svg> rectangle element
testRect <- '<svg width="250" height="75" xmlns="http://www.w3.org/2000/svg">
<g class="test-rect">
<rect x="10" y="10" width="200" height="50"
style="fill:wheat; stroke:blue; stroke-width:1px"/>
</g>
</svg>'
### Add a first child title element to the rectangle <svg>
### Hovering over the rectangle displays the tooltip
jscode_testRect <- '$(document).ready(function(){
setTimeout(function(){
var titleRect = document.createElementNS("http://www.w3.org/2000/svg", "title");
titleRect.textContent = "rectangle tooltip";
var testRect = document.getElementsByClassName("test-rect")[0];
testRect.insertBefore(titleRect, testRect.childNodes[0]);
}, 500);
});'
### Add a first child title element to the SVG
### Hovering over the x-axis title doesn't display tooltip
jscode_xaxisPlotly <- '$(document).ready(function(){
setTimeout(function(){
var titleAxis = document.createElementNS("http://www.w3.org/2000/svg", "title");
titleAxis.textContent = "x-axis tooltip";
var gXtitle = document.getElementsByClassName("g-xtitle")[0];
gXtitle.insertBefore(titleAxis, gXtitle.childNodes[0]);
}, 500);
});'
shinyApp(
ui <- fluidPage(
tags$head(tags$script(HTML(jscode_testRect))),
tags$head(tags$script(HTML(jscode_xaxisPlotly))),
h4("<svg> rectangle - tooltip is functional"),
tags$div(HTML(testRect)),
h4("plotly heatmap - tooltip does not work"),
plotlyOutput("heatmap"),
br(),
br()
),
server = function(input, output){
output$heatmap <- renderPlotly(plot_ly() %>%
add_trace(data=dat, x=~x, y=~y, z=~z, type="heatmap") %>%
layout(
xaxis = list(title="foo"),
title = "Title"
)
)
}
)