R: as.POSIXct timezone and scale_x_datetime issues in my dataset

后端 未结 2 667
心在旅途
心在旅途 2020-12-18 21:51

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 crea

相关标签:
2条回答
  • 2020-12-18 22:45

    Update for ggplot 3+. scale_x_datetime allows you to set the x-axis time zone directly (using a syntax slightly different than that given in older answers). The correct code now is:

    scale_x_datetime(limits = lims, breaks = date_breaks("2 hour"),
                     date_labels = "%m/%d %H:%M",
                     timezone = "America/Toronto")
    
    0 讨论(0)
  • 2020-12-18 22:49

    The function date_format() takes a tz 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.

    0 讨论(0)
提交回复
热议问题