Skip to content

Targeting/Adding traces in subplot #1899

@klar-C

Description

@klar-C

Hi - I'm trying to add/change traces in a graph with subplots. Below the example code.

It looks like each subplot becomes one trace (based on playing with the restyle section), but it is unclear to me how to add/remove traces. I changed the addTraces number at the end to 0-3 but it always adds it to the first subplot.

Any help would be appreciated.

data <- tibble(runmonth=c(1,1,1,1,4,4,4,4),variable=c('a','a','b','b','a','a','b','b'),x=c(1:8),y=c(8:1),keyname=paste0('k',c(1:8)))

p <- data %>% group_by(runmonth,variable) %>%
  do(
    p = highlight_key(., ~keyname,group='scattergroup') %>%
      plot_ly(showlegend = FALSE) %>%
      add_markers(
        x = ~x, y = ~y
      )
  ) %>%
  subplot(
    nrows = 2, margin = 0.01,
    shareY = TRUE, shareX = TRUE, titleY = FALSE
  ) %>% toWebGL() %>%
  highlight(
    on = "plotly_selected",
    off = c('plotly_relayout')
  )


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(
        "restyle",
        list(
          'marker.symbol' = list(c(rep('triangle-up',1),'circle'))
        ),
        list(0,1,2,3)
      ) %>%
      plotlyProxyInvoke(
        "addTraces",
        list(
          x = c(50,60),
          y = c(50,60),
          mode = "scatter"
        ),1
      ) %>%
      plotlyProxyInvoke(
        "moveTraces",
        -1,0
      )
  })
  

}

runApp(shinyApp(ui, server))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions