Wil*_*unn 13 timestamp r strptime ggplot2
所以我有一组这样的时间戳:
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")
Run Code Online (Sandbox Code Playgroud)
我想制作一个只有时间的直方图.我所做的是在空格处拆分列以仅获取时间,然后转换回POSIXct对象以便qplot绘制它:
library(ggplot2, stringr)
qplot(as.POSIXct(strptime((str_split_fixed(as.character(time), " ", 2)[,2]), "%H:%M:%S")))
Run Code Online (Sandbox Code Playgroud)
但是,输出as.POSIXct(strptime((str_split_fixed(as.character(datetime), " ", 2)[,2]), "%H:%M:%S"))是
"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"
Run Code Online (Sandbox Code Playgroud)
qplot描绘了我想要的东西,但这对我来说似乎是一个令人费解的黑客.当然有更好的方法来做到这一点?我可以转换为纪元时间和情节,但我试图避免不得不这样做作为额外的一步.
更大的问题是,"我如何控制strptime的输出?"
Tri*_*ou. 15
这种方法怎么样?
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)
Run Code Online (Sandbox Code Playgroud)
计算,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)
# 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)
Run Code Online (Sandbox Code Playgroud)
用于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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11705 次 |
| 最近记录: |