[R] how to modify the tickment of x-axis

Jim Lemon jim at bitwrit.com.au
Sun Mar 18 11:34:01 CET 2012


On 03/18/2012 02:00 PM, Jie Tang wrote:
> I have found that  the dimension number of label must be equal with the
> dimension of the plot data by your this method.
> if we have two data in every hour,it seems can not show the correct
> tickment?
> .plot(1:20, xaxt = "n")
> axis(1, at = 1:10, label = paste(1:10, "h", sep = "")) # Happy axis!
>
Hi Jie,
What is happening is that if you only pass the "y" values to "plot", it 
makes up the "x" values by using 1 to the number of y values. So:

plot(1:20)

has the "y" values 1 to 20, and because there are twenty values, it also 
has the "x" values of 1 to 20.

plot(11:30)

would have the same "x" values. There are two ways to get the values you 
want on the x axis. Say you have 20 values over 10 hours like this:

values<-c(rnorm(20,5,1))
hours<-as.numeric(paste(rep(8:17,each=2),c("00","30"),sep=""))
plot(hours,values,type="l")

Now your x axis shows hours in a "pretty" sequence. If you want all hours:

plot(hours,values,type="l",xaxt="n")
axis(1,at=seq(800,1700,by=100))

That's the easy way. You can do it like this also:

plot(values,type="l",xaxt="n")
axis(1,at=seq(1,19,by=2),labels=seq(800,1700,by=100))

Got it?

Jim



More information about the R-help mailing list