Add multiple lines to a plot_ly graph with add_trace

前端 未结 3 1610
日久生厌
日久生厌 2021-02-20 06:59

I found an example to add lines to a plot_ly plot by using the add_trace command. How can I add a list of lines to plot without using add_trace

3条回答
  •  庸人自扰
    2021-02-20 07:45

    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
    

提交回复
热议问题