[R] 2 images on one plot

Jim Lemon jim at bitwrit.com.au
Tue Mar 13 11:09:42 CET 2012


On 03/13/2012 03:07 AM, Petr PIKAL wrote:
> Dear all
>
> with image I can plot only one set of values in one plot.
>
> Do somebody have any insight how to put those 2 matrices into one picture
> so that in one cell in image picture are both values from mat[1,1] and
> mat2[1,1].
>
> mat<-matrix(1:4, 2,2)
> mat2<-matrix(4:1,2,2)
> x<-1:2
> y<-1:2
> image(x, y, mat)
> image(x, y, mat2)
>
> The only way I found is to mix x or y for both matrices  let say
>
> xm<- sort(c(x,x+.5))
> matm<- cbind(mat[,1], mat2[,1], mat[,2], mat2[,2])
> image(xm,y,t(matm))
>
> which lacks of elegance and is rather complicated when considering matrix
> with more rows and columns.
>
Hi Petr,
I don't know whether this will be of any help, but it gets any two 
matrices in one plot:

interdigitate<-function(x1,x2,columns=TRUE) {
  dimx<-dim(x1)
  if(columns) {
   newx<-cbind(x1[,1],x2[,1])
   for(column in 2:dimx[2]) newx<-cbind(newx,x1[,column],x2[,column])
  }
  else {
   newx<-rbind(x1[1,],x2[1,])
   for(row in 2:dimx[1]) newx<-rbind(newx,x1[row,],x2[row,])
  }
  return(newx)
}
mat1<-matrix(1:9,nrow=3)
mat2<-matrix(11:19,nrow=3)
library(plotrix)
color2D.matplot(interdigitate(mat1,mat2))
color2D.matplot(interdigitate(mat1,mat2),columns=FALSE)

Obviously a bit of work is required to improve the elegance.

Jim



More information about the R-help mailing list