Plotly R 中的多行不使用 add_trace
Multiple lines in plotly R not using add_trace
我有这样的数据:
> data_example
date A B C D E F
1 2020-09-22 1.3 0.0 1.3 0.3 0.9 0.0
2 2020-09-23 0.7 0.0 0.7 0.0 0.7 0.0
3 2020-09-24 0.4 0.0 0.4 0.0 0.4 0.0
4 2020-09-25 0.2 0.2 0.5 0.0 0.2 0.0
5 2020-09-26 1.0 0.0 1.0 0.0 1.0 0.0
6 2020-09-27 0.2 0.2 0.5 0.1 0.1 0.0
7 2020-09-28 0.6 0.1 0.7 0.0 0.6 0.0
8 2020-09-29 0.4 0.1 0.5 0.1 0.2 0.0
9 2020-09-30 0.4 0.1 0.6 0.0 0.4 0.0
10 2020-10-01 1.0 0.1 1.1 0.8 0.1 0.0
11 2020-10-02 0.6 0.1 0.8 0.2 0.4 0.0
我想在不使用 add_trace.原因是我正在构建一个闪亮的应用程序,用户可以在其中动态选择,使用 selectize 输入,变量想要绘制,所以要动态地进行它必须不要以 add_trace 方式。
还有其他方法吗?
谢谢。
编辑:
dput 的输出(data_example)
data_example <- structure(list(date = c("2020-09-22", "2020-09-23", "2020-09-24",
"2020-09-25", "2020-09-26", "2020-09-27", "2020-09-28", "2020-09-29",
"2020-09-30", "2020-10-01", "2020-10-02"), A = c(1.3, 0.7, 0.4,
0.2, 1, 0.2, 0.6, 0.4, 0.4, 1, 0.6), B = c(0, 0, 0, 0.2, 0, 0.2,
0.1, 0.1, 0.1, 0.1, 0.1), C = c(1.3, 0.7, 0.4, 0.5, 1, 0.5, 0.7,
0.5, 0.6, 1.1, 0.8), D = c(0.3, 0, 0, 0, 0, 0.1, 0, 0.1, 0, 0.8,
0.2), E = c(0.9, 0.7, 0.4, 0.2, 1, 0.1, 0.6, 0.2, 0.4, 0.1, 0.4
), F = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c(NA,
-11L))
您应该将 data.frame
重塑为长格式。
为此我更喜欢 library(data.table)
- 请参阅 melt
调用。之后,您可以使用 split
或 color
生成跟踪:
library(data.table)
library(plotly)
DF <- data.frame(
date = c("2020-09-22","2020-09-23","2020-09-24",
"2020-09-25","2020-09-26","2020-09-27","2020-09-28",
"2020-09-29","2020-09-30","2020-10-01","2020-10-02"),
A = c(1.3, 0.7, 0.4, 0.2, 1, 0.2, 0.6, 0.4, 0.4, 1, 0.6),
B = c(0, 0, 0, 0.2, 0, 0.2, 0.1, 0.1, 0.1, 0.1, 0.1),
C = c(1.3, 0.7, 0.4, 0.5, 1, 0.5, 0.7, 0.5, 0.6, 1.1, 0.8),
D = c(0.3, 0, 0, 0, 0, 0.1, 0, 0.1, 0, 0.8, 0.2),
E = c(0.9, 0.7, 0.4, 0.2, 1, 0.1, 0.6, 0.2, 0.4, 0.1, 0.4),
F = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
)
setDT(DF)
longDF <- melt(DF, id.vars = "date")
plot_ly(longDF, type = "scatter", mode = "lines+markers", x = ~date, y = ~value, split = ~variable)
我有这样的数据:
> data_example
date A B C D E F
1 2020-09-22 1.3 0.0 1.3 0.3 0.9 0.0
2 2020-09-23 0.7 0.0 0.7 0.0 0.7 0.0
3 2020-09-24 0.4 0.0 0.4 0.0 0.4 0.0
4 2020-09-25 0.2 0.2 0.5 0.0 0.2 0.0
5 2020-09-26 1.0 0.0 1.0 0.0 1.0 0.0
6 2020-09-27 0.2 0.2 0.5 0.1 0.1 0.0
7 2020-09-28 0.6 0.1 0.7 0.0 0.6 0.0
8 2020-09-29 0.4 0.1 0.5 0.1 0.2 0.0
9 2020-09-30 0.4 0.1 0.6 0.0 0.4 0.0
10 2020-10-01 1.0 0.1 1.1 0.8 0.1 0.0
11 2020-10-02 0.6 0.1 0.8 0.2 0.4 0.0
我想在不使用 add_trace.原因是我正在构建一个闪亮的应用程序,用户可以在其中动态选择,使用 selectize 输入,变量想要绘制,所以要动态地进行它必须不要以 add_trace 方式。
还有其他方法吗?
谢谢。
编辑:
dput 的输出(data_example)
data_example <- structure(list(date = c("2020-09-22", "2020-09-23", "2020-09-24",
"2020-09-25", "2020-09-26", "2020-09-27", "2020-09-28", "2020-09-29",
"2020-09-30", "2020-10-01", "2020-10-02"), A = c(1.3, 0.7, 0.4,
0.2, 1, 0.2, 0.6, 0.4, 0.4, 1, 0.6), B = c(0, 0, 0, 0.2, 0, 0.2,
0.1, 0.1, 0.1, 0.1, 0.1), C = c(1.3, 0.7, 0.4, 0.5, 1, 0.5, 0.7,
0.5, 0.6, 1.1, 0.8), D = c(0.3, 0, 0, 0, 0, 0.1, 0, 0.1, 0, 0.8,
0.2), E = c(0.9, 0.7, 0.4, 0.2, 1, 0.1, 0.6, 0.2, 0.4, 0.1, 0.4
), F = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c(NA,
-11L))
您应该将 data.frame
重塑为长格式。
为此我更喜欢 library(data.table)
- 请参阅 melt
调用。之后,您可以使用 split
或 color
生成跟踪:
library(data.table)
library(plotly)
DF <- data.frame(
date = c("2020-09-22","2020-09-23","2020-09-24",
"2020-09-25","2020-09-26","2020-09-27","2020-09-28",
"2020-09-29","2020-09-30","2020-10-01","2020-10-02"),
A = c(1.3, 0.7, 0.4, 0.2, 1, 0.2, 0.6, 0.4, 0.4, 1, 0.6),
B = c(0, 0, 0, 0.2, 0, 0.2, 0.1, 0.1, 0.1, 0.1, 0.1),
C = c(1.3, 0.7, 0.4, 0.5, 1, 0.5, 0.7, 0.5, 0.6, 1.1, 0.8),
D = c(0.3, 0, 0, 0, 0, 0.1, 0, 0.1, 0, 0.8, 0.2),
E = c(0.9, 0.7, 0.4, 0.2, 1, 0.1, 0.6, 0.2, 0.4, 0.1, 0.4),
F = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
)
setDT(DF)
longDF <- melt(DF, id.vars = "date")
plot_ly(longDF, type = "scatter", mode = "lines+markers", x = ~date, y = ~value, split = ~variable)