r - 如何在ggplot2中绘制时分秒时间序列

标签 r ggplot2 time-series

使用 ggplot2 绘制每行都有一个 hh:mm:ss 时间戳的时间序列数据的正确方法是什么?例如:

48:09:35  0.2
48:09:36  0.3
48:12:05  0.4
48:35:48  0.5

上面的每个时间戳都是这样的形式:小时:分钟:秒,比如48:09:35

我想在 x 轴上绘制它,在 y 轴上绘制与时间戳相关的值,但是 x 轴的间距适本地用于时-分-秒时间戳。

有没有办法在 R 中的 ggplot2 中做到这一点?

最佳答案

由于您的时间超出每天 24 小时的范围,因此无法使用标准函数(例如 chron 包中的 chron),您可以转换 Time 变量为数值。

df$Time_secs <- unlist(lapply(lapply(strsplit(df$Time, ":"), as.numeric), function(x) x[1]*60^2+x[2]*60+x[3]))
ggplot(df, aes(x = Time_secs, y = Value)) + geom_line() + scale_x_continuous(labels = df$Time)

enter image description here

数据:

dput(df)
structure(list(Time = c("48:09:35", "48:09:36", "48:12:05", "48:35:48"
), Value = c(0.2, 0.3, 0.4, 0.5)), .Names = c("Time", "Value"
), class = "data.frame", row.names = c(NA, -4L))

关于r - 如何在ggplot2中绘制时分秒时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29883630/

相关文章:

R ggplot : Change Grouped Boxplot Median line

R 通过使用facet_wrap和数据子集的函数调用传递ggplot参数

python - 使用自定义大小的间隔和聚合函数连接两个 pandas 数据框

cassandra - 在 Cassandra 中设计时间序列数据库

r - 如何防止 RMySQL 警告 "Unsigned INTEGER in col imported as numeric"?

r - 如何在 R 中解析和构建具有多个值参数的 URL 查询字符串

删除ggplot中所有x轴标签

python - 具有平均时间的 Pandas 数据透视表

r - 如何在Windows 7上安装ROracle软件包?

r - 使用 data.table 进行顺序分组