如何在 R 中的 X 轴上绘制时间 (HH:MM:SS)

发布于 2025-01-12 15:47:39 字数 458 浏览 1 评论 0原文

我尝试阅读stackoverflow、博客、书籍等,但无法找到有关在R中以以下格式(HH:MM:SS.000)在x轴上绘制时间以及在y轴上绘制另一个数量的答案轴。我有以下数据集:

Time             EcNo
12:54:09.000    -14.47
12:54:10.000    -17.96
12:54:11.000    -15.97
12:54:12.000    -14.61
12:54:13.000    -12.68
12:54:14.000    -10.73
12:54:15.000    -10.54
12:54:16.000    -11.62
12:54:17.000    -12.49
12:54:18.000    -11.12

如何以 HH:MM:SS.000 格式在 Y 轴与时间(x 轴)上绘制 EcNo,如上所示。

老实说,我希望得到一些帮助。 非常感谢

I have tried to read through stackoverflow, blogs, books etc but have been unable to find the answer on plotting time in the x-axis in the following format(HH:MM:SS.000) in R and another quantity on the y-axis. I have the following dataset:

Time             EcNo
12:54:09.000    -14.47
12:54:10.000    -17.96
12:54:11.000    -15.97
12:54:12.000    -14.61
12:54:13.000    -12.68
12:54:14.000    -10.73
12:54:15.000    -10.54
12:54:16.000    -11.62
12:54:17.000    -12.49
12:54:18.000    -11.12

How would I plot EcNo on Yaxis vs Time(x axis) in the format HH:MM:SS.000 as shown above.

I honestly would appreciate some help.
many thanks

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

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

发布评论

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

评论(3

我的黑色迷你裙 2025-01-19 15:47:39

您也可以尝试ggplot

library(ggplot2)
df$time <- as.POSIXct(strptime(df$Time, format="%H:%M:%S"))

# Automatic scale selection
ggplot(data = df, aes(x = time, y = EcNo)) + geom_point()

scale_x_datetime是一个ggplot函数,但是对于很好的参数date_breaksdate_format 你需要包scales

library(scales)

ggplot(data = df, aes(x = time, y = EcNo)) + geom_point() +
  scale_x_datetime(breaks = date_breaks("1 sec"), labels = date_format("%S"))

ggplot(data = df, aes(x = time, y = EcNo)) + geom_point() +
  scale_x_datetime(breaks = date_breaks("1 sec"), labels = date_format("%OS3"))

ggplot(data = df, aes(x = time, y = EcNo)) + geom_point() +
  scale_x_datetime(breaks = date_breaks("4 sec"), labels = date_format("%M:%S"))

You may also try ggplot:

library(ggplot2)
df$time <- as.POSIXct(strptime(df$Time, format="%H:%M:%S"))

# Automatic scale selection
ggplot(data = df, aes(x = time, y = EcNo)) + geom_point()

scale_x_datetime is a ggplot function, but for the nice arguments date_breaks, and date_format you need package scales:

library(scales)

ggplot(data = df, aes(x = time, y = EcNo)) + geom_point() +
  scale_x_datetime(breaks = date_breaks("1 sec"), labels = date_format("%S"))

ggplot(data = df, aes(x = time, y = EcNo)) + geom_point() +
  scale_x_datetime(breaks = date_breaks("1 sec"), labels = date_format("%OS3"))

ggplot(data = df, aes(x = time, y = EcNo)) + geom_point() +
  scale_x_datetime(breaks = date_breaks("4 sec"), labels = date_format("%M:%S"))
空城之時有危險 2025-01-19 15:47:39
plot(strptime(dta$Time, format="%H:%M:%S"), dta$EcNo, xaxt="n")
axis(1, at=as.numeric(strptime(dta$Time, format="%H:%M:%S")), 
       labels=strftime( strptime(dta$Time, format="%H:%M:%S"),format="%H:%M:%S"))
plot(strptime(dta$Time, format="%H:%M:%S"), dta$EcNo, xaxt="n")
axis(1, at=as.numeric(strptime(dta$Time, format="%H:%M:%S")), 
       labels=strftime( strptime(dta$Time, format="%H:%M:%S"),format="%H:%M:%S"))
很酷又爱笑 2025-01-19 15:47:39
df <- data.frame(
  Time=c('12:54:09.000','12:54:10.000','12:54:11.000','12:54:12.000','12:54:13.000','12:54:14.000','12:54:15.000','12:54:16.000','12:54:17.000','12:54:18.000'),
  EcNo=c(-14.47,-17.96,-15.97,-14.61,-12.68,-10.73,-10.54,-11.62,-12.49,-11.12)
)

op <- options(digits.secs=3)
plot(as.POSIXct(df$Time,format="%H:%M:%OS"),df$EcNo,xaxt="n")
axis.POSIXct(1, as.POSIXct(df$Time,format="%H:%M:%OS"), format="%H:%M:%OS")

输入图片此处描述

df <- data.frame(
  Time=c('12:54:09.000','12:54:10.000','12:54:11.000','12:54:12.000','12:54:13.000','12:54:14.000','12:54:15.000','12:54:16.000','12:54:17.000','12:54:18.000'),
  EcNo=c(-14.47,-17.96,-15.97,-14.61,-12.68,-10.73,-10.54,-11.62,-12.49,-11.12)
)

op <- options(digits.secs=3)
plot(as.POSIXct(df$Time,format="%H:%M:%OS"),df$EcNo,xaxt="n")
axis.POSIXct(1, as.POSIXct(df$Time,format="%H:%M:%OS"), format="%H:%M:%OS")

enter image description here

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