如果使用scale_x_datetime,则时间序列图将偏移2小时 [英] Time series plot gets offset by 2 hours if scale_x_datetime is used

查看:265
本文介绍了如果使用scale_x_datetime,则时间序列图将偏移2小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我试图用ggplot绘制一个时间序列,但由于某些原因数据被抵消了两个小时。



资料:



 >测试<  - 结构(列表(间隔=结构(c(1465423500,1465423800,
1465424100,1465424400,1465424700,1465425000,1465425300,1465425600,
1465425900,1465426200,1465426500,1465426800,1465427100), = c(POSIXct,
POSIXt),tzone =),mean = c(0.339622641509434,0.1132075471698113,
0.150943396226415,0.0754716981132075,2.09433962264151,0.528301886792453,
0.867924528301887,0, 1.47169811320755,0.30188679245283,0.132075471698113,0
0.320754716981132,0.6679245283018868)),.Names = c(interval,
mean),class = c(tbl_df,data.frame),row .names = c(NA,
-13L))

>测试
来源:本地数据框[13 x 2]

间隔均值
(时间)(dbl)
1 2016-06-09 00:05:00 0.3396226#第一个值:午夜过后5分钟
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()



但这不是:

  g < -  ggplot(interval.steps ,aes(interval,mean))
g + geom_line()+
scale_x_datetime(date_labels ='%H:%M')#偏移时间减去-2小时



问题



我做错了什么?

解决方案

时区独立实现



上面的Eipi10's 答案是一个很好的解决方法。但是,我想避免将时区设置到我的程序中,以尝试在任何语言环境中重现。实现这一点的方法非常简单,只需省略 tz 参数:

 #生成器函数为x轴创建'hh:mm'标签
#没有显式的'tz'说明
date_format< - function(format =%H:%M){

函数(x)格式(x,格式)
}



< h2> Advantage

这种方法的优点在于它无论原始变量的时区参数和 em>当前语言环境。例如,如果你的时间值是用这样的内容读入的: as.POSIXct(interval,format ='%H:%M' ,tz ='Pacific / Honolulu'),即使您在津巴布韦等地,图表仍会绘制正确的X轴标签。


Problem:

I'm trying to plot a time series with ggplot but the data gets offset by two hours for some reason.

Data:

> 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

Example

This works fine:

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

But this doesn't:

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

Question

What am I doing wrong? Thanks in advance.

解决方案

Time zone independent implementation

Eipi10's answer above is a good workaround. However, I wanted to avoid hardcoding a time zone setting into my program in at attempt to make it reproducible in any locale. The way to achieve this is very simple, just leave out the tz parameter:

# 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)
}

Advantage

The advantage of this method is that it works regardless of the time zone parameter of the original variable and the current locale.

For example if your time values were read in with something like this:as.POSIXct(interval, format = '%H:%M', tz = 'Pacific/Honolulu'), the graph will still be plotted with the correct X axis labels, even if you're in, say, Zimbabwe.

这篇关于如果使用scale_x_datetime,则时间序列图将偏移2小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆