On Fri, May 13, 2011 at 1:31 AM, Dat Mai <dat.d.mai at gmail.com>
wrote:> Yes, but I want to make it so that 2 different square matrices to both have
> the same order for the x and y axis while, at the same time, the x and y
> axis having variables in the same order as well:
>
> ????????? a?????? b?????? c???? d?????? e???? ...
> a??????? Outputs here------------>
> b??????????????? |
> c??????????????? |
> d??????????????? |
> e?????????????? V
> .
> .
> .
>
> Both matrices 1 and 2 should look like this. That way, comparing them via a
> heatmap would be much easier.
>
> That command would only serve or order each matrix individually by the
> values in column 1. The problem with this is that both matrices in question
> do not have the same or similar values for each output. Therefore, both
> matrices would be ordered by some column I choose, but will not be in the
> same order.
Sorry - I didn't know that you were trying to order by the dimnames of
the matrix. In that case, you should be able to do something like the
following:
> #row and column names
> dnames<-c('a','b','c','d','e')
> #matrix with rows and columns in correct order
> mat1<-matrix(rnorm(25),ncol=5,dimnames=list(dnames,dnames))
>
> #random order for row and column names
> rnames1<-sample(dnames,5)
> rnames2<-sample(dnames,5)
> #matrix with rows and columns in incorrect order
> mat2<-matrix(rnorm(25),ncol=5,dimnames=list(rnames1,rnames2))
> mat2
d c b a e
c -0.04923684 0.4765760 0.43840872 -0.5524667 -0.2905828
d -0.32432997 0.4206066 0.47332957 -0.4519173 -0.4126728
e 0.61084905 -0.6585749 0.06420868 1.7143967 -1.4071063
a 1.58412898 -1.4497131 -1.03201925 1.5232362 1.9599125
b 0.85896345 -1.4203311 -0.12717927 0.2900827
-1.0357730>
> #mat2 with rows and columns in correct order
> mat3<-mat2[order(dimnames(mat2)[[1]]),order(dimnames(mat2)[[2]])]
> mat3
a b c d e
a 1.5232362 -1.03201925 -1.4497131 1.58412898 1.9599125
b 0.2900827 -0.12717927 -1.4203311 0.85896345 -1.0357730
c -0.5524667 0.43840872 0.4765760 -0.04923684 -0.2905828
d -0.4519173 0.47332957 0.4206066 -0.32432997 -0.4126728
e 1.7143967 0.06420868 -0.6585749 0.61084905
-1.4071063>