I have a question about a weird behavior of plotly subplots. When adding a new trace to a single graph the new trace shows up on top. However when adding the trace into a graph that’s part of a sublot I have no way of getting the trace into the front. Please see below 2 examples.
The first one is a single graph. Adding the new trace gets to the front. The second one creates a subplot and then adds the trace. However even though it’s the latest trace its always in the background. I tried to move it around with moveTraces but nothing helps - it stays in the background. Is this a bug or what am I missing?
data <- tibble(
runmonth=c(1,1,1,1,4,4,4,4,c(2,2,2,2,5,5,5,5)),
variable=c('a','a','b','b','a','a','b','b',c('d','d','e','e','d','d','e','e')),
x=c(1:16),
y=c(16:1),
keyname=paste0('k',c(rep(1,8),rep(2,8)))
)
p_lst <- data %>% dlply(c('runmonth','variable'),function(df){
p <- highlight_key(df, ~keyname,group='scattergroup') %>%
plot_ly(showlegend = FALSE) %>%
add_markers(
x = ~x, y = ~y
) %>%
layout(
yaxis = list(title = df$variable[1]),
xaxis = list(title = df$runmonth[1]),
plot_bgcolor = "rgba(235,235,235,1)",
paper_bgcolor = "rgba(255,255,255,1)"
)
return(p)
})
ui <- function(id) {
div(
actionButton('go','go'),
plotlyOutput("scattergr")
)
}
server <- function(input,output,session) {
output$scattergr <- renderPlotly({p_lst[[1]]})
observeEvent(input$go,{
plotlyProxy("scattergr", session) %>%
plotlyProxyInvoke(
"addTraces",
list(
x = c(1,3),
y = c(16,18),
mode = "markers",
marker=list(color='red')
)
)
})
}
runApp(shinyApp(ui, server))
ui <- function(id) {
div(
actionButton('go','go'),
plotlyOutput("scattergr")
)
}
server <- function(input,output,session) {
output$scattergr <- renderPlotly({p})
observeEvent(input$go,{
plotlyProxy("scattergr", session) %>%
plotlyProxyInvoke(
"addTraces",
list(
x = c(1,3),
y = c(16,18),
mode = "markers",
marker=list(color='red')
)
)
})
}
runApp(shinyApp(ui, server))