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?"
In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.
You can plot datetime and duration arrays without converting them to numeric arrays. Most plotting functions accept datetime and duration arrays as input arguments. For example, plot a data set that has datetime values on the x-axis and numeric values on the y-axis.
Using the DateFormatter module from matplotlib, you can specify the format that you want to use for the date using the syntax: "%X %X" where each %X element represents a part of the date as follows: %Y - 4 digit year with upper case Y. %y - 2 digit year with lower case y. %m - month as a number with lower case m.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With