If I understand correctly, you don't want the lines in variables 1, 2, 3... to be connected to each other, right?
If this is the case, I think what is happening is that plotly is assuming all your data belongs to the same series.
You need to tell it that the data from each variable is a different series. You can do it by mapping the variable
to an attribute of the line (color
, linetype
, stroke
, etc...).
library(tidyverse)
library(plotly)
# Create a data set from EuStockMarkets data for this example
# (this is just to put the data in a dataframe in the same format as your dataset. You can skip this part)
df.data <- EuStockMarkets %>% as.data.frame() %>%
dplyr::mutate(date=time(EuStockMarkets)) %>%
dplyr::mutate(year=as.integer(floor(date))) %>%
dplyr::mutate(day.of.year = ceiling((date-year) * 365)) %>%
dplyr::mutate(date=ymd(sprintf('%4d-01-01', year))+ days(day.of.year)) %>%
dplyr::select(-year, -day.of.year) %>%
tidyr::pivot_longer(-date, names_to = "variable") %>%
dplyr::arrange(variable, date)
plot_ly(data=df.data, x = ~date, y = ~variable, z = ~value, type="scatter3d", mode='lines', color = ~variable)
If you don't want the lines in each variable
to have different colors, you can use the split
argument, which will create a different trace (i.e., line) for each value of variable
. I had to set the color of the line manually otherwise plotly set a different color automatically. I also removed the legend.
plot_ly(data=df.data, x = ~date, y = ~variable, z = ~value, type="scatter3d", mode='lines', split=~variable, color=I('black')) %>% layout(showlegend = FALSE)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…