learner
learner

Reputation: 2752

clustering the count values

I have a table which contains count for each variables inside each group.

Age var1 var2 var3 var4
10  0    200  0    100 
20  180  500  200  300
30  0    80   0    60
40  150  10   90   20

I am trying to create a heatmap using R. In the result of the heatmap, the columns are in the same order as they are present in the data.

I need to arrange/cluster these columns in such a way that variables with similar values are clustered together (to give a clustered look).

In the above example, looking at the counts (values), var1 is more similar to var3 whereas var2 is more similar to var4.

Here is the code to generate the same:

tmp = rbind(c(0,200,0,100),
rbind(c(180,  500,  200,  300),
rbind(c (0,    80,   0,    60), 
c(150,  10,   90,   20)
)))
rownames(tmp) = c('age10', 'age20', 'age30', 'age40')
colnames(tmp) = c('var1', 'var2', 'var3', 'var4')
tmp
heatmap(tmp, Rowv=NA, Colv=NA, 
        col = heat.colors(256), scale="column", margins=c(5,10))

Upvotes: 3

Views: 847

Answers (1)

David Robinson
David Robinson

Reputation: 78640

Simply remove the Rowv=NA and Colv=NA arguments, and the heatmap will be clustered by value:

heatmap(tmp, col = heat.colors(256), scale="column", margins=c(5,10))

Upvotes: 2

Related Questions