Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing the x-axis labels between period ticks

Tags:

graph

plot

r

I would like to place the x-axis labels between the ticks.

For example, by default R produces a graph that looks like this: (Note, I added axis(1,c(2001,2002,2003,2004,2005,2006,2007,2008,2009,2010)) to give the larger amount of label years, otherwise R only uses 2002 2004 2006 2008 2010 as labels.) enter image description here

But I want to move the labels such that the plot looks like this: enter image description here

I tried looking, but I don't even know what it's called doing this.

like image 522
OSlOlSO Avatar asked Sep 23 '11 14:09

OSlOlSO


1 Answers

You can offset the labels and the ticks with separate calls to axis.

(The example below does not look much like your data, but the idea is the same.)

Plot whatever, but keep the axes off.

plot(1:10, axes = FALSE)

Plot the labels at a half spacing offset and turn off the ticks. (Reverse the numbers just to be "interesting").

axis(1, at = (1:10) + 0.5, labels = 10:1, tick = FALSE)

Add the ticks back at the normal position, and keep the labels off. Add a box to finish the job.

Be careful though, the labels are now kind of ambiguous in terms of which tick they refer to, and what the tick position actually is (though for a year start to end that should not be a problem).

axis(1, at = (1:10), labels = FALSE, tick = TRUE)
box()

offset labels from ticks

You can use axis(2, ...) to construct the y-axis in the same way, or just use defaults with axis(2).

like image 111
mdsumner Avatar answered Nov 15 '22 20:11

mdsumner