本文介绍了ggplot2图表的每天中午都会中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在图表的每一天中午添加日轴标签.目前,它会在午夜添加标签,但如果这些标签在每天的中间间隔开,同时保留表示午夜的网格线,则我更希望这样做.我尝试使用 just ,但是结果看起来不太好.有办法吗?
I would like to add day axis labels at noon for each day of my chart. Currently, it adds the labels at midnight, but I'd prefer it if those labels were spaced midway between each day, while retaining grid lines denoting midnight. I tried using hjust but the results didn't look very good. Is there a way to do this?
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() 推荐答案这是一种方法.
首先,创建中午时间数据.使用 seq.Date :
First, create the noontime data. This is quite easy using seq.Date:
然后在您的绘图中添加 geom_vline :
Then add a geom_vline to your plot:
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")
更多推荐
ggplot2图表的每天中午都会中断
发布评论