如何使scale_x_date周从周日开始 [英] How to make scale_x_date week start with Sunday
本文介绍了如何使scale_x_date周从周日开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建每周时间序列图,并且一周应从星期日开始.当我指定scale_x_date(breaks = date_breaks('1 week'))网格线和标签在星期一开始时,结果看起来略有偏离.如何强制ggplot scale_x_date周从周日开始
I'm creating a weekly time series chart, and week should start with Sunday. When I specify scale_x_date(breaks = date_breaks('1 week')) grid line and labels start on Monday, so results looks slightly off. How can I force ggplot scale_x_date week to start on Sunday
这是我的代码示例
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"))
推荐答案
一种方法是使用函数seq()
,并从第一个星期日开始使用自己的断点(使用的最小值week.start
)并设置
One way would be to use function seq()
and provide your own break points starting with first Sunday (used minimal value of week.start
) and set 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"))
这篇关于如何使scale_x_date周从周日开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文