使用 Lattice 和将事件绘制为时间序列线图时遇到问题
我有一个数据表,我正在尝试使用 R 中的点阵来绘制图表。该表包含按日期(我已按相应月份的第一天分组)每个单位发生的特定事件的计数,并按日期排序。
> data
Unit Date Count
1 Unit14 2005-01-01 1
2 Unit19 2005-01-01 0
3 Unit13 2006-01-01 8
4 Unit17 2006-01-01 0
5 Unit13 2007-01-01 0
6 Unit14 2007-01-01 4
7 Unit19 2007-01-01 1
8 Unit5 2007-01-01 0
9 Unit9 2007-01-01 1
10 Unit13 2008-01-01 0
...
我在点阵中使用 xyplot 来绘图,如果我只是绘制它们发生的点,它就可以正常工作。我通过单位绘制每个事件得到图像网格,其中 x 轴为日期,y 轴为 Dount。
> xyplot(Count~Date | Unit, data)
但是,当我尝试将其绘制为折线图时,它不明白事件的顺序是按事件时间排列的,并且线条本身会加倍。换句话说,这条线从图表的中间开始,并在点之间形成“星”状图案。
> xyplot(Count~Date | Unit, data, col="blue",type="l")
我怎样才能把它绘制成时间序列?
我对使用 R 绘图还很陌生,因此我们将不胜感激。谢谢。
I have table of data that I am attempting to use lattice in R to graph. The table contains counts of a certain event that occurs per unit by date (which I have grouped by the first day of the corresponding month), sorted by date.
> data
Unit Date Count
1 Unit14 2005-01-01 1
2 Unit19 2005-01-01 0
3 Unit13 2006-01-01 8
4 Unit17 2006-01-01 0
5 Unit13 2007-01-01 0
6 Unit14 2007-01-01 4
7 Unit19 2007-01-01 1
8 Unit5 2007-01-01 0
9 Unit9 2007-01-01 1
10 Unit13 2008-01-01 0
...
I am using xyplot in lattice to plot, and it works fine if I am just plotting the points at which they occurred. I get a grid of images by Unit plotting each event with x-axis as Date, and y-axis as Dount.
> xyplot(Count~Date | Unit, data)
But, when I try to plot it as a line graph it does not understand that the order of events were by time of event, and the line doubles back on itself. In other words, the line starts in the middle of the graphs and makes "star" like patterns between the points.
> xyplot(Count~Date | Unit, data, col="blue",type="l")
How can I get this to plot as time series?
I'm still pretty new to graphing with R, so any help will be greatly appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
点按照它们出现的顺序绘制和连接,
不按时间顺序排列。
如果你对 data.frame 进行排序,这些文物就会消失
在绘图之前。
请注意,ggplot2 不会发生这种情况(除非您明确要求“星形”工件):
The points are drawn and joined in the order in which they appear,
not in chronological order.
Those artefacts will disappear if you sort your data.frame
before plotting.
Note that this does not happen with
ggplot2
(unless your explicitly ask for that "star-shape" artefact):