【问题标题】:How to plot time from seconds to HH/MM如何绘制从秒到 HH/MM 的时间
【发布时间】:2020-01-27 14:15:24
【问题描述】:

我有这样的数据

TIME<- c(36655,14330,23344,9992,...)

表示午夜后的秒数。我想使用 ggplot2 生成一个直方图,以便我可以看到数据的分布,但包括 x 轴上的白天时间而不是秒。

目前为止:

ggplot(data=df1, aes(TIME)) + geom_histogram(col="red", fill="green", alpha = .2,bins=9)+ 
labs(title="Histogram for call time") +labs(x="Time", y="Count")

但这只是给出了几秒钟。但是,如果我将其转换为:

TIME <- as.POSIXct(strptime(TIME, format="%R"))

这包括我不想要的今天日期。我只想要时间,即分成 c(0:00, 9:00, 12:00, 18:00, 24:00)。这可能吗?

【问题讨论】:

  • 尝试将 scale_x_datetime(date_labels = "%R") 添加到您的 ggplot 对象中,它应该格式化您的标签以仅显示时间
  • 这项工作,谢谢!

标签: r dataframe ggplot2


【解决方案1】:

根据 cmets,这是可行的:

TIME <- as.POSIXct(strptime(TIME, format="%R"))

ggplot(data=df1, aes(TIME)) + geom_histogram(col="red", fill="green", 
alpha = .2,bins=9)+ scale_x_datetime(date_labels = "%R")

【讨论】:

    猜你喜欢
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 2016-05-04
    • 1970-01-01
    • 2012-10-29
    相关资源
    最近更新 更多