为什么在使用 R 向 Plotly 对象迭代添加轨迹时只显示最后添加的轨迹?
Why is only the last added trace displayed when adding traces iteratively to a Plotly object using R?
library(plotly)
V <- data.frame(t, A, B, C, D, E, F)
pic <- plot_ly(V, x = ~t) %>%
add_trace(y = ~40000*exp(-t/7), name = "P(t)",
type = "scatter", mode="lines",
line=list(color="gray",width=2)
) %>%
layout(yaxis=list(title="Amount / million euros",zeroline=TRUE),
xaxis = list(title="t",zeroline = TRUE)
)
CLR <- c("darkblue", "powderblue", "mediumaquamarine", "darkred", "wheat", "tan")
for (j in 1:6)
{
pic <- pic %>%
add_trace(y = ~V[,j+1], name = colnames(V)[j+1],
type = "scatter", mode = 'lines+markers',
marker=list(size=8,color=CLR[j]),
line=list(color=CLR[j],width=1.5)
)
print(pic)
}
我有一个 7 列的数据框,V
,我想通过每次向绘图对象 pic
添加一条轨迹来绘制最后 6 列与第一列的对比。然而,当我在每次迭代结束时打印出 pic
时,只显示最后添加的轨迹,而之前的所有轨迹都消失了。不过,传说无处不在。为什么会这样?我该如何解决?
两种可能的解决方案。
set.seed(123)
t <- 1:20
A <- 4*exp(-t/7)+rnorm(20)*0.1
B <- 4*exp(-t/7)+rnorm(20)*0.2
C <- 4*exp(-t/7)+rnorm(20)*0.3
D <- 4*exp(-t/7)+rnorm(20)*0.4
E <- 4*exp(-t/7)+rnorm(20)*0.5
F <- 4*exp(-t/7)+rnorm(20)*0.6
V <- data.frame(t, A, B, C, D, E, F)
pic <- plot_ly(data=V, x = ~t) %>%
add_trace(y = ~4*exp(-t/7), name = "P(t)",
type = "scatter", mode="lines",
line=list(color="gray",width=2)
) %>%
layout(yaxis=list(title="Amount / million euros",zeroline=TRUE),
xaxis = list(title="t",zeroline = TRUE)
)
CLR <- c("darkblue", "powderblue", "mediumaquamarine", "darkred", "wheat", "tan")
for (j in 1:6) {
pic <- pic %>%
add_trace(x = V[,1], y = V[,j+1], name = colnames(V)[j+1],
type = "scatter", mode = 'lines+markers',
marker=list(size=8, color=CLR[j]),
line=list(color=CLR[j], width=1.5, inherit.aes=F)
)
}
print(pic)
或者
for (j in 1:6) {
df <- data.frame(x=V[,1], y=V[,j+1])
pic <- pic %>%
add_trace(data=df, x = ~x, y = ~y, name = colnames(V)[j+1],
type = "scatter", mode = 'lines+markers',
marker=list(size=8, color=CLR[j]),
line=list(color=CLR[j], width=1.5)
)
}
library(plotly)
V <- data.frame(t, A, B, C, D, E, F)
pic <- plot_ly(V, x = ~t) %>%
add_trace(y = ~40000*exp(-t/7), name = "P(t)",
type = "scatter", mode="lines",
line=list(color="gray",width=2)
) %>%
layout(yaxis=list(title="Amount / million euros",zeroline=TRUE),
xaxis = list(title="t",zeroline = TRUE)
)
CLR <- c("darkblue", "powderblue", "mediumaquamarine", "darkred", "wheat", "tan")
for (j in 1:6)
{
pic <- pic %>%
add_trace(y = ~V[,j+1], name = colnames(V)[j+1],
type = "scatter", mode = 'lines+markers',
marker=list(size=8,color=CLR[j]),
line=list(color=CLR[j],width=1.5)
)
print(pic)
}
我有一个 7 列的数据框,V
,我想通过每次向绘图对象 pic
添加一条轨迹来绘制最后 6 列与第一列的对比。然而,当我在每次迭代结束时打印出 pic
时,只显示最后添加的轨迹,而之前的所有轨迹都消失了。不过,传说无处不在。为什么会这样?我该如何解决?
两种可能的解决方案。
set.seed(123)
t <- 1:20
A <- 4*exp(-t/7)+rnorm(20)*0.1
B <- 4*exp(-t/7)+rnorm(20)*0.2
C <- 4*exp(-t/7)+rnorm(20)*0.3
D <- 4*exp(-t/7)+rnorm(20)*0.4
E <- 4*exp(-t/7)+rnorm(20)*0.5
F <- 4*exp(-t/7)+rnorm(20)*0.6
V <- data.frame(t, A, B, C, D, E, F)
pic <- plot_ly(data=V, x = ~t) %>%
add_trace(y = ~4*exp(-t/7), name = "P(t)",
type = "scatter", mode="lines",
line=list(color="gray",width=2)
) %>%
layout(yaxis=list(title="Amount / million euros",zeroline=TRUE),
xaxis = list(title="t",zeroline = TRUE)
)
CLR <- c("darkblue", "powderblue", "mediumaquamarine", "darkred", "wheat", "tan")
for (j in 1:6) {
pic <- pic %>%
add_trace(x = V[,1], y = V[,j+1], name = colnames(V)[j+1],
type = "scatter", mode = 'lines+markers',
marker=list(size=8, color=CLR[j]),
line=list(color=CLR[j], width=1.5, inherit.aes=F)
)
}
print(pic)
或者
for (j in 1:6) {
df <- data.frame(x=V[,1], y=V[,j+1])
pic <- pic %>%
add_trace(data=df, x = ~x, y = ~y, name = colnames(V)[j+1],
type = "scatter", mode = 'lines+markers',
marker=list(size=8, color=CLR[j]),
line=list(color=CLR[j], width=1.5)
)
}