【问题标题】:Plot multiple time-series lines in 3D with ggplot/plotly使用 ggplot/plotly 在 3D 中绘制多条时间序列线
【发布时间】:2021-02-28 07:50:28
【问题描述】:

我有一个包含不同时间序列信号的数据框,我试图在 3D 中绘制这些信号,x 轴代表时间,Y 轴代表所有线的标准化值,Z 轴显示每一行。这是我的意思的一个例子。

我现在尝试配置一段代码以正确输出它,但我不确定如何正确分配 y 和 z 变量。 df 包含 5 列;时间 + 4 个不同的时间序列信号。

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

数据框如下所示:

      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

我正在尝试使用 plotly 或 ggplot 来执行渲染。感谢您的帮助!

我来自:https://www.r-bloggers.com/2016/06/3d-density-plot-in-r-with-plotly/

【问题讨论】:

  • 请提供给我们一个虚拟的data.frame 或发布dput(df) 的输出。
  • @ismirsehregal 编辑了帖子以包含一个虚拟 df。
  • 请检查我的回答。
  • 这非常接近生产我需要的东西 - 非常感谢! plotly 有没有用颜色填充每个时间序列线的曲线下区域的方法?
  • 不幸的是,在 plotly 中还没有可用于 scatter3d 跟踪的 fill 参数。请参阅this

标签: r ggplot2 plot plotly r-plotly


【解决方案1】:

在这种情况下,您应该使用例如将数据从宽格式重新格式化为长格式。 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'))

如果您想避免重塑您的 data.frame,您可以使用 add_trace 为数据的每一列添加新的跟踪。

【讨论】:

    猜你喜欢
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2018-03-14
    • 2019-12-21
    相关资源
    最近更新 更多