[英]R plotly adding traces in a loop
我試圖在同一個圖上繪制許多軌跡,因此需要使用循環,但它確實不起作用,如下面的示例所示:
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 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)))
如您所見,有一個問題:該圖僅顯示最后添加的軌跡。
當我嘗試一一繪制每條跡線時,效果非常好,如下所示:
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 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)))
我一直在尋找解決方案,這不是第一次提交這個問題,但沒有一個不同的答案對我有用。 (以評估為例)
注意:這里我使用了一個小循環和一小部分軌跡樣本,但最后我想繪制大約 8000 條軌跡。
我使用的數據是一個簡單的數據框,第一列顯示 x 軸,其他所有代表需要繪制的每條跡線
我認為您正在用每個循環覆蓋 Fig3abis,而不是添加跟蹤。 在你的循環中試試這個:
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)))
正如 Limey 在評論中所建議的那樣,問題是由於惰性評估問題造成的。 我使用 lapply 循環而不是 for 循環解決了它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.