使用lubridate()固定tick标签/网格线,并在基本r中使用日期/时间图
我深表歉意,这不是问题 - 我不知道如何将其纳入一个很好的可复制例子。
我有一个基本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"))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以只使用
abline(v = dat $ date [st],...)
将垂直网格线与子集日期对齐。(这样做的更深的原因是
“ posixt”
格式是1970-01-01-01
以来的秒数 - 所谓的Unix Epoch-在整数中,标记为日期(类似于因素);在
mtext
line =
参数默认为零(重复1,1,1
在我的以前的答案不是错字,出于懒惰,我忽略了论点)。您可以使用line = .75
使标签更近一点。数据:
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 since1970-01-01
- the so called unix epoch - in integers, labelled as date (similar to factors); compareSys.time()
andas.numeric(Sys.time())
.)In
mtext
you omitted theline=
argument which defaults to zero (the repeated1, 1
in my former answer was no typo, out of laziness I left out the arguments). You could useline=.75
which gets the labels a little closer.Data: