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

Plotly time series - lines plotting horizontally

我想这是一个非常简单的问题,但我正在尝试在 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")

结果是:

非常感谢,对于菜鸟问题​​深表歉意!

您的数据顺序错误,它是按 n 的降序排列的。对于时间序列,需要按日期排序。尝试这样做:

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

因此您的数据框已正确排序,然后绘图:

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

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


之前:

之后: