当前位置: 动力学知识库 > 问答 > 编程问答 >

r - ggplot2 axis as time with 1 hour error

问题描述:

This question already has an answer here:

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

    2 answers

网友答案:

You need to specify the time zone in scale_x_datetime.

The function date_format() is by default set to "UTC". Therefore, your labels are converted to UTC. To use the time zone e.g. I used "Europe/London" (to get your desired output), you can do the following in your ggplot code: labels = date_format("%H:%M", tz = "Europe/London")

But firstly in order to run your code I also had to define what you specified in your code as fmt_decimals So I used this function given by @joran:

fmt_dcimals <- function(decimals=0){
   # return a function responpsible for formatting the 
   # axis labels with a given number of decimals 
   function(x) as.character(round(x,decimals))
}

So your code looks like this:

ggplot(x, aes(x=Chms,y=Breath.d13C)) +
  geom_point() +
  scale_y_continuous(name=expression(delta^13*C["Breath"]*" "("\u2030")),
                     limits=c(-30,-10),
                     breaks=seq(-30,-10,5),
                     labels=fmt_dcimals(1)) +
    scale_x_datetime(name="Collection Time", 
                   labels = date_format("%H:%M", tz = "Europe/London"),
                   date_breaks = "1 hour") 

And output:

网友答案:

The problem lie in the time zone you select, i.e. UTC. You should choose the current time zone. The corrected code is as below

     library(ggplot2)
    ggplot(x, aes(x=Chms,y=Breath.d13C)) +
      geom_point() +
      scale_y_continuous(name=expression(delta^13*C["Breath"]*" "   
         ("\u2030")),
                 limits=c(-30,-10),
                 breaks=seq(-30,-10,5)) +
      scale_x_datetime(name="Collection Time", 
               labels = date_format("2016-08-01 %H:00",""),
               date_breaks = "1 hour")

See the plot as belpw

分享给朋友:
您可能感兴趣的文章:
随机阅读: