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)))
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)))
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
plot_ly, but this may be a lazy evaluation problem. Try replacing yourforloop with anlapply. (Or putting aprint(Fig3abis)inside theforloop). Also, ifplot_lyworks withggplotaesthetics, you could avoid any sort of loop by working with tidy data:pivot_longerwill do the trick.lapplyworks 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 !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 simplyp. You've not shown your implementation of thelapplysolution, 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.