【问题标题】:how to plot hour-minute-second time series in ggplot2如何在ggplot2中绘制时分秒时间序列
【发布时间】:2015-07-05 04:35:21
【问题描述】:

使用 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 中做到这一点?

【问题讨论】:

    标签: r ggplot2 time-series


    【解决方案1】:

    由于您的时间超出了 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)
    

    数据:

    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))
    

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 2017-03-04
      • 1970-01-01
      • 2021-01-06
      相关资源
      最近更新 更多