2017-08-04 269 views
2

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

从我所了解的情况来看,这是一个与我的变量如何输入代码有关的问题。但不完全知道如何解决这个问题?

假设这是我的变量做的,我已经印刷低于我的数据集的结构:

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

非常感谢提前,道歉的菜鸟问题!

回答

3

您的数据按错误顺序排序,顺序依次递减。对于时间序列,它需要在日期进行排序。尝试这样做:

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

所以你的数据帧的正确排序,然后用绘图:

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

注意~列名Date.receivedn之前,这是必要的,让plot_ly知道你是指到数据帧Time的列名称。


前: Before

后:After

+0

同样,我能说什么 - 非常感谢你弗洛里安! – Pryore