在循环中添加跟踪会删除R闪亮中的先前跟踪

时间:2018-06-15 09:41:55

标签: r plot ggplot2 r-plotly

我已尝试过链接中提到的这三种解决方案 Plotly: add_trace in a loop

但是我的代码仍然无效,代码只显示循环中的最终跟踪。

output$plot <- renderPlotly({
    if(input$x == "M"){
      my_x <- c(input$mmin:input$mmax)
      number_of_cpgs <- 500*my_x
      timetaken <- rep(0,input$mmax-input$mmin +1)
      p<-plot_ly(y= timetaken, x= (number_of_cpgs) ,type="scatter", mode="markers+lines")
      for(i in input$nmin:input$nmax){
        for(j in input$kmin:input$kmax){
          timetaken <- timemat[i,my_x,j]
          p<-add_trace(p, y=~timemat[i,my_x,j], x=~(number_of_cpgs) , type="scatter",mode="markers+lines",visible = TRUE )
        }
      }
    }

enter image description here

可重复的例子:

timemat <- array(c(1:1000), dim=c(10, 10, 10)) 
my_x <- c(1:10) 
number_of_cpgs <- 500*my_x 
timetaken <- rep(0,10) 
p<-plot_ly(y= timetaken, x= (number_of_cpgs) ,type="scatter", mode="markers+lines") 
for(i in 5:6){ 
  for(j in 6:7){ 
    timetaken <- timemat[i,my_x,j] 
p <- add_trace(p, y=~timemat[i,my_x,j], x=~(number_of_cpgs) , type="scatter", mode="markers+lines", evaluate = TRUE) 
  }
  } 
p

1 个答案:

答案 0 :(得分:1)

这是你在找什么?

p<-plot_ly(y= timetaken, x= (number_of_cpgs) ,type="scatter", mode="markers+lines") 
for(i in 5:6){ 
  for(j in 6:7){ 
   # timetaken <- timemat[i,my_x,7] 
  testing = timemat[i,my_x,j]
p <- add_trace(p, y=testing, x=~(number_of_cpgs) , type="scatter", mode="markers+lines", evaluate = TRUE) 
  }
  } 
p

enter image description here