开发者

Placing the x-axis labels between period ticks

开发者 https://www.devze.com 2023-04-06 19:45 出处:网络
I would like to place the x-axis labels between the ticks. For example, by default 开发者_高级运维R produces a graph that looks like this:

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.)

Placing the x-axis labels between period ticks

But I want to move the labels such that the plot looks like this:

Placing the x-axis labels between period ticks

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


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()

Placing the x-axis labels between period ticks

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号