在 for 循环中构建绘图图不显示所有系列 [英] Building plotly graph in for loop not displaying all series
问题描述
通过一次添加一列来创建一个图就可以了
Creating a plot by adding one column at a time works just fine
exPlot <- plot_ly(data.table(matrix(1:9,ncol = 3)))
theCols <- c("V2","V3")
exPlot <- exPlot %>% add_lines(x = ~V1, y = ~get(theCols[1]), type = "scatter",mode = "lines")
exPlot <- exPlot %>% add_lines(x = ~V1, y = ~get(theCols[2]), type = "scatter",mode = "lines")
exPlot
但如果我尝试在 for 循环中做同样的事情,它只会显示第二个跟踪,覆盖第一个.
but if I try to do the same thing in a for loop, it only displays the second trace, overwriting the first one.
exPlot <- plot_ly(data.table(matrix(1:9,ncol = 3)))
theCols <- c("V2","V3")
for(i in 1:2){
exPlot <- exPlot %>% add_lines(x = ~V1, y = ~get(theCols[i]), type = "scatter",mode = "lines")
}
exPlot
有什么办法可以解决这个问题?我环顾了一下,设置evaluate = TRUE"曾经是答案,但似乎已被弃用.
Any way to get around this? I looked around a bit, and setting "evaluate = TRUE" used to be the answer, but that seems to have been deprecated.
推荐答案
原因让我失望,但您要求任何解决此问题的方法".
The reason beats me but you are asking for 'any way to get around this'.
您可以在循环中指定所需的 y 值,而不是一次传递整个 data.table
,它应该可以工作.
Instead of passing the whole data.table
at once, you could specify the required y-values in the loop and it should work.
df <- data.table(matrix(1:9,ncol = 3))
exPlot <- plot_ly(df[[1]])
theCols <- c("V2","V3")
for(i in 1:2){
exPlot <- add_lines(exPlot,
y = df[[theCols[i]]],
type = "scatter",
mode = "lines")
}
exPlot
这篇关于在 for 循环中构建绘图图不显示所有系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!