使用lubridate()固定tick标签/网格线,并在基本r中使用日期/时间图

发布于 2025-01-23 06:35:36 字数 906 浏览 0 评论 0原文

我深表歉意,这不是问题 - 我不知道如何将其纳入一个很好的可复制例子。

我有一个基本r图,我正在尝试做两件事:在tick痕迹上获得 Clean 轴标签,并添加与这些tick标记匹配的网格线,到目前为止,我还没有成功。我正在寻找带有正确标签(即所有日期)的网格线,或者在彼此顶部看起来不像同一标签中的两个标签。我附上了我在下面以及代码下方的图表。我使用lubritation()将我的CSV文件中的日期/时间列转换为一个名为newdate的列。我将不胜感激的任何帮助 - 我一直在尝试这样做一段时间,没有成功,我快到了。谢谢你!

PS4T1$newdate <- with(PS4T1, as.POSIXct(paste(date, time), format="%m-%d-%Y %H:%M"))
st <- substr(PS4T1$date, 12, 13) == '00'

plot(average ~ newdate, data=PS4T1, type='b', col='blue', xaxt='n')
axis(1, PS4T1$newdate[st], labels=F)
mtext(strftime(PS4T1$newdate[st], '%b %d'), 1, at=PS4T1$newdate[st])
grid(nx=NA, ny=NULL)
abline(v=axis.POSIXct(1, pretty(PS4T1$newdate[st])),col = "lightgray", lty = "dotted", lwd = par("lwd"))

I apologize that this isn't question isn't clean - I don't know how make this into a good reproducible example.

I have a base R plot where I'm trying to do two things: get clean axis labels on the tick marks and add gridlines that match with these tick marks and so far I have been unsuccessful. I'm looking for either grid lines with correct labels (i.e. all the dates) or axis labels that don't look like two of the same label on top of each other. I attached the graph I'm getting down below as well as my code. I used lubridate() to transform my date/time columns in my csv file into one column, called newdate. I would appreciate any help - I've been trying to do this for quite some time with no success and I'm almost there. Thank you!

PS4T1$newdate <- with(PS4T1, as.POSIXct(paste(date, time), format="%m-%d-%Y %H:%M"))
st <- substr(PS4T1$date, 12, 13) == '00'

plot(average ~ newdate, data=PS4T1, type='b', col='blue', xaxt='n')
axis(1, PS4T1$newdate[st], labels=F)
mtext(strftime(PS4T1$newdate[st], '%b %d'), 1, at=PS4T1$newdate[st])
grid(nx=NA, ny=NULL)
abline(v=axis.POSIXct(1, pretty(PS4T1$newdate[st])),col = "lightgray", lty = "dotted", lwd = par("lwd"))

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧街凉风 2025-01-30 06:35:36

您可以只使用abline(v = dat $ date [st],...)将垂直网格线与子集日期对齐。

(这样做的更深的原因是“ posixt”格式是1970-01-01-01以来的秒数 - 所谓的Unix Epoch-在整数中,标记为日期(类似于因素);

mtext line =参数默认为零(重复1,1,1在我的以前的答案不是错字,出于懒惰,我忽略了论点)。您可以使用line = .75使标签更近一点。

st <- substr(dat$date, 12, 13) == '00'

plot(dat, type='b', col='blue', xaxt='n')
axis(1, dat$date[st], labels=F)
mtext(strftime(dat$date[st], '%b %d'), side=1, line=.75, at=dat$date[st])
grid(nx=NA, ny=NULL)
abline(v=dat$date[st], col="lightgray", lty="dotted", lwd=par("lwd"))

数据:

set.seed(42)
dat <- data.frame(
  date=seq.POSIXt(as.POSIXct('2021-06-22'), as.POSIXct('2021-06-29'), 'hour'),
  v=runif(169)
)

You may just use abline(v=dat$date[st], ...) to align vertical grid lines with the subsetted dates.

(The deeper reason for this is that "POSIXt" format is the number of seconds since 1970-01-01 - the so called unix epoch - in integers, labelled as date (similar to factors); compare Sys.time() and as.numeric(Sys.time()).)

In mtext you omitted the line= argument which defaults to zero (the repeated 1, 1 in my former answer was no typo, out of laziness I left out the arguments). You could use line=.75 which gets the labels a little closer.

st <- substr(dat$date, 12, 13) == '00'

plot(dat, type='b', col='blue', xaxt='n')
axis(1, dat$date[st], labels=F)
mtext(strftime(dat$date[st], '%b %d'), side=1, line=.75, at=dat$date[st])
grid(nx=NA, ny=NULL)
abline(v=dat$date[st], col="lightgray", lty="dotted", lwd=par("lwd"))

enter image description here

Data:

set.seed(42)
dat <- data.frame(
  date=seq.POSIXt(as.POSIXct('2021-06-22'), as.POSIXct('2021-06-29'), 'hour'),
  v=runif(169)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文