R:在绘制日期时找到刻度线的位置 [英] R: Finding the location of tick marks when plotting dates
问题描述
我正在运行一个R脚本,该脚本使用 plot
随时间绘制数据并以 as.Date
提取一些数据.然后,我使用 grid
添加以添加如下准则:
I'm running an R script which plots data over time using plot
and some data taken as.Date
. I am then adding using grid
to add guidelines like so:
plot(as.Date(data[["Date"]]), -data[["dbns"]], lwd=1, col = colours[i], type="l",ylim=ylimit, xlim=xlimit, xlab="", ylab =ylabel)
grid(NULL,NULL,lwd=1)
下面是一个如何显示结果的示例(实际上是在将此图循环3次以添加每行之后的示例)
Below is an example of how this comes out (actually example is after this plot is looped over 3 times to add each line)
我遇到的问题是,在 grid
中添加的指南与 plot
中添加的默认刻度线不一致.默认情况下,plot函数每10年添加一次滴答,这就是我想要的.查看 axTicks(1)
给我 0 5000 10000 15000
,它是准则的位置,以1970年1月1日之后的天数为单位.
The problem I am having is that the guidelines added with grid
do not line up with the default tick marks added by plot
. The plot function by default adds tick every 10 years, which is what I want. Looking at axTicks(1)
gives me 0 5000 10000 15000
which is the locations of guidelines in units of days after 1/1/1970.
是否有办法找出已添加的刻度线的位置?而且更重要的是,然后有一种方法可以设置准则以匹配这些位置吗?
Is there a way to find out the location of the tickmarks that have been added? And more importantly, is there then a way to set the guidelines to match these locations?
我意识到,在这个示例中,刻度是 [0,365.25 * 10,365.25 * 20等]
,但是解决一般情况的答案会很好.
I realise that for this example the ticks are [0, 365.25*10, 365.25*20 etc.]
but an answer which solves a general case would be nice.
谢谢
JP
这是一个可复制的示例,用于确切说明我的意思.
Here is a reproducible example to show exactly what I mean.
data <- read.csv("test.csv"), header =TRUE)
data[["Date"]] <- as.Date(data[["Date"]], format = "%d/%m/%Y")
data<-data[order(as.Date(data$Date)),]
plot(as.Date(data[["Date"]]), -data[["dbns"]], type="l", xlim=xlimit)
grid(NULL,NULL,lwd=3)
文件test.csv如下所示(实际数据的提取)
Where the file test.csv looks like this (extract of actual data)
bore Date dbns
42230210A 20/01/2009 13.13
42230210A 8/05/2009 13.21
42230210A 18/06/2009 13.19
42230210A 3/08/2009 13.2
42230210A 2/09/2009 13.25
42230210A 1/10/2009 13.3
42230210A 3/12/2009 13.32
42230210A 24/02/2010 13.3
42230210A 18/05/2010 13.3
42230210A 25/04/2012 11.45
42230210A 27/09/1966 11.18
42230210A 28/10/1969 11.14
42230210A 6/01/1970 11.03
42230210A 5/06/1973 10.68
42230210A 28/08/1973 10.63
42230210A 2/10/1973 10.73
42230210A 4/12/1973 10.7
42230210A 20/02/1980 11.39
42230210A 29/04/1980 11.45
42230210A 27/08/1980 11.45
42230210A 22/06/1988 11.14
42230210A 27/02/1996 11.78
42230210A 6/08/1996 11.68
42230210A 8/02/2000 11.64
42230210A 8/06/2000 11.71
42230210A 7/09/2000 11.75
42230210A 15/07/2008 13.01
其输出如下.准则偏离了刻度线?
The output of which is below. The guidelines are offset from the tick marks?
推荐答案
所以检查?grid
表明发生了什么事,它是根据默认"刻度线位置放置网格线,但由于您已绘制日期对象,因此R并未使用默认值"(在将日期视为数字变量的意义上)来放置刻度线.
So checking ?grid
suggests that what's going on is that it is placing the grid lines based on the "default" tick mark locations, but since you plotted a date object, R didn't use the "default" (in the sense of treating the date as a numeric variable) to place the tick marks.
文档进一步指出,如果需要更好的控制,则应使用 abline
直接放置它们.
The documentation further notes that if you need finer control, you should use abline
to place them directly.
所以您可能想这样做:
plot(as.Date(data[["Date"]]), -data[["dbns"]], type="l")
grid(NA,NULL,lwd=3)
v <- as.numeric(as.Date(paste0(c(1970,1980,1990,2000,2010),'-01-01')))
abline(v = v,lty = "dotted",col = "lightgray",lwd = 3)
这篇关于R:在绘制日期时找到刻度线的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!