# [R] how to plot a clustered matrix in graphs

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Nov 22 21:07:00 CET 2005

```On 11/22/05, peter eric <eric_wzl at yahoo.com> wrote:
> dear deepayan & all...
>   let me explain you my full problem clearly..so that you could clearly
> suggest me in right direction....
>   my matrix (a) looks like as below...I´ve done clusutering in  this matrix
>   ----------------------------------------------------------------
>    a<-matrix(c(seq(3,45,3),seq(10,6,-1)),4,5,byrow=F)
>  col<-c("ra","rb","rc","rd","re")
>  rows<-c("ca","cb","cc","cd")
>  dimnames(a)<-list(rows,col)
> a
>      ra rb rc rd re
> ca  3 15 27 39  9
> cb  6 18 30 42  8
> cc  9 21 33 45  7
> cd 12 24 36 10  6

Your notation below (what you call rows and columns) is inconsistent
with what you have above, so I'm going to pretend that

a <- t(a)

>   -----------------------------------------------------------
>   clustering based on row and columns..suppose after clustering my row & col
> clustes looks like this..
>
>   row cluster 1: ra,rb
>                    2: rc,rd
>                    3:re
>   column cluster 1: ca,cb
>                          2: cc,cd

Let's code this as

rowc <- list(c("ra", "rb"), c("rc", "rd"), "re")
colc <- list(c("ca", "cb"), c("cc", "cd"))

>   So my clustered matrix (should) looks like
>
>                                                         ROWS
>                                         1                   2
>   3
>                                  I  ra    rb       I      rc   rd       I
>    re
>
> ----------------I------------------I----------------------I------------------
>                         ca     I   3    15      I      27  39       I
> 9
>                 1             I                  I                      I
>                         cb     I   6    18      I      30  42       I
> 8
>                                  I                  I
> I
>
> COLUMNS---------------I-----------------------------------------I-----------------
>                       cc     I   9     21     I      33  45       I      7
>               2               I                  I                      I
>                         cd     I 12    24     I      36  10       I      6
>
> ----------------I-----------------I-----------------------I--------------------
>   The above is the output matrix..It has to be plotted in graphs showing the
> partitions clearly as it is shown in the above matrix...
>   .
>   what kind of approach will be useful in gettign this..?particularly in
> colored graphs...

Here's what I would do:

levelplot(a[unlist(rowc), unlist(colc)],
colc = colc,
rowc  = rowc,
aspect = "iso",
panel = function(..., rowc, colc) {
panel.levelplot(...)
h = 0.5 + cumsum(sapply(colc, length))
v = 0.5 + cumsum(sapply(rowc, length))
panel.abline(h = h[-length(h)], v = v[-length(v)])
})

-Deepayan

```