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

前端 未结 2 1962
[愿得一人]
[愿得一人] 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:13

    It looks like scale_x_datetime is changing the timezone of interval from your local timezone to UTC. The function below should resolve the problem.

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

提交回复
热议问题