【发布时间】: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