0

As a gnuplot user for many years, I'm going to despair with R trying to align the grid for time-based values to the x-axis labels, i.e. to some "reasonable" multiples of human time (minutes, hours, days, etc.). In Gnuplot it's more or less automatic...

For example I have these time values (I still couldn't find out how to use dump() properly, so a simplified example without measurement data):

> range(perf$time)
[1] "2016-11-22 02:26:02 CET" "2016-11-22 04:02:12 CET"
> length(perf$time)
[1] 49
> plot(perf$time, 1:length(perf$time))
> grid()
>

The plot window looks like this: R plot window with time-based mis-aligned grid So you see that "3:00" matches a grid line, but no other value. I tried to make the number of lines matching, but couldn't get the alignment right. Isn't there a simple solution?

None of the solutions for "Placing the grid along date tickmarks" uses grid(), so is grid() rather useless?

U. Windl
  • 3,480
  • 26
  • 54

1 Answers1

0

If you work with time dependent data I recommend you to use xts package. It makes all formatting out of the box.

library(xts)
perf.xts <- xts(1:length(perf$time), perf$time)
plot(perf.xts, type="p")

Without the xts package you can solve the problem too in a little bit trickier way. You need supress the x-grids. POSIXct class times are large integers and I think grid() uses these numbers to calculate lines position. Example below works if you use times with POSIXct class. (For other time classes find the different method of axis.)

plot(perf$time, 1:length(perf$time))
grid(nx = NA, ny = NULL)
axis.POSIXct(1, perf$time, tck=1, lty = "dotted", col = "lightgray")
kaliczp
  • 457
  • 1
  • 12
  • 18
  • Sorry, but having to enter explicit data for the grid is the trivial solution, just as manually adding axes. Isn't there an elegant solution (that works with base)? – U. Windl Nov 24 '16 at 10:21
  • @Windl Are you tired the first solution? That is absolutely automatic. You need convert your data (perhaps data.frame) to xts. You did not give a sample dataset but it is worth to note [it works with numerical columns.](http://stackoverflow.com/questions/6550482/convert-data-frame-to-xts-object-and-preserve-types) – kaliczp Nov 24 '16 at 11:49
  • @Windl Ah! Sorry, I modified the solution wiht base graphics. Try it out! Or you can see the different solution in the [duplicated question](http://stackoverflow.com/questions/9119323/placing-the-grid-along-date-tickmarks). – kaliczp Nov 24 '16 at 11:55