Time series plot gets offset by 2 hours if scale_x_datetime is used

前端 未结 2 1956
[愿得一人]
[愿得一人] 2020-12-02 00:22

Problem:

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

Data:

> test <- struc         


        
2条回答
  •  [愿得一人]
    2020-12-02 01:00

    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.

提交回复
热议问题