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)
Below is an example of how this comes out (actually example is after this plot is looped over 3 times to add each line)
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?
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.
Thanks
JP
EDIT:
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)
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?