问题描述
我有一个数据帧,其中包含我要尝试以3D方式绘制的不同时间序列信号,其中x轴表示时间,Y轴表示所有线的标准值,Z轴显示每一行.这是我的意思的例子.
我有一段代码,我现在尝试配置以正确输出,但是我不确定如何正确分配y和z变量.df包含5列;时间+ 4种不同的时间序列信号.
plot_ly(数据= dfx = df $ Time,y =标度(df),z =名称(df),类型='scatter3d',模式='线',颜色= c('红色','蓝色','黄色','绿色'))
数据框如下所示:
时间coup.nu Coup.nuti coup.Ca coup.B1 198.001 0.0002630826 0.0003027965 2.141347e-07 12 198.002 0.0002630829 0.0003027953 2.141379e-07 13 198.003 0.0002630833 0.0003027940 2.141412e-07 14 198.004 0.0002630836 0.0003027928 2.141444e-07 15 198.005 0.0002630840 0.0003027916 2.141477e-07 1
我正在尝试使用plotly或ggplot执行渲染.感谢您的帮助!
我从以下来源获得此资源:
如果要避免重塑 data.frame
,可以使用 add_trace
为数据的每一列添加新的跟踪.
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/
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.
这篇关于使用ggplot/plotly在3D中绘制多条时序线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!