[R] setting the steps for x axis labels on plot

Jim Lemon jim at bitwrit.com.au
Mon Mar 1 12:37:42 CET 2010


On 03/01/2010 10:16 PM, Gonzalo Garcia-Perate wrote:
> Hello, I'm new to R, I've been working with it for the last 2 weeks. I
> am plotting some data and not getting the labels on the x axis I am
> expecting on my plot.
>
>
> my code reads
>
> #hours in the day
> h <- c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
> #hp is a data frame with a pivot table of 25 columns (label and data for
> 24 hours)
> plot(h, as.matrix(hp[1,2:25]), main = hp[1,1], type="l", xlim=c(0,23),
> ylim=c(0,1800), xlab="hour", ylab="visitor activity")
>
>
> the result you can see here:
> http://www.flickr.com/photos/gonzillaaa/4397357491/
>
> I was hoping the steps on the x axis would be every hour or at least
> every two hours I am getting unevenly spaced steps (0, 5, 10, 20) instead.
>
>
>
> apologies if this is too obvious a question, I've looked around on
> documentation etc. but found no reference to this.
>
>
Hi Gonzalo,
The plotting functions try to space the axis tick labels so that there 
is no crowding or overlapping. One way to get around this is to not 
display the x axis on the initial plot (xaxt="n") and then add a custom 
axis. One function that you can use is staxlab in the plotrix package.

staxlab(1,at=0:23,labels=0:23)

although you may want nlines=3 if you are willing to adjust the x axis 
label.

Jim



More information about the R-help mailing list