plotly / plotly/plotly.R

Targeting/Adding traces in subplot

Open
#1,899 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

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))

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the supplied Shiny reproduction as the starting point, focusing on the plotlyProxyInvoke calls for restyle, addTraces, and moveTraces in the subplot example. Determine the expected trace indexing behavior for subplots and whether the result should be a code correction or clearer documentation; done means the requested traces can be targeted predictably.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.