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

Pau*_*ano 4 plot datetime r ggplot2 posixct

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

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
Run Code Online (Sandbox Code Playgroud)

我将如何以 HH:MM:SS.000 格式在 Yaxis vs Time(x 轴) 上绘制 EcNo,如上所示。

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

Hen*_*rik 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()
Run Code Online (Sandbox Code Playgroud)

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"))
Run Code Online (Sandbox Code Playgroud)