# [R] nice log-log plots

ElManuelito emmanuel.branlard at gmail.com
Sun Mar 14 17:12:38 CET 2010

```Here's what I do,
I'm open to any suggestions and improvements...

seq.log=function(x1,x2="1"){
if(length(x1)==2){x2=x1[2];x1=x1[1];}
n1=floor(log(x1)/log(10))
n2=floor(log(x2)/log(10))
suff=NULL;
if(x1==1000){n1=n1+1;}
if(x2==1000){suff=1000;}
if(n2-(log(x2)/log(10))==0){
n2=n2-1;
suff=x2;
}
dn=n2-n1+1
return(c(as.vector( t(10^(log(matrix(1:9,dn,9,byrow=T)
)/log(10)+matrix(n1:n2,dn,9) ))),suff))
}

nextint=function(v){
w=numeric(length(v))
for(i in 1:length(v)){
if(v[i]<0){
w[i]=floor(v[i]);
}else{
w[i]=ceiling(v[i]);
}
}
return(w)
}

range.log=function(X){
return(10^nextint(log(range(X,na.rm=T))/log(10)))
}
return(nextint(log(range(X,na.rm=T))/log(10)))
}

plot.loglog=function(X,Y,majorgrid=T,minorgrid=T,ygrid=Y,xlim=range(X),ylim=range(Y),...){
### Work on axis, ticks and labels
rX=range(xlim,na.rm=T)
rY=range(ylim,na.rm=T)
#minor grid
xmgrid=seq.log(rX)
ymgrid=seq.log(rY)
#Major grid
px=nextint(log(rX)/log(10))
Vpx=px[1]:px[2];
xMgrid=10^(Vpx)
py=nextint(log(rY)/log(10))
Vpy=py[1]:py[2];
yMgrid=10^(Vpy)

xlim=range(xMgrid);
ylim=range(yMgrid);

### Plotting
plot.empty(rX,rY,log="xy",xlim=xlim,ylim=ylim,...)
# grid
if(minorgrid){
abline(v=xmgrid,col="lightgray")
abline(h=ymgrid,col="lightgray")
}
if(majorgrid){
abline(v=xMgrid,col="gray")
abline(h=yMgrid,col="gray")
}
par(new=T)
# plot
plot(X,Y,log="xy",axes=F,frame.plot=T,ylim=ylim,xlim=xlim,...)
#labels
#par(las=1) #horizontal
for(px in pretty(Vpx)){
axis(1,at=10^px,bquote(10^.(px)))
}
for(py in pretty(Vpy)){
axis(2,at=10^py,bquote(10^.(py)))
}

}

#plot.loglog(f,S,type="o",col=4,pch="",xaxs="i",yaxs="i");

--
View this message in context: http://n4.nabble.com/nice-log-log-plots-tp793534p1592527.html
Sent from the R help mailing list archive at Nabble.com.

```