2

I have the following Shiny Application:

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("plot")
)

server <- function(input, output) {

  # renderPlotly() also understands ggplot2 objects!
  output$plot <- renderPlotly({
    plot_ly(mtcars, x = ~mpg, y = ~wt)
  })

}

shinyApp(ui, server)

If I now hoover over a point I get values like: (14.5, 17.3). Is there an easy way to make sure these values appear as:

mpg: 12.3 [enter] wt: 45.2

Florian
  • 24,425
  • 4
  • 49
  • 80
Henk Straten
  • 1,365
  • 18
  • 39

1 Answers1

2

I believe the following does what you want:

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("plot")
)

server <- function(input, output) {

  # renderPlotly() also understands ggplot2 objects!
  output$plot <- renderPlotly({
    plot_ly(mtcars, 
            x = ~mpg, 
            y = ~wt,  
            hoverinfo="text", 
            text = ~paste0("mpg: ", mpg, "\nwt: ", wt))
  })

}

shinyApp(ui, server)

enter image description here

Hope this helps!

Florian
  • 24,425
  • 4
  • 49
  • 80