如何更改 R 中日期时间序列的 x 轴标签间隔
How to change x-axis label intervals for a datetime series in R
我创建了一个图,其中 x 轴为时间序列,y 轴为计数值。我想知道如何更改时间序列 x 轴上的间隔,使时间间隔为每 3 小时一次。我不确定如何将像 at=seq(0,100,10) 这样的简单方法应用于时间序列。其次,是否可以为数据集中未表示的时间使用文本标签?即下面的示例数据从 09:29 开始以 10 分钟为间隔,但是是否可以在 x 轴上使用落在小时上的时间值?我假设执行此操作的唯一方法是在文本字符串中手动列出时间,但想知道我是否遗漏了什么?任何建议将不胜感激。
示例数据:
> dput(df)
structure(list(datetime = structure(c(1396603740, 1396604340,
1396604940, 1396605540, 1396606140, 1396606740, 1396607340, 1396607940,
1396608540, 1396609140, 1396609740, 1396610340, 1396610940, 1396611540,
1396612140, 1396612740, 1396613340, 1396613940, 1396614540, 1396615140,
1396615740, 1396616340, 1396616940, 1396617540, 1396618140, 1396618740,
1396619340, 1396619940, 1396620540, 1396621140, 1396621740, 1396622340,
1396622940, 1396623540, 1396624140, 1396624740, 1396625340, 1396625940,
1396626540, 1396627140, 1396627740, 1396628340, 1396628940, 1396629540,
1396630140, 1396630740, 1396631340, 1396631940, 1396632540, 1396633140,
1396633740, 1396634340, 1396634940, 1396635540, 1396636140, 1396636740,
1396637340, 1396637940, 1396638540, 1396639140, 1396639740, 1396640340,
1396640940, 1396641540, 1396642140, 1396642740, 1396643340, 1396643940,
1396644540, 1396645140, 1396645740, 1396646340, 1396646940, 1396647540
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), count = c(46,
45, 47, 43, 49, 46, 46, 44, 44, 44, 45, 49, 50, 46, 45, 43, 47,
48, 48, 46, 47, 46, 46, 44, 46, 44, 45, 49, 44, 46, 44, 48, 44,
42, 44, 50, 44, 44, 48, 43, 43, 46, 44, 49, 49, 44, 42, 45, 45,
48, 46, 45, 46, 43, 40, 50, 39, 42, 48, 44, 44, 46, 43, 47, 46,
41, 43, 43, 46, 48, 45, 48, 43, 42)), .Names = c("datetime",
"count"), row.names = c(NA, -74L), class = "data.frame")
>head(df)
datetime count
1 2014-04-04 09:29:00 46
2 2014-04-04 09:39:00 45
3 2014-04-04 09:49:00 47
4 2014-04-04 09:59:00 43
5 2014-04-04 10:09:00 49
6 2014-04-04 10:19:00 46
示例代码(绘制所有日期时间):
#Convert datetime character string to POSIXct format
df$datetime = as.POSIXct(strptime(df$datetime, format="%d/%m/%Y %H:%M:%S"), tz="UTC")
plot(df$datetime, df$count, axes=FALSE)
axis(2,las=2)
axis.POSIXct(1, at=df$datetime, labels=format(df$datetime, “%H:%M")) #datetime format can be changed as desired
lines(df$datetime, df$count, col="grey22")
首先,让我们更正您的 axis.POSIXct
电话:
axis.POSIXct(1, at=df$datetime, format="%H:%M")
参数 format
可让您定义时间格式,而无需一一设置每个标签的格式。 at
参数是我们将用来定义刻度的参数。
首先,您可以使用应用于 POSIXt 对象的 seq
定义一个日期序列(请参阅 ?seq.POSIXt
),其中可以为参数 by
提供自定义名称,例如 "3 hours"
:
A character string, containing one of "sec", "min", "hour", "day", "DSTday", "week", "month" or "year". This can optionally be preceded by a (positive or negative) integer and a space, or followed by "s".
因此:
seq(df$datetime[1], tail(seq$datetime,1), by="3 hours")
# [1] "2014-04-04 09:29:00 UTC" "2014-04-04 12:29:00 UTC" "2014-04-04 15:29:00 UTC" "2014-04-04 18:29:00 UTC" "2014-04-04 21:29:00 UTC"
现在要使序列在整点开始,需要使用 round
(参见 ?round.POSIXt
):
round(df$datetime[1], "hour")
# [1] "2014-04-04 09:00:00 UTC"
seq(round(df$datetime[1],"hour"), tail(df$datetime,1), by="3 hours")
# [1] "2014-04-04 09:00:00 UTC" "2014-04-04 12:00:00 UTC" "2014-04-04 15:00:00 UTC" "2014-04-04 18:00:00 UTC" "2014-04-04 21:00:00 UTC"
总而言之,您的代码变为:
df <- structure(list(datetime = structure(c(1396603740, 1396604340, 1396604940, 1396605540, 1396606140, 1396606740, 1396607340, 1396607940, 1396608540, 1396609140, 1396609740, 1396610340, 1396610940, 1396611540, 1396612140, 1396612740, 1396613340, 1396613940, 1396614540, 1396615140, 1396615740, 1396616340, 1396616940, 1396617540, 1396618140, 1396618740, 1396619340, 1396619940, 1396620540, 1396621140, 1396621740, 1396622340, 1396622940, 1396623540, 1396624140, 1396624740, 1396625340, 1396625940, 1396626540, 1396627140, 1396627740, 1396628340, 1396628940, 1396629540, 1396630140, 1396630740, 1396631340, 1396631940, 1396632540, 1396633140, 1396633740, 1396634340, 1396634940, 1396635540, 1396636140, 1396636740, 1396637340, 1396637940, 1396638540, 1396639140, 1396639740, 1396640340, 1396640940, 1396641540, 1396642140, 1396642740, 1396643340, 1396643940, 1396644540, 1396645140, 1396645740, 1396646340, 1396646940, 1396647540), class = c("POSIXct", "POSIXt"), tzone = "UTC"), count = c(46, 45, 47, 43, 49, 46, 46, 44, 44, 44, 45, 49, 50, 46, 45, 43, 47, 48, 48, 46, 47, 46, 46, 44, 46, 44, 45, 49, 44, 46, 44, 48, 44, 42, 44, 50, 44, 44, 48, 43, 43, 46, 44, 49, 49, 44, 42, 45, 45, 48, 46, 45, 46, 43, 40, 50, 39, 42, 48, 44, 44, 46, 43, 47, 46, 41, 43, 43, 46, 48, 45, 48, 43, 42)), .Names = c("datetime", "count"), row.names = c(NA, -74L), class = "data.frame")
plot(df$datetime, df$count, axes=FALSE)
axis(2,las=2)
axis.POSIXct(1, at=seq(from=round(df$datetime[1],"hour"),
to=tail(df$datetime,1),
by="3 hours"),
format="%H:%M")
lines(df$datetime, df$count, col="grey22")
我创建了一个图,其中 x 轴为时间序列,y 轴为计数值。我想知道如何更改时间序列 x 轴上的间隔,使时间间隔为每 3 小时一次。我不确定如何将像 at=seq(0,100,10) 这样的简单方法应用于时间序列。其次,是否可以为数据集中未表示的时间使用文本标签?即下面的示例数据从 09:29 开始以 10 分钟为间隔,但是是否可以在 x 轴上使用落在小时上的时间值?我假设执行此操作的唯一方法是在文本字符串中手动列出时间,但想知道我是否遗漏了什么?任何建议将不胜感激。
示例数据:
> dput(df)
structure(list(datetime = structure(c(1396603740, 1396604340,
1396604940, 1396605540, 1396606140, 1396606740, 1396607340, 1396607940,
1396608540, 1396609140, 1396609740, 1396610340, 1396610940, 1396611540,
1396612140, 1396612740, 1396613340, 1396613940, 1396614540, 1396615140,
1396615740, 1396616340, 1396616940, 1396617540, 1396618140, 1396618740,
1396619340, 1396619940, 1396620540, 1396621140, 1396621740, 1396622340,
1396622940, 1396623540, 1396624140, 1396624740, 1396625340, 1396625940,
1396626540, 1396627140, 1396627740, 1396628340, 1396628940, 1396629540,
1396630140, 1396630740, 1396631340, 1396631940, 1396632540, 1396633140,
1396633740, 1396634340, 1396634940, 1396635540, 1396636140, 1396636740,
1396637340, 1396637940, 1396638540, 1396639140, 1396639740, 1396640340,
1396640940, 1396641540, 1396642140, 1396642740, 1396643340, 1396643940,
1396644540, 1396645140, 1396645740, 1396646340, 1396646940, 1396647540
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), count = c(46,
45, 47, 43, 49, 46, 46, 44, 44, 44, 45, 49, 50, 46, 45, 43, 47,
48, 48, 46, 47, 46, 46, 44, 46, 44, 45, 49, 44, 46, 44, 48, 44,
42, 44, 50, 44, 44, 48, 43, 43, 46, 44, 49, 49, 44, 42, 45, 45,
48, 46, 45, 46, 43, 40, 50, 39, 42, 48, 44, 44, 46, 43, 47, 46,
41, 43, 43, 46, 48, 45, 48, 43, 42)), .Names = c("datetime",
"count"), row.names = c(NA, -74L), class = "data.frame")
>head(df)
datetime count
1 2014-04-04 09:29:00 46
2 2014-04-04 09:39:00 45
3 2014-04-04 09:49:00 47
4 2014-04-04 09:59:00 43
5 2014-04-04 10:09:00 49
6 2014-04-04 10:19:00 46
示例代码(绘制所有日期时间):
#Convert datetime character string to POSIXct format
df$datetime = as.POSIXct(strptime(df$datetime, format="%d/%m/%Y %H:%M:%S"), tz="UTC")
plot(df$datetime, df$count, axes=FALSE)
axis(2,las=2)
axis.POSIXct(1, at=df$datetime, labels=format(df$datetime, “%H:%M")) #datetime format can be changed as desired
lines(df$datetime, df$count, col="grey22")
首先,让我们更正您的 axis.POSIXct
电话:
axis.POSIXct(1, at=df$datetime, format="%H:%M")
参数 format
可让您定义时间格式,而无需一一设置每个标签的格式。 at
参数是我们将用来定义刻度的参数。
首先,您可以使用应用于 POSIXt 对象的 seq
定义一个日期序列(请参阅 ?seq.POSIXt
),其中可以为参数 by
提供自定义名称,例如 "3 hours"
:
A character string, containing one of "sec", "min", "hour", "day", "DSTday", "week", "month" or "year". This can optionally be preceded by a (positive or negative) integer and a space, or followed by "s".
因此:
seq(df$datetime[1], tail(seq$datetime,1), by="3 hours")
# [1] "2014-04-04 09:29:00 UTC" "2014-04-04 12:29:00 UTC" "2014-04-04 15:29:00 UTC" "2014-04-04 18:29:00 UTC" "2014-04-04 21:29:00 UTC"
现在要使序列在整点开始,需要使用 round
(参见 ?round.POSIXt
):
round(df$datetime[1], "hour")
# [1] "2014-04-04 09:00:00 UTC"
seq(round(df$datetime[1],"hour"), tail(df$datetime,1), by="3 hours")
# [1] "2014-04-04 09:00:00 UTC" "2014-04-04 12:00:00 UTC" "2014-04-04 15:00:00 UTC" "2014-04-04 18:00:00 UTC" "2014-04-04 21:00:00 UTC"
总而言之,您的代码变为:
df <- structure(list(datetime = structure(c(1396603740, 1396604340, 1396604940, 1396605540, 1396606140, 1396606740, 1396607340, 1396607940, 1396608540, 1396609140, 1396609740, 1396610340, 1396610940, 1396611540, 1396612140, 1396612740, 1396613340, 1396613940, 1396614540, 1396615140, 1396615740, 1396616340, 1396616940, 1396617540, 1396618140, 1396618740, 1396619340, 1396619940, 1396620540, 1396621140, 1396621740, 1396622340, 1396622940, 1396623540, 1396624140, 1396624740, 1396625340, 1396625940, 1396626540, 1396627140, 1396627740, 1396628340, 1396628940, 1396629540, 1396630140, 1396630740, 1396631340, 1396631940, 1396632540, 1396633140, 1396633740, 1396634340, 1396634940, 1396635540, 1396636140, 1396636740, 1396637340, 1396637940, 1396638540, 1396639140, 1396639740, 1396640340, 1396640940, 1396641540, 1396642140, 1396642740, 1396643340, 1396643940, 1396644540, 1396645140, 1396645740, 1396646340, 1396646940, 1396647540), class = c("POSIXct", "POSIXt"), tzone = "UTC"), count = c(46, 45, 47, 43, 49, 46, 46, 44, 44, 44, 45, 49, 50, 46, 45, 43, 47, 48, 48, 46, 47, 46, 46, 44, 46, 44, 45, 49, 44, 46, 44, 48, 44, 42, 44, 50, 44, 44, 48, 43, 43, 46, 44, 49, 49, 44, 42, 45, 45, 48, 46, 45, 46, 43, 40, 50, 39, 42, 48, 44, 44, 46, 43, 47, 46, 41, 43, 43, 46, 48, 45, 48, 43, 42)), .Names = c("datetime", "count"), row.names = c(NA, -74L), class = "data.frame")
plot(df$datetime, df$count, axes=FALSE)
axis(2,las=2)
axis.POSIXct(1, at=seq(from=round(df$datetime[1],"hour"),
to=tail(df$datetime,1),
by="3 hours"),
format="%H:%M")
lines(df$datetime, df$count, col="grey22")