[R-SIG-Mac]plot hidden grid lines

Peter Paul Sint sint@oeaw.ac.at
Fri, 13 Jul 2001 14:30:05 +0200


Dear Stefano,

It is hard to plot hidden grid lines, e.g. in barplot, covered by the bars.

I do it by doing the barplot, drawing the grid, and painting the bars again (see appendix). Not very satisfactory (and concerning the slow graphics further reducing speed). It comes near to rewriting barplot. If you advise me on the original code to determine the axes (and how to get the appropriate panel variables = what par() 's are used? Are the par()s /din,fin,pin,mai.../ enough or do I need more?) I would do this for my puposes.

But there is a better solution:
What is missing is an implementation of panel.first
See example in plot.default {base}   :
> data(cars)
> Speed <- cars$speed
> Distance <- cars$dist
> plot(Speed, Distance, panel.first = grid(8,8),
+      pch = 0, cex = 1.2, col = "blue")
> 
plot(Speed, Distance,
     panel.first = lines(lowess(Speed, Distance), lty = "dashed"),
     pch = 0, cex = 1.2, col = "blue")
>

Another way may be to use par(new=T) for continuing to paint on an empty plot?
(I do not know if axis are set up un a comparable way).

May be somebody has a better solution?

Peter

My present solution

bargrid<- function(jjin,beside1=F,horiz1=F,Xlim=NA,Ylim=NA,...) {
    if(is.na(Ylim)) { if(is.na(Xlim))
	jjbp<- barplot(jjin,horiz=horiz1,beside=beside1,axes=F,...) else
        jjbp<- barplot(jjin,horiz=horiz1,beside=beside1,xlim=Xlim,axes=F,...) } else {           
        if(is.na(Xlim))    
    {jjbp<- barplot(jjin,horiz=horiz1,beside=beside1,ylim=Ylim,axes=F,...)} else 
     jjbp<- barplot(jjin,horiz=horiz1,beside=beside1,xlim=Xlim,ylim=Ylim,axes=F,...) } 
    #if(is.vector(jjin))  jjm<- matrix(jjin,1,length(jjin));
    if(is.vector(jjin)) { jjm<- matrix(jjin,length(jjin),1); 
    #jju<- rep(0,length(jjin) 
    jju<- jjm
    col1<- heat.colors(length(jjm)); 
    } else 
    { jjm<- jjin;
    # col1<- rep(heat.colors(jjd1),rep(jjd2,jjd1))
    if(!is.matrix(jjm))  { cat ("\n Input must be matrix or vector\n")  };
    jjd1<- dim(jjm)[1]
    jjnbarg<- jjd1
    jjd2<- dim(jjm)[2] 
    col1<- rep(heat.colors(jjd1),rep(jjd2,jjd1))
    jju<- apply(jjm,2,cumsum)
    }
pretty1<- pretty(as.vector(jju))
cat(pretty1,"\n",jju,"\n")
if(beside1) { pretty1<- pretty(as.vector(jjm));col1<- heat.colors(jjd1)  }
jjrectl<- jju-jjm
jjrectu<- jju
if(beside1) {jjrectl<- 0;jjrectu<- t(jjm); }
if(horiz1)  
    {if(!is.na(Xlim)) { pretty2<- pretty(c(0,Xlim[2]));
	    #cat("Xlim=",Xlim," pretty2=",pretty2,"\n"); 
	    for(i in 1:length(pretty1)) 
	    abline(v=pretty2[i],lty="dashed"); axis(1,pretty2) } else 
	    #lines(c(pretty2[i],pretty2[i]),c(0,jjbp[3,4]+1),lty="dashed") } else 
    axis(1,pretty1,tck=1,lty="dashed")      
    rect( t(jjrectl),jjbp-0.5,t(jjrectu),jjbp+0.5,col=col1)    
    } else {
    if(!is.na(Ylim)) {  pretty2<- pretty(c(0,Xlim[2])); for(i in 1:length(pretty1)) 
	abline(v=pretty2[i],lty="dashed"); axis(2,pretty2) } else 
	#lines(c(0,jjbp[3,4]),c(pretty1[i],pretty1[i]),lty="dashed")  } else
    axis(2,pretty1,tck=1,lty="dashed")
    rect( jjbp-0.5,t(jjrectl),jjbp+0.5,t(jjrectu),col=col1) 
   }
invisible(jjbp)
}
jjin<- 1:5
jjin<- matrix(1:12,3,4)
bargrid(jjin,beside=T,horiz=T,Xlim=c(-10,13))


-- 
Peter Sint
sint@oeaw.ac.at