我如何只绘制时间戳的时间部分,包括日期?

时间:2021-04-04 22:21:14

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:

我想做一个只有时间的直方图。我所做的是在空间上分割列以得到唯一的时间,然后转换回POSIXct对象,以便qplot绘图:

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

然而,asn . posixct (strptime(str_split_fixed) (as.character(datetime), " " " ", 2 ", 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"

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.

qplot将绘制我想要的图形,但对我来说,这似乎是一个复杂的黑客。肯定有更好的方法来做这件事吗?我可以把它转换成纪元时间并绘制出来,但我尽量避免把它作为额外的步骤。

The larger question is, "How do I control the output of strptime?"

更大的问题是,“如何控制strptime的输出?”

2 个解决方案

#1


13  

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.

通过计算,dtPOSIXct - trunc(dtPOSIXct,“days”),以小时为单位提取POSIXct类对象的时间。

我如何只绘制时间戳的时间部分,包括日期?

For ggplot2-0.9.1:

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:

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)

#2


4  

Just use the basic tools as they were intended:

只要按预期使用基本工具即可:

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")
datetime <- as.POSIXct(dtstring)
library(ggplot2)
qplot(datetime)

The format of your strings is the default for parsing with as.POSIXct, see ?strptime for details or if you have something other than this format.

字符串的格式是用于解析的默认格式。POSIXct,参见?strptime获取详细信息,或者如果您有其他格式的内容。

If you want a specific string format from your date-time values, use format, as in

如果您希望从日期-时间值中获得特定的字符串格式,请使用format,如in

format(datetime, "%d-%b")
[1] "28-Sep" "24-Aug" "19-Sep" "18-Aug" "17-Sep" "15-Aug"

Again, see ?strptime for details. If you really want to trash the time values, you can use the Date class. Just be aware that date-times or dates need their full structure, any other representation is just formatted text.

再一次,详情见strptime。如果您真的想要垃圾时间值,可以使用Date类。请注意,日期时间或日期需要它们的完整结构,任何其他表示都只是格式化的文本。

qplot(as.Date(datetime))

qplot(as.Date(datetime))

#1


13  

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.

通过计算,dtPOSIXct - trunc(dtPOSIXct,“days”),以小时为单位提取POSIXct类对象的时间。

我如何只绘制时间戳的时间部分,包括日期?

For ggplot2-0.9.1:

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:

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)

#2


4  

Just use the basic tools as they were intended:

只要按预期使用基本工具即可:

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")
datetime <- as.POSIXct(dtstring)
library(ggplot2)
qplot(datetime)

The format of your strings is the default for parsing with as.POSIXct, see ?strptime for details or if you have something other than this format.

字符串的格式是用于解析的默认格式。POSIXct,参见?strptime获取详细信息,或者如果您有其他格式的内容。

If you want a specific string format from your date-time values, use format, as in

如果您希望从日期-时间值中获得特定的字符串格式,请使用format,如in

format(datetime, "%d-%b")
[1] "28-Sep" "24-Aug" "19-Sep" "18-Aug" "17-Sep" "15-Aug"

Again, see ?strptime for details. If you really want to trash the time values, you can use the Date class. Just be aware that date-times or dates need their full structure, any other representation is just formatted text.

再一次,详情见strptime。如果您真的想要垃圾时间值,可以使用Date类。请注意,日期时间或日期需要它们的完整结构,任何其他表示都只是格式化的文本。

qplot(as.Date(datetime))

qplot(as.Date(datetime))