0
votes

I have a data frame containing different time-series signals which I'm trying to plot in 3D, with the x-axis representing Time, the Y-axis representing a standardized value for all the lines, and the Z-axis showing each line. Here's an example of what I mean. enter image description here

I have a snippet of code I'm trying to configure now to output it properly but I'm not sure how to properly assign the y and z variables. The df contains 5 columns; Time + 4 different time-series signals.

plot_ly(
  data = df,
  x = df$Time,
  y = scale(df),
  z = names(df),
  type = 'scatter3d',
  mode = 'lines',
  color = c('red', 'blue', 'yellow', 'green'))

Dataframe looks like so:

      Time       coup.nu          Coup.nuti       coup.Ca       coup.B
1  198.001  0.0002630826       0.0003027965  2.141347e-07            1
2  198.002  0.0002630829       0.0003027953  2.141379e-07            1
3  198.003  0.0002630833       0.0003027940  2.141412e-07            1
4  198.004  0.0002630836       0.0003027928  2.141444e-07            1
5  198.005  0.0002630840       0.0003027916  2.141477e-07            1

I'm trying to use plotly or ggplot to perform the render. Thanks for the help!

I sourced this from: https://www.r-bloggers.com/2016/06/3d-density-plot-in-r-with-plotly/

1
Please provide us with a dummy data.frame or post the output of dput(df) .ismirsehregal
@ismirsehregal edited the post to include a dummy df.Kaddy
Please check my answer.ismirsehregal
This is very close to producing exactly what I need - thank you very much! Does plotly have a method of filling the area under the curve of each time-series line with colour?Kaddy
Unfortunately there is no fill argument available for scatter3d traces in plotly yet. Please see this.ismirsehregal

1 Answers

0
votes

In a case like this you should reformat your data from wide to long using e.g. melt:

library(plotly)
library(reshape2)

DF <- data.frame(
        Time = c(198.001, 198.002, 198.003, 198.004, 198.005),
     coup.nu = c(0.000263083,0.000263083,0.000263083, 0.000263084,0.000263084),
   Coup.nuti = c(0.000302797,0.000302795,0.000302794, 0.000302793,0.000302792),
     coup.Ca = c(2.14e-07, 2.14e-07, 2.14e-07, 2.14e-07, 2.14e-07),
      coup.B = c(1L, 1L, 1L, 1L, 1L)
)
DF_long <- melt(DF, id.vars=c("Time"))

plot_ly(
  data = DF_long,
  type = 'scatter3d',
  mode = 'lines',
  x = ~ Time,
  y = ~ value,
  z = ~ variable,
  color = ~ variable,
  colors = c('red', 'blue', 'yellow', 'green'))

result

If you want to avoid reshaping your data.frame you could use add_trace to add a new trace for each column of your data.