使用 Lattice 和将事件绘制为时间序列线图时遇到问题

发布于 2025-01-06 23:05:27 字数 810 浏览 2 评论 0原文

我有一个数据表,我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

快乐很简单 2025-01-13 23:05:27

点按照它们出现的顺序绘制和连接,
不按时间顺序排列。
如果你对 data.frame 进行排序,这些文物就会消失
在绘图之前。

# Sample data
n <- 20
k <- 3
d <- data.frame(
  Unit = rep(LETTERS[1:k], each=n),
  Date = rep(sample(seq.Date(Sys.Date(), length=n, by=1)), k),
  Count = rpois(k*n,10)
)
# Unorderd data    
xyplot(Count ~ Date | Unit, data=d, type="l")
# Ordered data
xyplot(Count ~ Date | Unit, data=d[order(d$Date),], type="l")

请注意,ggplot2 不会发生这种情况(除非您明确要求“星形”工件):

library(ggplot2)
ggplot(d, aes(Date, Count)) + geom_line() + facet_grid(~Unit)

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.

# Sample data
n <- 20
k <- 3
d <- data.frame(
  Unit = rep(LETTERS[1:k], each=n),
  Date = rep(sample(seq.Date(Sys.Date(), length=n, by=1)), k),
  Count = rpois(k*n,10)
)
# Unorderd data    
xyplot(Count ~ Date | Unit, data=d, type="l")
# Ordered data
xyplot(Count ~ Date | Unit, data=d[order(d$Date),], type="l")

Note that this does not happen with ggplot2 (unless your explicitly ask for that "star-shape" artefact):

library(ggplot2)
ggplot(d, aes(Date, Count)) + geom_line() + facet_grid(~Unit)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文