ggplot2 :: scale_x_time:格式化hms对象 [英] ggplot2::scale_x_time: Formatting hms objects

查看:84
本文介绍了ggplot2 :: scale_x_time:格式化hms对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ggplot2在x轴上绘制时间以秒为单位的一些数据.数据是模拟的结果,也可以是在监视的轨迹上计算的结果.

I'm plotting some data with a time-duration in seconds on the x-axis using ggplot2. The data is the result of either a simulation or is computed on a monitored trace.

我正在使用 scale_x_time 来获取正确的标签.但是,由于数据跨越了很长的时间范围,因此我想在标签中包括天数和周数.最小的例子:

I'm using scale_x_time to get proper labels. However, as the data spans a significant time frame, I would like to include the number of days and weeks in the labels. Minimal example:

library(ggplot2)
n = 100
d <- data.frame(
    t = seq(from=0, to=100*24*60*60, length.out=n),
    v = runif(n, min=10, max=20)
)
ggplot(d) + geom_point(aes(x=t, y=v)) + scale_x_time()

有没有一种方法可以使用 hms 包和基数R?理想情况下,我只想给出一个格式字符串,例如%Ww%dd%H:%M:%S" .

Is there a way to achieve just that using the hms package and base R? Optimally, I would like to just give a format string, e.g. "%Ww %dd %H:%M:%S".

hms 软件包包含许多帮助程序功能,用于从 hms 对象中提取信息并格式化对象.这些功能不会导出.我曾希望提取天数,自己格式化天数和周数,然后构造一个新的 hms 对象,并使用附带的 format 函数.但是我不想在自己的代码中重新实现所有 hms 包的逻辑.

The hms package contains plenty of helper functions to extract information from hms objects and format the objects. These functions are not exported. I had hoped to extract the number of days, format the number of days and weeks myself and then construct a new hms object and use the shipped format function. But I don't want to re-implement all the hms package's logic in my own code.

到目前为止,我的解决方法是将 hms 对象转换为 lubridate 期间对象. lubridate 具有很多功能,可以从期间中提取信息.但这对我来说似乎太复杂了.另外,句点表示的人类"时间跨度与用例不匹配.

My workaround thus far has been to convert hms objects to lubridate period objects. lubridate has plenty of functions to extract information from periods. But this seems way too complicated to me. Also, periods express "human" time spans which does not match the use-case.

library(lubridate)

format_dhms <- function(x) {
    p <- as.period(x)

    return(
        format(p)
    )
}

format_wdhms <- function(x) {
    p <- as.period(x)

    w <- floor(day(p) / 7)

    return(
        paste(
            ifelse(w >= 1, paste(w, "w", sep=""), ""),
            ifelse(w >= 1, format(p - weeks(w)), format(p))
        )
    )
}

ggplot(d) + geom_point(aes(x=t, y=v)) + scale_x_time(labels=format_wdhms)

推荐答案

使用此问题/答案作为起点"

Using this question/answer as a starting point "Convert seconds to days: hours:minutes:seconds" and extending to add weeks:

library(ggplot2)
n = 100
d <- data.frame(
  t = seq(from=0, to=100*24*60*60, length.out=n),
  v = runif(n, min=10, max=20)
)

wdhms <- function(t){
  t<-as.numeric(t)
  paste(t %/% (60*60*24*7), formatC(t %/% (60*60*24) %% 7, width = 2, format = "d", flag = "0") 
    ,paste(formatC(t %/% (60*60) %% 24, width = 2, format = "d", flag = "0")
           ,formatC(t %/% 60 %% 60, width = 2, format = "d", flag = "0")
           ,formatC(t %% 60, width = 2, format = "d", flag = "0")
           ,sep = ":")
  )
}
ggplot(d) + geom_point(aes(x=t, y=v)) + scale_x_time(labels=wdhms)

这篇关于ggplot2 :: scale_x_time:格式化hms对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆