2013-04-25 118 views
4

我正在创建每周时间系列图表,并且每星期应该从星期天开始。当我指定scale_x_date(breaks = date_breaks('1 week'))网格线和标签从星期一开始时,结果稍微偏离。我怎么能强迫ggplot scale_x_date本周在周日开始如何使scale_x_date每周以星期日开始

这是例如我的代码

library(ggplot2) 
library(scales) 

data.set <- structure(list(week.start = structure(c(15732, 15739, 
          15746, 15753, 15760, 15767, 15774, 15781, 
          15788, 15795, 15802, 15809), class = 
          "Date"), overtime.avg = c(2.8, 
          2.85666666666667, 2.18333333333333, 
          2.44666666666667, 2.04833333333333, 
          2.45833333333333, 2.12833333333333, 
          1.81666666666667, 1.82166666666667, 
          1.54333333333333, 2.09166666666667, 
          0.970833333333333)), .Names = 
          c("week.start", "overtime.avg"), row.names 
          = 29733:29744, class = "data.frame") 

ggplot(data = data.set, 
     aes(x = week.start, 
      y = overtime.avg)) + 
    geom_line() + 
    geom_point() + 
    scale_x_date(breaks = date_breaks("1 week"), 
       labels = date_format(format = "%Y-%m-%d")) 

回答

3

一种方法是使用功能seq(),并提供自己的破发点开始,第一个星期日(使用最小值的week.start)并且设置by="week"

ggplot(data = data.set,aes(x = week.start,y = overtime.avg)) + 
    geom_line() + 
    geom_point() + 
    scale_x_date(breaks = seq(min(data.set$week.start),max(data.set$week.start),by="week"), 
       labels = date_format(format = "%Y-%m-%d")) 
+0

This works great!谢谢 – ilya 2013-04-25 14:55:32

相关问题