0

I'm trying to plot many traces on the same figure, thus needing to use a loop but it doesn't work, as you can see in the example below :

Fig3abis<-plot_ly(data=dataresults3ab,x=dataresults3ab[[1]])
    
    for(j in c(100,500,1000,1500))
    {
        #print(dataresults3ab[,j])
        Fig3abis <- add_trace(Fig3abis,
                       y=~dataresults3ab[,j],
                       name=paste("N1",as.character(j),sep = "_"),
                       type="scatter",
                       mode="markers",
                       marker=list(size=4,color="black"))
    }
    
    Fig3abis <- Fig3abis%>% layout(title="Bifurcation diagram for five species competing for five resources. Local minima and maxima of species 1, 
                         from t=2,000 to t=4,000 days, as a function of the half-saturation constant K41",
                             showlegend=F,
                             xaxis=list(title="Half-saturation constant K41, of species 1",range=c(0.1,0.5)),
                             yaxis=list(title="Abundancie of species 1",range=c(0,100))) 

First Figure

As you can see there is a problem : the plot only show the last added trace.

When I try to plot each trace one by one, it works really well, as you can see bellow :

Fig3a <- plot_ly(data=dataresults3ab,x=dataresults3ab[[1]])
  
  Fig3a <- add_trace(Fig3a,
                        y=~dataresults3ab[,100],
                        name="N1_2",
                        type="scatter",
                        mode="markers",
                        marker=list(size=4,color="black"))%>%
           add_trace(Fig3a,
                       y=~dataresults3ab[,500],
                       name="N_3",
                       type="scatter",
                       mode="markers",
                       marker=list(size=4,color="black"))%>%
           add_trace(Fig3a,
                       y=~dataresults3ab[,1000],
                       name="N1_4",
                       type="scatter",
                       mode="markers",
                       marker=list(size=4,color="black"))%>%
            add_trace(Fig3a,
                       y=~dataresults3ab[,1500],
                       name="N1_5",
                       type="scatter",
                       mode="markers",
                       marker=list(size=4,color="black"))

  Fig3a <- Fig3a%>% layout(title="Bifurcation diagram for five species competing for five resources. Local minima and maxima of species 1, from t=2,000 to t=4,000 days, as a function of the half-saturation constant K41",
           showlegend=F,
           xaxis=list(title="Half-saturation constant K41, of species 1",range=c(0.1,0.5)),
           yaxis=list(title="Abundancie of species 1",range=c(0,100)))

Second Figure

I've been looking for solution for quite a while and this is not the first time this problem is submitted but none of the different answers worked for me (using evaluate for example)

Note: here I used a small loop and a small sample of traces but in the end I'd like to plot around 8000 traces.

The data I used is a simple data frame with the first column displaying the x axis and all of the other represent each trace that needs to be plotted

4
  • I'm not familair with plot_ly, but this may be a lazy evaluation problem. Try replacing your for loop with an lapply. (Or putting a print(Fig3abis) inside the for loop). Also, if plot_ly works with ggplot aesthetics, you could avoid any sort of loop by working with tidy data: pivot_longer will do the trick. Commented Jul 1, 2022 at 14:52
  • Possible duplicate of https://stackoverflow.com/questions/67413684/adding-layers-to-ggplots-works-but-adding-in-a-loop-does-not. Commented Jul 1, 2022 at 15:01
  • Hi @Limey your solution using lapply works really well thank you very much. Therefore I have antoher problem because the loop is showing the plot at each step, making the process very long (I'll need 8000 loops in total) is there a way to disable this ? (it is also printing the current loop in the console). I couldn't find where to do it in the documentation. Thanks again ! Commented Jul 4, 2022 at 8:32
  • That's a new question! Nevertheless: library(tidyverse); dfList <- lapply(1:3, function(x) tibble(x=rnorm(5), y=rnorm(5))); p <- lapply(dfList, function(d) d %>% ggplot() + geom_line(aes(x, y))) does not print a plot for each iteration of the loop for me. You can then see the plots with simply p. You've not shown your implementation of the lapply solution, so I cannot say more. I repeat my suggestion that you may well be better off working with long data. This may be an XY problem. Commented Jul 4, 2022 at 8:41

2 Answers 2

0

I think you are overwriting Fig3abis with every loop instead of adding a trace. Try this inside your loop:

Fig3abis <- Fig3abis %>% add_trace(....
Fig3abis<-plot_ly(data=dataresults3ab,x=dataresults3ab[[1]])
  
  for(j in c(100,500,1000,1500))
  {
  #print(dataresults3ab[,j])
  Fig3abis <- Fig3abis %>% 
           add_trace(Fig3abis,
                     y=~dataresults3ab[,j],
                     name=paste("N1",as.character(j),sep = "_"),
                     type="scatter",
                     mode="markers",
                     marker=list(size=4,color="black"))
  }
  
  Fig3abis <- Fig3abis%>% layout(title="Bifurcation diagram for five species competing for five ressources. Local minima and maxima of species 1, 
                       from t=2,000 to t=4,000 days, as a function of the half-saturation constant K41",
                           showlegend=F,
                           xaxis=list(title="Half-saturation constant K41, of species 1",range=c(0.1,0.5)),
                           yaxis=list(title="Abundancie of species 1",range=c(0,100))) 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I tried but it doesn't work but thanks anyway :)
0

As Limey suggest in the comments, the problem was due to a lazy avaluation problem. I solved it using a lapply loop instead of a for loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.