Plotly时间序列 - 水平绘制的线条

时间:2017-08-04 07:49:39

标签: r date plotly timeserieschart

我想这是一个非常简单的问题,但我试图在Plotly(R)中绘制时间序列,每次我尝试绘制时 - 线条自动呈现y轴(即面向水平方向)。

据我所知,这是一个与我的变量如何输入代码有关的问题。但不完全确定如何解决这个问题...

假设这与我的变量有关,我在下面打印了数据集的结构:

 Classes ‘tbl_df’, ‘tbl’ and 'data.frame':  53 obs. of  2 variables:
 $ Date.received: Date, format: "2017-06-29" "2017-06-22" "2017-05-16" "2017-06-23" ...
 $ n            : num  20 17 14 13 12 12 12 11 11 11 ...

我的Plotly代码如下:

 plot_ly(Time, x = Date.received, y = n, mode = "line")

结果是:

PLotly time series with horizontal lines

非常感谢,为新秀提问道歉!

1 个答案:

答案 0 :(得分:3)

您的数据顺序错误,按n的减少值排序。对于时间序列,需要按日期排序。尝试做:

Time = Time[order(Time$Date.received),]

因此,您的数据框已正确排序,然后使用以下内容进行绘图:

plot_ly(Time, x = ~Date.received, y = ~n, mode = "line")

请注意列名~Date.received之前的n,这是让plot_ly知道您指的是数据帧时间的列名所必需的。

在: Before

在:After

相关问题