更改将鼠标悬停在绘图中的值上

时间:2018-04-16 11:32:26

标签: r shiny plotly

我有以下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)

如果我现在翻过一个点,我得到的值如下:(14.5,17.3)。是否有一种简单的方法可以确保这些值显示为:

mpg:12.3 [输入] wt:45.2

1 个答案:

答案 0 :(得分:2)

我相信以下是您想要的:

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

希望这有帮助!