R:as.POSIXct时区和我的数据集中的scale_x_datetime问题 [英] R: as.POSIXct timezone and scale_x_datetime issues in my dataset
问题描述
我花了一些时间试图弄清楚为什么在应用scale_x_datetime时小时刻度会发生变化。我试图在创建日期/时间列时给出时区。我使用包尺度中的ggplot和scale_x_datetime()。小时刻度错误,该数据点与日期/时间列中的时间不匹配。
以下是处理我的数据集的一些步骤。
DF $ DateTime< -as.POSIXct(DF $ timestamp,format =%m /%d /%y%H :%M,tz =美国/多伦多)
DF $日期< -as.Date(DF $日期时间)
lims< - as.POSIXct(strptime(c 2015-07-21 00:00,2015-07-23 00:00),format =%Y-%m-%d%H:%M),tz =America / Toronto)
ggplot(DF)+ geom_line(aes(x = DateTime,y = -Diff,group = Date))+ scale_x_datetime(limits = lims,breaks = date_breaks(2 hour),labels = date_format(%m /%d%H:%M))
??请帮我弄明白。
非常感谢!
date_format()
需要一个默认设置为UTC
的 tz
参数。因此,您的标签将转换为UTC。要使用America / Toronto时区,您可以执行以下操作: scale_x_datetime(限制=限制,休息= date_breaks (2小时),
pre>
标签=日期格式(%m /%d%H:%M,tz =美国/多伦多))
这个参数是在0.2.5版本中引入的。在更新后必须更改使用
date_format()
在其他时区创建比UTC更新的图。I spent some time trying to figure out why the hour ticks were shifted when scale_x_datetime was applied. I've tried to give the timezone when the Date/Time column was created. I used ggplot and scale_x_datetime() from the package scales. The hour ticks were wrong, which datapoint did not match the time in their Date/Time column.
Here is some procedures to deal with my dataset.
DF$DateTime<-as.POSIXct(DF$timestamp,format="%m/%d/%y %H:%M", tz="America/Toronto") DF$Date<-as.Date(DF$DateTime) lims <- as.POSIXct(strptime(c("2015-07-21 00:00","2015-07-23 00:00"), format = "%Y-%m-%d %H:%M"), tz="America/Toronto") ggplot(DF) + geom_line(aes(x=DateTime, y=-Diff,group=Date)) + scale_x_datetime(limits =lims, breaks=date_breaks("2 hour"), labels=date_format("%m/%d %H:%M"))
Do I miss anything here?? Please help me to figure it out. Many thanks!
解决方案The function
date_format()
takes atz
argument that is by default set to"UTC"
. Therefore, your labels are converted to UTC. To use the time zone "America/Toronto", you can do the following:scale_x_datetime(limits = lims, breaks = date_breaks("2 hour"), labels = date_format("%m/%d %H:%M", tz = "America/Toronto"))
This argument was introduced with version 0.2.5. Code that uses
date_format()
to create plots in other time zones than UTC must be changed after the update.这篇关于R:as.POSIXct时区和我的数据集中的scale_x_datetime问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!