【问题标题】:How to integrate information from multiple traces in a singular plotly hovertext in shiny如何在闪亮的单个情节悬停文本中集成来自多个跟踪的信息
【发布时间】:2020-05-04 22:14:04
【问题描述】:

代码的目的是利用 plotly 创建一个闪亮的应用程序。我们希望每条跟踪线的信息出现在沿 x 轴的任何给定点的单个悬停模板中。例如,单个散点图中的三个跟踪线(模式 = 'lines+markers'),当用户将光标放在单个散点上时,悬停文本会为他们提供该 x- 三个图中的每一个的分数价值。

在下面的示例中,我们使用响应式函数 Data() 作为一个参与者的数据,该数据已在服务器端代码中进行了子集化。 variable1、variable2 和 variable3 是我们在散点图中绘制的 Data() 中的列。与下面的 3 个单独的相比,解决方案不会有一个单一的 hovertext 规范。

我们希望有一个悬停模板/悬停文本大致如下:

这是你的分数

变量 1 = XXX

变量 2 = XXX

变量 3 = XXX

对于高级点:当他们将鼠标悬停在从该数据点从顶部到底部的数据点上时出现的垂直线看起来很棒。

output$distPlot <-  renderPlotly({
  
  
  plot_ly() %>%
    add_trace(                  # trace for variable 1         
      type = 'scatter',
      mode = 'lines+markers',
      x = Data()$Sys.time          # take the time variable from data() for x
      y = Data()$variable1,        # Plot variable1
      hovertemplate = paste(
        "<b>Here are your scores</b><br><br>",
        "variable1: y <br>",
        "variable2: plot2$y <br>",    # placeholder for solution
        "variable3: plot3$y <br>",    # placeholder for solution
        "<extra></extra>"
      ),
      showlegend = TRUE
    ) %>%

       add_trace(                   # trace for variable 2            
      type = 'scatter',
      mode = 'lines+markers',
      x = Data()$Sys.time          # take the time variable from data() for x
      y = Data()$variable2,        # Plot variable2
      hovertemplate = paste(
        "<b>Here are your scores</b><br><br>",
        "variable1: plot1$y <br>",         # Placeholder for solution
        "variable2: y <br>",   
        "variable3: plot3$y <br>",    # Placeholder for solution
        "<extra></extra>"
      ),
      showlegend = TRUE
    ) %>%

    add_trace(                      # trace for variable 3                     
      type = 'scatter',
      mode = 'lines+markers',
      x = Data()$Sys.time          # take the time variable from data() for x
      y = Data()$variable3,        # Plot variable3
      hovertemplate = paste(
        "<b>Here are your scores</b><br><br>",
        "variable1: plot1$y <br>",    # Placeholder for solution
        "variable2: plot2$y <br>",    # Placeholder for solution
        "variable3: y <br>",          
        "<extra></extra>"
      ),
      showlegend = TRUE
    ) 
})      # Close plot

【问题讨论】:

  • 嗨,你能提供一个reproducible example 和ui 和server 部分吗?
  • 你知道你可以在plotly的工具栏(右上角)点击compare data on hover默认获得类似的行为吗?
  • @ismirsehregal 这很好用,我们可以在闪亮图中设置“悬停时比较数据”默认选项吗?即在比较而不是单一模式下呈现?
  • 请看下面我的回答,干杯

标签: r shiny hover plotly r-plotly


【解决方案1】:

您只需设置hovermode = 'compare'。

这是一个简单的例子:

library(shiny)
library(plotly)

DF <- data.frame(values = c(seq(1, 10, length.out = 10),
                            seq(1, 20, length.out = 10),
                            seq(1, 30, length.out = 10)), variables = rep(LETTERS[1:3], each = 10))

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

server <- function(input, output, session) {
  output$myPlot <- renderPlotly({
    plot_ly(DF, y = ~values, color = ~variables, type = "scatter", mode = "lines+markers") %>%
      layout(hovermode = 'compare')
  })
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 2015-11-25
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2017-12-17
    • 1970-01-01
    • 2018-09-21
    相关资源
    最近更新 更多