R only does natural (neperian) log scales by default, and this is lame.

Here is a simple code to do decimal log scale, pretty much a requirement for scientists…

The force(x/y)lim options works for natural and log scales (for the later case, you need to specify the power of 10 that you want : c(-2,2) fixes the limit from 0.01 to 100)

drawlogaxis <- function(side,range)
{
par(tck=0.02)
# d <- log(range,10)
d <- range
mlog <- floor(min(d))
Mlog <- ceiling(max(d))
SeqLog <- c(mlog:Mlog)
Nlog <- (Mlog-mlog)+1
axis(side,at=SeqLog,labels=10^SeqLog)
ats <- log(seq(from=2,to=9,by=1),10)
mod <- NULL
for(i in SeqLog)
{
mod <- c(mod,rep(i,length(ats)))
}
ats <- rep(ats,Nlog)
ats <- ats+mod
par(tck=0.02/3)
axis(side,at=ats,labels=NA)
}
logplot <- function(x,y,log='xy',...,forceylim=c(0,0),forcexlim=c(0,0))
{
par(tck=0.02)
xlg <- FALSE
ylg <- FALSE
if('x'%in%strsplit(log,'')[[1]]){x <- log(x,10);xlg=TRUE}
if('y'%in%strsplit(log,'')[[1]]){y <- log(y,10);ylg=TRUE}
yl <- ifelse(forceylim==c(0,0),range(y),forceylim)
xl <- ifelse(forcexlim==c(0,0),range(x),forcexlim)
plot(x,y,...,axes=FALSE,ylim=yl,xlim=xl)
if(xlg){drawlogaxis(1,xl)}else{axis(1,at=pretty(xl),labels=pretty(xl))}
if(ylg){drawlogaxis(2,yl)}else{axis(2,at=pretty(yl),labels=pretty(yl))}
box()
}
addlog <- function(x,y,log='xy',...)
{
xlg <- FALSE
ylg <- FALSE
if('x'%in%strsplit(log,'')[[1]]){x <- log(x,10);xlg=TRUE}
if('y'%in%strsplit(log,'')[[1]]){y <- log(y,10);ylg=TRUE}
points(x,y,...)
}

*Related*

To

**leave a comment** for the author, please follow the link and comment on his blog:

** [R] tricks**.

R-bloggers.com offers

**daily e-mail updates** about

R news and

tutorials on topics such as: visualization (

ggplot2,

Boxplots,

maps,

animation), programming (

RStudio,

Sweave,

LaTeX,

SQL,

Eclipse,

git,

hadoop,

Web Scraping) statistics (

regression,

PCA,

time series,

trading) and more...

If you got this far, why not

__subscribe for updates__ from the site? Choose your flavor:

e-mail,

twitter,

RSS, or

facebook...