Add multiple lines to a plot_ly graph with add_trace
I believe with the release of plotly 4.0
calling any of the add_*
family of functions forces evaluation so there is no need to call evaluate = T
anymore
So, something like this should work fine:
devtools::install_github("ropensci/plotly")
library(plotly)
p <- plot_ly()
for(i in 1:5){
p <- add_trace(p, x = 1:10, y = rnorm(10), mode = "lines")
}
p
You need to set evaluate = TRUE
to force evalutation / avoid lazy evaluation
p <- plot_ly()
p
for(line in my_lines) { p <- add_trace(p, y=line[['y']], x=line[['x']],
marker=list(color=line[['color']]),
evaluate = TRUE)
}
p
You can transform your inputs into a long-form data frame first, then plot using the split
argument.
library(plotly)
library(reshape2)
my_lines = data.frame(x = 1:10, red = 2:11, blue = 0:9, green = 3:12)
my_lines_long = reshape2::melt(my_lines, id.vars = "x")
fig = plotly::plot_ly(my_lines_long, x = ~x, y = ~value, split = ~variable,
marker=list(color=~variable))
fig