4

我试图通读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。

老实说,我会很感激一些帮助。非常感谢

4

3 回答 3

5

您也可以尝试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"))
于 2013-10-08T00:21:02.897 回答
1
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"))
于 2013-10-07T22:18:24.547 回答
0
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")

在此处输入图像描述

于 2019-07-02T10:26:46.227 回答