行作为系列Plotly R

时间:2018-09-17 21:33:19

标签: r plotly r-plotly

我有一个数据框:

df<-data.frame(Level=c("Small","Bigger","Biggest"),Threshold1=c(0.9,.8,.7),Threshold2=c(0.4,.5,.6),Threshold3=c(.6,.2,.1))

我想制作如下图形,但是使用plotly:

enter image description here

您将如何读懂它以实现这一目标?

我尝试使用t()进行旋转,并查看了详细的文档,但没有看到有关将行用作序列并将标识符列的值用作序列名称的方法的任何信息。

在我的实际数据中,Level列中可以存在的值数量可以更改,因此理想情况下,我想找到一个可以缩放任意数量的值Level的解决方案可能包含。

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

使用data.table会更容易:

library(data.table)
library(plotly
df<-data.table(Level=c("Small","Bigger","Biggest"),Threshold1=c(0.9,.8,.7),Threshold2=c(0.4,.5,.6),Threshold3=c(.6,.2,.1))

d <-melt.data.table(df, id.vars='Level')
plot_ly(d, x=~variable, y=~value,type = 'scatter', mode = 'lines',  color = ~Level)

您应该获得所需的图表。 Chart

答案 1 :(得分:0)

R基中的溶液:

library(plotly)

df<-data.frame(Level=c("Small","Bigger","Biggest"),
               Threshold1=c(0.9,.8,.7),Threshold2=c(0.4,.5,.6),Threshold3=c(.6,.2,.1))

df.reshaped <- reshape(df, varying = c("Threshold1", "Threshold2", "Threshold3"), 
                            v.names = "Values", idvar = "Level", direction = "long", 
                            times = c("Threshold1", "Threshold2", "Threshold3"), 
                            new.row.names = 1:(3 * nrow(df)))

plot_ly(df.reshaped, x=~time, y=~Values, type = 'scatter', mode = 'lines',  color = ~Level)

输出图:

enter image description here