ggplot2图表的每天中午都会中断

编程入门 行业动态 更新时间:2024-10-28 19:35:39
本文介绍了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图表的每天中午都会中断

本文发布于:2023-11-22 10:40:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617096.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都会   图表   中午

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!