2012-04-25 33 views
1

我想在我的图表的每一天中午添加日轴标签。目前,它在午夜添加了标签,但如果这些标签每天中间间隔一段距离,我更喜欢它,同时保留表示午夜的网格线。我尝试使用hjust,但结果看起来不太好。有没有办法做到这一点?ggplot2图表中每天中午都会打破轴线

library(ggplot2) 
library(scales) 

dat <- data.frame(time_value=seq(as.POSIXct("2011-07-01"), length.out=24*30, by = "hours"), 
        usage_value=sample(1:10, 24*30, replace=TRUE), 
        group=1) 
dat$week <- format(dat$time_value, '%W') 
dat <- subset(dat, week == 27) 

ggplot(dat, aes(x=time_value, y=usage_value, group=1)) + 
    scale_x_datetime(breaks='day', labels=date_format('%A')) + 
    geom_line() 
+0

不知道,但也许看看''符= date_breaks使用date_breaks(...) '' – Idr 2012-04-25 20:28:05

回答

2

这是一种方法。

首先创建中午数据。这是很容易使用seq.Date

然后添加一个geom_vline到您的情节:

noon <- data.frame(
    x=with(dat, seq(from=min(time_value), to=max(time_value), by="1 day"))+12*60*60 
) 

ggplot(dat, aes(x=time_value, y=usage_value, group=1)) + 
    geom_line() + 
    geom_vline(data=noon, aes(xintercept=x), col="blue") 

enter image description here

+0

谢谢,虽然我正在寻找一种方法将轴标签转移到中午线的位置。 – 2012-04-25 19:13:16

+2

@ErikShilts只需将Andrie构造的日期序列传递给breaks参数:'scale_x_datetime(breaks = noon $ x,...)'。 – joran 2012-04-26 00:53:37

+0

美丽,谢谢。 – 2012-04-26 15:20:47