Align grid with ticks

£可爱£侵袭症+ 提交于 2020-01-06 20:36:26

问题


When I let plot draw the axis, grid aligns with ticks by default.

However, my plot is somewhat involved:

mytime <- as.POSIXct("2015-08-20") + seq(0,by=3600,length.out=7*24)
plot(x=mytime,y=rnorm(7*24),xaxt="n")
ticks <- mytime[seq(1,by=12,to=length(mytime))]
axis(1, ticks, strftime(ticks, "%a %H:%M"))
grid(ny=NULL,nx=14)

and I cannot get the grid to align with the ticks:

How do I make them align?


回答1:


Looks like abline is the answer:

mytime <- as.POSIXct("2015-08-20") + seq(0,by=3600,length.out=7*24)
plot(x=mytime,y=rnorm(7*24),xaxt="n")
ticks <- c(mytime[seq(1,by=12,to=length(mytime))],mytime[1]+7*24*3600)
axis(1, ticks, strftime(ticks, "%a %H:%M"))
grid(ny=NULL,nx=NA)
abline(v=ticks[seq(1,length(ticks),2)],lty="dotted",col="lightgray")



来源:https://stackoverflow.com/questions/32120291/align-grid-with-ticks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!