Paul.Rustomji at csiro.au
2009-Apr-23 12:35 UTC
[R] how to control significant digits(?) on axis labels
Dear R mailing list I would like some help on how to get R to display the same number of significant digits (?) for *all* tick marks on axis labels (yet be flexible enough to handle different data sets that vary by 10-1000X). Consider this simple example: #---------------------------# x <- seq(0,2,by=0.5) xlim <- range(pretty(x,n=5)) ylim <- xlim plot(c(xlim[1],xlim[2])~c(ylim[1],ylim[2]),type="n",xlim=xlim,ylim=ylim,xaxt="n",yaxt="n",lty=1,pch=40,xaxs="i",yaxs="i",xlab="",ylab="") par(xaxt="s") par(xaxs="i") at <- pretty(xlim) print(at) axis(1,at=at,labels=at,las=1,tcl=-0.4,lwd=0.8,cex.axis=1,padj=-1) mtext("x",1,line=1.8,cex=1,padj=-0) par(yaxt="s") par(yaxs="i") at <- pretty(ylim) print(at) axis(2,at=at,labels=at,las=1,tcl=-0.4,lwd=0.8,cex.axis=1,padj=0.3,hadj=0.6) mtext("y",2,line=2.1,cex=1,padj=-0.5,las=3) points(c(xlim[1],xlim[2])~c(ylim[1],ylim[2]),type="l",lwd=0.4) points(x,x,col="red") #---------------------------# This results in axis labels of 0, 0.5, 1, 1.5, 2 when I would like 0.0, 0.5, 1.0, 1.5, 2.0. The wierd thing is the print(at) statements above show what I want (ie a consistent number of decimal places) but somewhere in the axis labelling procedure things seem to change. I have tried using> sprintf("%.1f",at)which works for the range of data above, but this then gives unsatisfactory results if the data is 1+ order of magnitude greater (which it could be in the data I need this to work on) in which case the decimal place is best avoided (eg I would like labels of 0, 5, 10, 15, 20 etc rather than 0.0, 5.0, 15.0, 20.0, 25.0) Also I think signif() wouldn't be quite what I want and have tried mucking around with formatC but to no avail. Any ideas? Paul.> sessionInfo()R version 2.8.1 (2008-12-22) i386-pc-mingw32 locale: LC_COLLATE=English_Australia.1252;LC_CTYPE=English_Australia.1252;LC_MONETARY=English_Australia.1252;LC_NUMERIC=C;LC_TIME=English_Australia.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] xlsReadWrite_1.3.3 qualV_0.2-4 KernSmooth_2.22-22 loaded via a namespace (and not attached): [1] tools_2.8.1 Paul Rustomji Rivers and Estuaries CSIRO Land and Water GPO Box 1666 Canberra ACT 2601 ph +61 2 6246 5810 mobile 0406 375 739
I ran into the same problem - for me, wrapping the axis vector in FORMAT worked well (see below); it may or may not be elegant but was effective for your problem I think. JT #at <- pretty(xlim) at <- format(pretty(xlim), digits=2) # at <- pretty(ylim) at <- format(pretty(ylim), digits=2) #---------------------------# This results in axis labels of . . . 0.0, 0.5, 1.0, 1.5, 2.0. -- View this message in context: http://www.nabble.com/how-to-control-significant-digits%28-%29-on-axis-labels-tp23196067p23336221.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Reasonably Related Threads
- problem with gls finding model terms without specifying data=named.object
- Problem loading ReadImages package (and RGooglemaps)
- Read PNG file and display with more than 256 colors RGDAL
- How to set directory Rscript runs in/Sweave output directory
- Is conditional evaluation of R code chunks possible in Sweave ?