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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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.
原因打败了我,但您要求“以任何方式解决此问题”。
data.table您可以在循环中指定所需的 y 值,而不是一次传递整个,它应该可以工作。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
588 次 |
| 最近记录: |