[R] How to control the number format on plot axes ?

jim holtman jholtman at gmail.com
Mon Aug 13 02:30:23 CEST 2007


Here is a way that you can put the formatting that you want; you were
not clear on exactly what you were after.  You can setup the 'labels'
argument for whatever you want.

a<-1:10
myTicks<-c(0.1,1,2,5,10)
# set ylim to range of myTicks that you want
plot(x=a,y=a,log="y",type="p",yaxt="n", ylim=range(myTicks))
# change the sprintf to whatever formatting you want
axis(side=2,at=myTicks,
    labels=ifelse(myTicks >= 1, sprintf("%.0f", myTicks),
sprintf("%0.1f", myTicks)))





On 8/12/07, Sébastien <pomchip at free.fr> wrote:
> Dear R-users,
>
> Basically, everything is in the title of my e-mail. I know that some
> threads from the archives have already addressed this question but they
> did not really give a clear solution.
> Here is a series of short codes that will illustrate the problem:
>
> # First
> a<-1:10
> plot(x=a,y=a,log="y",type="p")
>
> # Second
> a<-1:10
> myTicks<-c(1,2,5,10)
> plot(x=a,y=a,log="y",type="p",yaxt="n")
> axis(side=2,at=myTicks)
>
> # Third
> a<-1:10
> myTicks<-c(0.1,1,2,5,10)
> plot(x=a,y=a,log="y",type="p",yaxt="n")
> axis(side=2,at=myTicks)
>
> # Forth
> a<-0.1:10
> plot(x=a,y=a,log="y",type="p")
>
> In the first and second examples, the plots are identical and the tick
> labels are 1, 2, 5 and 10. In the third, the labels are number in the
> x.0 format (1.0, 2.0, 5.0 and 10.0), even if there is no point below 1.
> The only reason I see is because the first element of myTicks is 0.1.
> And, the forth example is self-explanatory.
> Interestingly, the 'scales' argument of xyplot in the lattice package do
> not add these (unnecessary) decimals on labels greater than 1.
>
> Do you know how I could transpose the behavior of the lattice 'scales'
> argument to the 'axis' function ?
>
> Thank you
>
> PS: No offense, but please don't suggest I use lattice. I have to go for
> base R graphics in my full-scale project (it is a speed issue).
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list