沿着日期刻度线放置网格
我有以下数据:
x=strptime(20010101:20010110)
y=1:10
z=data.frame(x,y)
所以我的数据是这样的:
x y
1 2001-01-01 1
2 2001-01-02 2
3 2001-01-03 3
4 2001-01-04 4
5 2001-01-05 5
6 2001-01-06 6
7 2001-01-07 7
8 2001-01-08 8
9 2001-01-09 9
10 2001-01-10 10
当我使用以下方法在基础中创建绘图时:
plot(x,y)
grid(NULL,NULL)
我的垂直网格与日期刻度线不对齐。我知道这似乎是一个非常简单的问题,但我还没有在任何地方找到解决方案。有没有一种方法可以使用底座让垂直网格与日期刻度线对齐,而不需要我这样做:
abline(v=as.numeric(strptime(c(20010102,20010104,20010106,20010108,20010110),'%Y%m%d')))
我有很多具有不同日期的图,我真的很想尽可能地自动化此操作,希望使用基地。
I have the following data:
x=strptime(20010101:20010110)
y=1:10
z=data.frame(x,y)
So my data is this:
x y
1 2001-01-01 1
2 2001-01-02 2
3 2001-01-03 3
4 2001-01-04 4
5 2001-01-05 5
6 2001-01-06 6
7 2001-01-07 7
8 2001-01-08 8
9 2001-01-09 9
10 2001-01-10 10
When I create a plot in base using:
plot(x,y)
grid(NULL,NULL)
My vertical grid does not align with the date tick marks. I know this seems like a pretty simple problem, but I have not found a solution to this anywhere. Is there a way to get the vertical grid to align with the date tick marks using base that does not require me to do this:
abline(v=as.numeric(strptime(c(20010102,20010104,20010106,20010108,20010110),'%Y%m%d')))
I have a lot of plots with different dates and I would really like to automate this as much as possible, hopefully using base.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
函数
axis
绘制轴、刻度线和标签,并将刻度线位置作为向量返回。由于您有
Date
数据,因此需要使用axis.Date
来执行此操作,然后使用abline
绘制网格:The function
axis
draws your axes, tick marks and labels, and returns the tick mark positions as a vector.Since you have
Date
data, you need to useaxis.Date
to do this, and then useabline
to plot the grid:正如
?grid
帮助文件所述,“如果需要更多微调,[您可以]直接使用abline(h = ., v = .)
”。这需要更多的工作,但不多,如果您想经常使用它,很容易将其封装在一个函数中。这是一种方法:
As the
?grid
help file says, "if more fine tuning is required, [you can] useabline(h = ., v = .)
directly". It's a little bit more work, but not much, and would be easy enough to wrap up in a function if you want to use it often.Here's one way to do it:
abline
可以从 POSIXlt 向量中提取日期刻度(通过 strptime)。我建议创建您自己的函数,该函数将添加水平和垂直网格。
abline
can extract the date ticks from your POSIXlt vector (via strptime).I would suggest creating your own function which would add both horizontal and vertical grids.
我不熟悉复杂的基本情节。但是,ggplot 有效地做到了这一点。
I'm not familiar with the intricacies of the base plots. But, ggplot does this effectively.