Add hover text in plotly

plotly

#1

Is it possible to have a button in plotlty chart which on clicked would display some text, like an FAQ? I know we can add buttons. But how to add a text display when clicked or hovered on the button.


#2

If your ploy.ly plot is part of a shiny app, shiny has a few options:

Hover options with hoverOpts

shinyBS has some Tooltips and Popovers options to check out.

Shiny Notifications (notification will appear in the bottom right corner of the browser, remain for 5 seconds, and then fade away)

Shiny Modal Dialog (pop ups)

Dean Attali has the shinyAlert package that might be helpful.

And then ployly has a nice community here too: https://community.plot.ly/


#3

If you simply need to add text, add a custom button that triggers either a relayout or restyle event.

library(plotly)

x <- seq(-2*pi, 2*pi, length.out = 1000)
df <- data.frame(x, y = sin(x))

p <- plot_ly(df, x = ~x) %>%
    add_lines(y = ~y)

faq_txt <- list(
    x = 0, 
    y = 0, 
    text = "Sin starts at 0!"
)

layout(
    p,
    updatemenus = list(
        list(
            type = "buttons",
            y = 0.8,
            buttons = list(
                list(
                    method = "relayout",
                    args = list("annotations", list(faq_txt)),
                    label = "FAQ"
                )
            )
        )
    )
)

Having a single button that adds and clears the same text isn't as straightforward. If you're willing to have another button to clear the text, you could add another to this example pretty easily, but for a better user experience, I think you'd have to write some custom HTML/JavaScript or move to a shiny app.