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

标签 r shiny plotly

我有以下 Shiny 的应用程序:

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)。有没有一种简单的方法可以确保这些值显示为:

英里/加仑:12.3 [输入] 重量:45.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

希望这有帮助!

关于r - 更改将鼠标悬停在绘图中的值上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49856253/

相关文章:

r - 如何选择 `selectInput()` 作为数据框中的元素,将其粘贴在一起,用 "|"分隔并对齐?

python - 本地运行的 python dash 应用程序对敏感数据是否安全?

python - 绘制 Distplot 子图

javascript - 使用 onInputChange 和 addCustomMessageHandler 将数据传递给forceNetwork

python - 将 Plotly 与 pycharm 一起使用

r - 在 R 中执行预测模型的时间

r - 如何在R中一起绘制多个堆叠的直方图?

r - 如何在 dplyr 分组数据上使用 rollmean

r - 在 lme4 中获取残差-协方差矩阵

javascript - 使用 DT 进行列格式化的单个/多个条件