r - add multiple lines to a plot_ly graph with add_trace -
i found example add lines plot_ly
plot using add_trace
command. how can add list of lines plot without using add_trace
multiple times?
i tried for
loop add traces doesn't work expected.
my_lines <- list( list(x=1:10, y=2:11, color='red'), list(x=1:10, y=0:9, color='blue'), list(x=1:10, y=3:12, color='green') ) p <- plot_ly() p for(line in my_lines) { p <- add_trace(p, y=line[['y']], x=line[['x']], marker=list(color=line[['color']])) } p
but example works expected.
p <- plot_ly() p <- add_trace(p, y=my_lines[[1]][['y']], x=my_lines[[1]][['x']], marker=list(color=my_lines[[1]][['color']])) p <- add_trace(p, y=my_lines[[2]][['y']], x=my_lines[[2]][['x']], marker=list(color=my_lines[[2]][['color']])) p <- add_trace(p, y=my_lines[[3]][['y']], x=my_lines[[3]][['x']], marker=list(color=my_lines[[3]][['color']])) p
hope can give me hint on this.
you need set evaluate = true
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
Comments
Post a Comment