jprockbelly
jprockbelly

Reputation: 1607

R: Finding the location of tick marks when plotting dates

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)

output from print statement with date

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?

enter image description here

Upvotes: 3

Views: 1570

Answers (3)

josep maria porr&#224;
josep maria porr&#224;

Reputation: 1388

The answer to your questions are the following ones:

Is there a way to find out the location of the tickmarks that have been added?

Just after the plot function call, use the function axis.Date

x_ticks <- axis.Date(1, data$Date, labels = FALSE)

Variable x_ticks contains the location of the ticks used in the x axis (identified by 1 in the first parameter of the function).

And more importantly, is there then a way to set the guidelines to match these locations?

grid(NA, NULL, lwd=3)
x_ticks <- axis.Date(1, data$Date, labels = FALSE)
abline(v = x_ticks, lty = 3, col ="lightgray", lwd = 3)

With the NA in the first parameter of the grid function (nx = NA), you avoid that the vertical guidelines are printed in the wrong places. The second gives you the location of the ticks. Finally, the third prints the guidelines in the right locations.

In this way, the plot you obtain looks like, I guess, the one you expected enter image description here

Upvotes: 0

mnel
mnel

Reputation: 115382

There is a function Axis.Date which creates appropriately pretty axis tick locations for an axis for a Date object. This does not change par('xaxp') which grid uses to find the locations of the tick marks.

We can get around by creating the axis manually (and saving the values)

# assuming your data is called DD
DD$Date <- as.Date(as.character(DD$Date), format = '%d/%m/%Y')
# don't plot the x axis (xaxt = 'n')
plot(-dbns~Date,data = DD[order(DD$Date),], type="l", xaxt = 'n')
# create the axis and  save the tick locations
at <- as.numeric(Axis(side = 1, x = DD$Date))
grid(NA,NULL,lwd=3)
abline(v = at,lty = "dotted",col = "lightgray",lwd = 3)

Upvotes: 3

joran
joran

Reputation: 173517

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.

The documentation further notes that if you need finer control, you should use abline to place them directly.

So you probably want to do this:

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)

Upvotes: 3

Related Questions