Aim of the code is to create a shiny application utilising plotly. We want the information for each trace line to appear in a singular hovertemplate for any given point along the x-axis. For example, three trace lines in a singular scatter plot (mode = 'lines+markers') and when the user places their curser on a singular scatter point, the hover text gives them their scores from each of the three plots for that x-value.
In the example below, we are using reactive function Data() to be the data for one participant which has been subsetted earlier in the server end code. variable1, variable2, and variable3 are the columns from Data() we are plotting in the scatter plot. No double the solution will have a singular hovertext specification as opposed to the 3 seperate ones below.
We want to have a hovertemplate / hovertext to look roughly like:
Here are your scores
variable 1 = XXX
variable 2 = XXX
variable 3 = XXX
For advanced points: a verticle line appearing when they hover over a datapoint that goes from the top to the bottom of the plot at that datapoint would look great.
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
ui
andserver
parts? – bretauvcompare data on hover
in plotly's toolbar (upper right corner) to get a similar behaviour by default? – ismirsehregal