【问题标题】:Time Series plot with "times" in y-axisy 轴上带有“时间”的时间序列图
【发布时间】:2015-12-10 19:56:51
【问题描述】:

假设我有一些活动的完成时间。

completion_data <- data.frame(
    date=c("2009-05-04", "2010-04-07", "2011-04-02", "2011-05-06",
           "2012-06-03", "2012-07-09", "2013-09-03", "2014-02-01"),
    time_taken=c("1:53:01", "1:50:01", "1:30:01", "1:29:01", 
                 "1:28:03", "1:20:01", "1:15:11", "1:12:24"))

library(zoo)
library(chron)
completion_data$date <- as.Date(completion_data$date)
completion_data$time_taken <- chron(times.=completion_data$time_taken)

我想创建一个时间序列图,其中 x 轴为日期,y 轴为所用时间。

completion_ts <- zoo(completion_data$time_taken, completion_data$date)
plot(completion_ts, ylab="Time (H:M:S)", xlab="Date", type='o')

但是,y 轴没有我想要的 H:M:S 格式。例如,第一个点对应于 1:53:01 的时间(即 1 小时 53 分钟 1 秒),我希望 y 轴使用这种格式。

  • 我想使用基本图形来执行此操作,即不使用 ggplot2(如果可能)
  • 我的一个想法是在绘图后使用axis 语句,但不确定是否可以直接绘制times 对象(由chron 创建)

【问题讨论】:

  • 是否可以在 y 轴上绘制日期或时间?我认为这与问题有关。

标签: r time plot time-series chron


【解决方案1】:

自己把标签放在Y轴上:

plot(completion_ts, ylab = "Time (H:M:S)", xlab = "Date", type = 'o', yaxt = "n")
tt <- seq(times("00:00:00"), times("23:59:59"), times("00:10:00"))
axis(2, tt, sub(":00$", "", times(tt)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-20
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多