add_trace 在 Plotly 中循环
add_trace in Plotly in a loop
我想在不覆盖所有以前的轨迹的情况下循环绘制多条轨迹。
在 2015 年的 this post 中,通过在 plot_ly 或 add_trace 函数中设置 evaluate = TRUE
,提出了在循环内添加跟踪的解决方案。但是,截至 2017 年,在 plot_ly 的较新版本中,evaluate
不是属性。
'scatter3d' objects don't have these attributes: 'evaluate'
This question, and also fail to answer the problem. This post 承认 evaluate = TRUE isn't 不适用于 new plotly,但没有给出解决方案。 这个问题的新解决方案是什么?
我的 copy/paste 数据:
structure(list(x_1 = c(74.651438, 75.31493, 76.736865, 77.858125,
79.347856, 80.302483), y_1 = c(249.063605, 247.149121, 245.461889,
243.811041, 242.382685, 240.300034), z_1 = c(4.373868, 3.653744,
4.101455, 4.134471, 4.225507, 4.890157), x_2 = c(85.468712, 86.637469,
87.993127, 87.907308, 88.748766, 89.680155), y_2 = c(249.063405,
247.145423, 245.46148, 244.949469, 244.005001, 242.917665), z_2 = c(4.56633,
4.059976, 3.842906, 4.019021, 4.316799, 4.378894), x_3 = c(101.720648,
101.70598, 102.022836, 102.166059, 102.242424, 102.317043), y_3 = c(249.660075,
249.562464, 249.771452, 249.619983, 249.39349, 249.444531), z_3 = c(3.080686,
3.003508, 2.774651, 2.291644, 2.239727, 1.932501)), .Names = c("x_1",
"y_1", "z_1", "x_2", "y_2", "z_2", "x_3", "y_3", "z_3"), row.names = c(NA,
6L), class = "data.frame")
我的代码:
library(plotly)
p <- plot_ly(temp, type = 'scatter3d', mode = 'lines', width = 1)
for(i in 1:3){
x = paste0("x_",i)
y = paste0("y_",i)
z = paste0("z_",i)
p <- add_trace(p, x = ~get(x), y = ~get(y), z = ~get(z))
}
p
您可以从一个空的 Plotly 对象开始,然后按顺序添加轨迹。
library(plotly)
temp <- structure(list(x_1 = c(74.651438, 75.31493, 76.736865, 77.858125,
79.347856, 80.302483), y_1 = c(249.063605, 247.149121, 245.461889,
243.811041, 242.382685, 240.300034), z_1 = c(4.373868, 3.653744,
4.101455, 4.134471, 4.225507, 4.890157), x_2 = c(85.468712, 86.637469,
87.993127, 87.907308, 88.748766, 89.680155), y_2 = c(249.063405,
247.145423, 245.46148, 244.949469, 244.005001, 242.917665), z_2 = c(4.56633,
4.059976, 3.842906, 4.019021, 4.316799, 4.378894), x_3 = c(101.720648,
101.70598, 102.022836, 102.166059, 102.242424, 102.317043), y_3 = c(249.660075,
249.562464, 249.771452, 249.619983, 249.39349, 249.444531), z_3 = c(3.080686,
3.003508, 2.774651, 2.291644, 2.239727, 1.932501)), .Names = c("x_1",
"y_1", "z_1", "x_2", "y_2", "z_2", "x_3", "y_3", "z_3"), row.names = c(NA,
6L), class = "data.frame")
p <- plot_ly()
for(i in 1:3){
x = paste0("x_",i)
y = paste0("y_",i)
z = paste0("z_",i)
p <- add_trace(p,
x = temp[[x]],
y = temp[[y]],
z = temp[[z]],
type = 'scatter3d',
mode = 'lines',
line = list(width = 1))
}
p
我想在不覆盖所有以前的轨迹的情况下循环绘制多条轨迹。
在 2015 年的 this post 中,通过在 plot_ly 或 add_trace 函数中设置 evaluate = TRUE
,提出了在循环内添加跟踪的解决方案。但是,截至 2017 年,在 plot_ly 的较新版本中,evaluate
不是属性。
'scatter3d' objects don't have these attributes: 'evaluate'
This question, and
我的 copy/paste 数据:
structure(list(x_1 = c(74.651438, 75.31493, 76.736865, 77.858125,
79.347856, 80.302483), y_1 = c(249.063605, 247.149121, 245.461889,
243.811041, 242.382685, 240.300034), z_1 = c(4.373868, 3.653744,
4.101455, 4.134471, 4.225507, 4.890157), x_2 = c(85.468712, 86.637469,
87.993127, 87.907308, 88.748766, 89.680155), y_2 = c(249.063405,
247.145423, 245.46148, 244.949469, 244.005001, 242.917665), z_2 = c(4.56633,
4.059976, 3.842906, 4.019021, 4.316799, 4.378894), x_3 = c(101.720648,
101.70598, 102.022836, 102.166059, 102.242424, 102.317043), y_3 = c(249.660075,
249.562464, 249.771452, 249.619983, 249.39349, 249.444531), z_3 = c(3.080686,
3.003508, 2.774651, 2.291644, 2.239727, 1.932501)), .Names = c("x_1",
"y_1", "z_1", "x_2", "y_2", "z_2", "x_3", "y_3", "z_3"), row.names = c(NA,
6L), class = "data.frame")
我的代码:
library(plotly)
p <- plot_ly(temp, type = 'scatter3d', mode = 'lines', width = 1)
for(i in 1:3){
x = paste0("x_",i)
y = paste0("y_",i)
z = paste0("z_",i)
p <- add_trace(p, x = ~get(x), y = ~get(y), z = ~get(z))
}
p
您可以从一个空的 Plotly 对象开始,然后按顺序添加轨迹。
library(plotly)
temp <- structure(list(x_1 = c(74.651438, 75.31493, 76.736865, 77.858125,
79.347856, 80.302483), y_1 = c(249.063605, 247.149121, 245.461889,
243.811041, 242.382685, 240.300034), z_1 = c(4.373868, 3.653744,
4.101455, 4.134471, 4.225507, 4.890157), x_2 = c(85.468712, 86.637469,
87.993127, 87.907308, 88.748766, 89.680155), y_2 = c(249.063405,
247.145423, 245.46148, 244.949469, 244.005001, 242.917665), z_2 = c(4.56633,
4.059976, 3.842906, 4.019021, 4.316799, 4.378894), x_3 = c(101.720648,
101.70598, 102.022836, 102.166059, 102.242424, 102.317043), y_3 = c(249.660075,
249.562464, 249.771452, 249.619983, 249.39349, 249.444531), z_3 = c(3.080686,
3.003508, 2.774651, 2.291644, 2.239727, 1.932501)), .Names = c("x_1",
"y_1", "z_1", "x_2", "y_2", "z_2", "x_3", "y_3", "z_3"), row.names = c(NA,
6L), class = "data.frame")
p <- plot_ly()
for(i in 1:3){
x = paste0("x_",i)
y = paste0("y_",i)
z = paste0("z_",i)
p <- add_trace(p,
x = temp[[x]],
y = temp[[y]],
z = temp[[z]],
type = 'scatter3d',
mode = 'lines',
line = list(width = 1))
}
p