如果使用scale_x_datetime,时间序列图将偏移2小时

时间:2016-06-09 22:40:12

标签: r ggplot2 timezone timestamp time-series

问题:

我试图用ggplot绘制时间序列,但由于某种原因,数据偏移了两个小时。

数据:

> test <- structure(list(interval = structure(c(1465423500, 1465423800, 
1465424100, 1465424400, 1465424700, 1465425000, 1465425300, 1465425600, 
1465425900, 1465426200, 1465426500, 1465426800, 1465427100), class = c("POSIXct", 
"POSIXt"), tzone = ""), mean = c(0.339622641509434, 0.132075471698113, 
0.150943396226415, 0.0754716981132075, 2.09433962264151, 0.528301886792453, 
0.867924528301887, 0, 1.47169811320755, 0.30188679245283, 0.132075471698113, 
0.320754716981132, 0.679245283018868)), .Names = c("interval", 
"mean"), class = c("tbl_df", "data.frame"), row.names = c(NA, 
-13L))

> test
    Source: local data frame [13 x 2]     

interval      mean     
(time)     (dbl)     
1  2016-06-09 00:05:00 0.3396226 # First value: 5 minutes past midnight    
2  2016-06-09 00:10:00 0.1320755     
3  2016-06-09 00:15:00 0.1509434     
4  2016-06-09 00:20:00 0.0754717     
5  2016-06-09 00:25:00 2.0943396     
6  2016-06-09 00:30:00 0.5283019     
7  2016-06-09 00:35:00 0.8679245     
8  2016-06-09 00:40:00 0.0000000     
9  2016-06-09 00:45:00 1.4716981     
10 2016-06-09 00:50:00 0.3018868     
11 2016-06-09 00:55:00 0.1320755     
12 2016-06-09 01:00:00 0.3207547     
13 2016-06-09 01:05:00 0.6792453

实施例

这很好用:

g <- ggplot(interval.steps, aes(interval, mean))
g + geom_line()

X axis labels show actual data

但这并不是

g <- ggplot(interval.steps, aes(interval, mean))
g + geom_line() +
    scale_x_datetime(date_labels = '%H:%M') # offsets times by -2 hours

enter image description here

问题

我做错了什么?提前谢谢。

2 个答案:

答案 0 :(得分:3)

看起来scale_x_datetime正在将interval的时区从当地时区更改为UTC。以下功能可以解决问题。

# Source: http://stackoverflow.com/a/11002253/496488
# Put in your local timezone. I've inserted mine just for illustration.
date_format_tz <- function(format = "%H:%M", tz = "America/Los_Angeles") {
  function(x) format(x, format, tz=tz)
}

g <- ggplot(interval.steps, aes(interval, mean))
g + geom_line() +
  scale_x_datetime(labels = date_format_tz())

答案 1 :(得分:2)

时区独立实施

上面的Eipi10&#39; 答案是一个很好的解决方法。但是,我想避免将硬编码时区设置到我的程序中,以使其在任何语言环境中都可以重现。实现这一目标的方法非常简单,只需省略0 2.0 1 2.0 2 3.0 3 NaN dtype: float64 参数:

tz

优势

此方法的优点在于,无论原始变量的时区参数是,是 当前区域设置。

例如,如果您使用以下内容读取时间值:# Generator function to create 'hh:mm' labels for the x axis # without explicit 'tz' specification date_format <- function(format = "%H:%M") { function(x) format(x, format) } ,即使您在津巴布韦,也可以使用正确的X轴标签绘制图表。< / p>