如何只绘制时间戳的时间部分,包括日期? [英] How do I plot only the time portion of a timestamp including a date?
问题描述
datetime< -c(2011-09-28 03: 33:00,2011-08-24 13:41:00,2011-09-19 16:14:00,2011-08-18 11:01:00,2011-09-17 06:35:00,2011-08-15 12:48:00)
I想要制作只有时间的直方图。我所做的就是在这个空间拆分这个列以获得唯一的时间,然后转换回一个POSIXct对象以便qplot绘制它:
库(GGPLOT2,stringr)
qplot(as.POSIXct(strptime((str_split_fixed(as.character(时间), ,2)[2]),%H:% M:%S)))
然而,的输出为。 POSIXct(strptime((str_split_fixed(as.character(datetime),,2)[,2]),%H:%M:%S))
是
2011-10-04 03:33:00 PDT2011-10-04 13:41:00 PDT2011-10- 04 16:14:00 PDT2011-10-04 11:01:00 PDT2011-10-04 06:35:00 PDT2011-10-04 12:48:00 PDT
qplot绘制我想要的图,但是这对我来说似乎是一个令人费解的黑客。当然有更好的方法来做到这一点?我可以转换成时代和阴谋,但我试图避免必须作为一个额外的步骤。
更大的问题是,我该如何控制strptime的输出是什么?
这种方法如何?
require(ggplot2)
dtstring< - c(
2011-09-28 03:33:00,2011-08-24 13 :41:00,2011-09-19 16:14:00,
2011-08-18 11:01:00,2011-09-17 06:35:00, 2011-08-15 12:48:00
)
dtPOSIXct< - as.POSIXct(dtstring)
#提取'date + time'的时间(POSIXct)以小时为数字
dtTime< - as.numeric(dtPOSIXct - TRUNC(dtPOSIXct, 天))
$ b $碱基< - qplot(dtTime)+ xlab( 时隙) + scale_x_datetime(format =%S:00)
print(p)
计算, dtPOSixct - trunc(dtPOSIXct,days)
,以小时为单位提取POSIXct类对象的时间。
对于 ggplot2-0.9.1
:
require(ggplot2)
require(scales)
dtstring< - c(
2011-09-28 03:33:00,2011-08-24 13:41:00,2011-09-19 16:14:00,
2011-08-18 11: 01:00,2011-09-17 06:35:00,2011-08-15 12:48:00
)
dtPOSIXct< - as.POSixct(dtstring)
#将时间'date + time'(POSIXct)以小时为单位提取为数值
dtTime< - as.numeric(dtPOSIXct - trunc(dtPOSIXct,days))
p <-qplot(dtTime)+ xlab(时隙)+
scale_x_datetime(labels = date_format(%S:00))
print(p)
对于 ggplot2-0.9.3.1
:
require(ggplot2)
require(scales)
dtstring< - c(
2011-09-28 03:33:00,2011-08-24 13:41:00,2011-09-19 16:14:00,
2011-08-18 11: 01:00,2011-09-17 06:35:00,2011-08-15 12:48:00
)
dtPOSIXct< - as.POSixct(dtstring)
#提取小时为数字
dtTime< '日期+时间'(POSIXct)的时间; - as.numeric(dtPOSIXct - TRUNC(dtPOSIXct, 天))
类(dtTime)LT; - POSIXct
$ b $碱基< - qplot(dtTime)+ xlab( 时隙)+
scale_x_datetime(标签= DATE_FORMAT( %S:00))
打印( p)
So I have a set of timestamps like this:
datetime<-c("2011-09-28 03:33:00", "2011-08-24 13:41:00", "2011-09-19 16:14:00", "2011-08-18 11:01:00", "2011-09-17 06:35:00", "2011-08-15 12:48:00")
I want to make a histogram of only the times. What I did was to split the column at the space to get only the times, then convert back to a POSIXct object in order for qplot to plot it:
library(ggplot2, stringr)
qplot(as.POSIXct(strptime((str_split_fixed(as.character(time), " ", 2)[,2]), "%H:%M:%S")))
However, the output of as.POSIXct(strptime((str_split_fixed(as.character(datetime), " ", 2)[,2]), "%H:%M:%S"))
is
"2011-10-04 03:33:00 PDT" "2011-10-04 13:41:00 PDT" "2011-10-04 16:14:00 PDT" "2011-10-04 11:01:00 PDT" "2011-10-04 06:35:00 PDT" "2011-10-04 12:48:00 PDT"
qplot plots what I want, but this seems like a convoluted hack to me. Surely there's a better way to do this? I could convert into epoch time and plot that, but I was trying to avoid having to do that as an extra step.
The larger question is, "How do I control the output of strptime?"
How about this approach?
require("ggplot2")
dtstring <- c(
"2011-09-28 03:33:00", "2011-08-24 13:41:00", "2011-09-19 16:14:00",
"2011-08-18 11:01:00", "2011-09-17 06:35:00", "2011-08-15 12:48:00"
)
dtPOSIXct <- as.POSIXct(dtstring)
# extract time of 'date+time' (POSIXct) in hours as numeric
dtTime <- as.numeric(dtPOSIXct - trunc(dtPOSIXct, "days"))
p <- qplot(dtTime) + xlab("Time slot") + scale_x_datetime(format = "%S:00")
print(p)
The calculation, dtPOSIXct - trunc(dtPOSIXct, "days")
, extracts time of POSIXct class objects in hours.
For ggplot2-0.9.1
:
require("ggplot2")
require("scales")
dtstring <- c(
"2011-09-28 03:33:00", "2011-08-24 13:41:00", "2011-09-19 16:14:00",
"2011-08-18 11:01:00", "2011-09-17 06:35:00", "2011-08-15 12:48:00"
)
dtPOSIXct <- as.POSIXct(dtstring)
# extract time of 'date+time' (POSIXct) in hours as numeric
dtTime <- as.numeric(dtPOSIXct - trunc(dtPOSIXct, "days"))
p <- qplot(dtTime) + xlab("Time slot") +
scale_x_datetime(labels = date_format("%S:00"))
print(p)
For ggplot2-0.9.3.1
:
require("ggplot2")
require("scales")
dtstring <- c(
"2011-09-28 03:33:00", "2011-08-24 13:41:00", "2011-09-19 16:14:00",
"2011-08-18 11:01:00", "2011-09-17 06:35:00", "2011-08-15 12:48:00"
)
dtPOSIXct <- as.POSIXct(dtstring)
# extract time of 'date+time' (POSIXct) in hours as numeric
dtTime <- as.numeric(dtPOSIXct - trunc(dtPOSIXct, "days"))
class(dtTime) <- "POSIXct"
p <- qplot(dtTime) + xlab("Time slot") +
scale_x_datetime(labels = date_format("%S:00"))
print(p)
这篇关于如何只绘制时间戳的时间部分,包括日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!