如何在 R 中绘制一天中的时间与日历日期的关系

发布于 2024-10-05 20:46:40 字数 808 浏览 1 评论 0原文

我很遗憾在网上搜索了这个简单问题几个小时之后才来到这里发帖。

我有几个数据集要在 R 中绘制,每个数据集包含两列数据:时间、日期。我通过 Rgui 在 Windows 计算机上使用 R 2.11.0。

时间是观察事件的“一天中的时间”。例如,识别为:

Factor w/ 87 levels "5:53","5:54",..: 84 85 85 85 86 ...

日期为日历日期,识别为:

Class 'Date'  num [1:730] 13879 13880 13881 13882 13883 ...

时间值以 24 小时制、h:mm 或 hh:mm 的格式记录。日期值显示为 yyyy-mm-dd。

我想绘制时间(y 轴)与日期(x 轴)的图。

使用

plot(date,time)

给出了一个看起来准确的图,但 y 轴被标记为数字因子值(大约 0 到 90),而不是因子变量所需的按时间排序的水平。 x 轴以所需的、人类可读的格式标记。

我该如何纠正这个问题? R 中是否有可以将“时间”变量转换成的“一天中的时间”格式?随后,我也喜欢对时间值进行算术运算,并且不介意必须携带一列用于绘图的值和一列用于数学的值。

我在网上遇到了几个在 R 中操作(日期+时间)变量的示例,并将它们转换为不同的格式。我不认为这是我的问题,因为我有单独的时间和日期字段,并且想将一个字段与另一个字段进行比较。

预先感谢您的建议或您对网络可访问资源的指导(我所在的位置没有合适的图书馆或书店)。

I regret that I come to post here after being burned-out on hours of Internet searching regarding this simplistic question.

I have several data sets to plot in R, each consisting of two columns of data: time, date. I am using R 2.11.0 on a Windows computer, via the Rgui.

Time is "time of day" that an event is observed. As an example, it is recognized as:

Factor w/ 87 levels "5:53","5:54",..: 84 85 85 85 86 ...

Date is calendar date, recognized as:

Class 'Date'  num [1:730] 13879 13880 13881 13882 13883 ...

The time values are recorded in the format of a 24-hr clock, h:mm or hh:mm. The date values are displayed yyyy-mm-dd.

I want to plot time (y-axis) vs. date (x-axis).

Using

plot(date,time)

gives an accurate-looking plot, but the y-axis is labeled as the numeric factor values (about 0 to 90), rather than the desired, temporally-ordered levels of the factor variable. The x-axis is labeled in the desired, human-readable format.

How can I correct this? Is there a "time of day" format in R that I can convert my "time" variable into? I will subsequently like to do arithmetic on the time values as well, and would not mind having to carry one column of values to use in plotting and one column of values for maths.

I ran across several examples online of manipulation of (date + time) variables in R, and converting those to different formats. I do not believe this is my problem, as I have separate fields for time and date and want to plot one against the other.

My thanks to you in advance for your suggestions, or your directions to a web-accessible resource (no appropriate libraries or bookstores at my location).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

失去的东西太少 2024-10-12 20:46:40

可能有一种更简单的方法可以做到这一点,但您始终可以自己标记 y 轴。调整下面的 ticksAt 向量以找到适合您的数据的内容。

Data <- data.frame(date=Sys.Date()+1:10,time=paste(5,41:50,sep=":"))
with(Data, plot(date,time,yaxt="n"))
ticksAt <- c(1,3,5,7,9)
axis(2, at=ticksAt, labels=as.character(Data$time)[ticksAt])

?plot.zoo 有一些关于如何创建漂亮的轴注释的好示例,尽管其中一些可能是动物园特定的。 ?par 也是一个很好的资源。

There may be an easier way to do this, but you can always label the y-axis yourself. Adjust the ticksAt vector below to find something that looks suitable for your data.

Data <- data.frame(date=Sys.Date()+1:10,time=paste(5,41:50,sep=":"))
with(Data, plot(date,time,yaxt="n"))
ticksAt <- c(1,3,5,7,9)
axis(2, at=ticksAt, labels=as.character(Data$time)[ticksAt])

?plot.zoo has some good examples of how to create pretty axis annotations, though some of them may be zoo-specific. ?par is also a good resource.

悲喜皆因你 2024-10-12 20:46:40

ts 和 timeSeries 是两个不错的选择。
查看
相关

ts and timeSeries are two good choices.
Take a look at
Related

暮倦 2024-10-12 20:46:40

假设您有两个向量,一个是名为“dt”的 Date 类,另一个是名为“tm”的因子:

 x <- paste(as.character(dt[1:2]), as.character(tm))
 strptime(x, "%Y-%m-%d %H:%M")
## [1] "2008-01-01 05:53:00" "2008-01-02 05:54:00"
class(strptime(x, "%Y-%m-%d %H:%M"))
##  [1] "POSIXt"  "POSIXlt"

Let's assume you have two vectors, one of Date class named "dt" and the other a factor named "tm":

 x <- paste(as.character(dt[1:2]), as.character(tm))
 strptime(x, "%Y-%m-%d %H:%M")
## [1] "2008-01-01 05:53:00" "2008-01-02 05:54:00"
class(strptime(x, "%Y-%m-%d %H:%M"))
##  [1] "POSIXt"  "POSIXlt"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文