Plot multiple time-series lines in 3D with ggplot/plotly

限于喜欢 提交于 2020-12-15 04:56:33

问题


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.

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:


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'))

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.



来源:https://stackoverflow.com/questions/64869455/plot-multiple-time-series-lines-in-3d-with-ggplot-plotly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!