[英]R: How to print only the time on the x axis leaving out the date
I have an xts object, a snapshot of the data is as follow: 我有一个xts对象,数据快照如下:
ts <- structure(c(620519.432512971, 619086.596917204, 620478.784694651,
620997.044378227, 620885.262877848, 620275.545603053), index = structure(c(1519954199.9845,
1519955999.7115, 1519957799.9675, 1519959599.9935, 1519961399.9365,
1519963199.2225), tzone = "", tclass = c("POSIXct", "POSIXt")), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts",
"zoo"), .Dim = c(6L, 1L), .Dimnames = list(NULL, "yy"))
All the timestamps are within the same date , and I am trying to add more tick marks on the x axis (time), which I managed to achieve. 所有时间戳都在同一date内 ,并且我试图在x轴(时间)上添加更多刻度线,这是我设法实现的。 It looks as follow:
它看起来如下:
ggplot(data=ts) +
geom_line(aes(x=Index,y=yy,colour = "yy"),na.rm=T) +
scale_x_datetime(date_breaks ="20 min")
As you can see the timestamps above are correct, but i want to remove the date part and just have the time on the x-axis. 如您所见,上面的时间戳是正确的,但是我想删除日期部分,只在x轴上显示时间。 So I tried the following code:
所以我尝试了以下代码:
ggplot(data=ts) +
geom_line(aes(x=Index,y=yy,colour = "yy"),na.rm=T) +
scale_x_datetime(date_breaks ="20 min",labels = date_format("%H:%M:%S"))
But the times are all incorrect. 但是时代都不对。 What should I do to fix this to make it print only the time, leaving out the date, since the xts data are all within the same date ?
我应该怎么做才能解决此问题,使其仅在时间上打印,而忽略日期,因为xts数据都在同一日期内?
I believe your times are being converted to UTC times when you plot them. 我相信您在绘制时间时会将其时间转换为UTC时间。 You can convert to your timezone.
您可以转换为您的时区。 Here is how I convert to using the "US/Pacific" time zone.
这是我转换为使用“美国/太平洋”时区的方式。 I added the
tz
argument to date_format
我将
tz
参数添加到date_format
ggplot(data=ts) +
geom_line(aes(x=Index,y=yy,colour = "yy"),na.rm=T) +
scale_x_datetime(date_breaks ="20 min",
labels = date_format("%H:%M:%S", tz = "US/Pacific"))
You may have to change the timezone (see OlsonNames()
您可能必须更改时区(请参见
OlsonNames()
It is very simple, 很简单
ggplot(data=ts) +
geom_line(aes(x = Index,y = yy,colour = "yy"), na.rm=T) +
scale_x_datetime(date_breaks = "20 min", date_labels = "%H:%M:%S")
You just have to use parameter date_labels
to set the formatting option of your datetime object. 您只需要使用参数
date_labels
来设置datetime对象的格式选项。
Sources: 资料来源:
- http://ggplot2.tidyverse.org/reference/scale_date.html -http://ggplot2.tidyverse.org/reference/scale_date.html
- https://www.stat.berkeley.edu/~s133/dates.html -https://www.stat.berkeley.edu/~s133/dates.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.