I found this previously published Q&A: R: How do I display clustered matrix heatmap (similar color patterns are grouped)
However, when trying to do the same with my data I get: ggplot Heatmap My code is:
Data <- X
Data.features <-Data
Data.features$condition <-NULL
results <- kmeans(Data.features, 12)
m2 <- cbind(Data.features, id=seq(nrow(Data.features)), Data$condition, results$cluster)
m2$idsort <- m2$`Data$condition`[order(m2$`results$cluster`)]
m2$idsort <- order(m2$idsort)
library(reshape2)
m3 <- melt(m2, id.vars = c("id", "idsort"))
library(ggplot2)
ggplot(m3, aes(x=variable, y=idsort)) + geom_tile(aes(fill=value))
Here is the beginning of the m3:
id idsort variable value
1 1 BV RM 1.488831956
2 2 BV RM -0.060080659
3 3 BV RM -0.965135718
4 4 BV RM 0.109121125
5 5 BV RM 0.529575031
6 6 BV RM 0.345320036
7 7 BV RM -0.130297731
8 8 Nun RM -0.352261118
9 9 BV RM -1.076270641
10 10 BV RM -0.35698175
11 11 BV RM 0.986731511
12 12 BV RM 0.442295651
13 13 un RM 0.74754209
14 14 un RM -0.535263676
15 15 un RM -0.61017644
16 16 cF RM -1.476832822
17 17 Bw RM 0.031005185
18 18 Bw RM -0.368904758
19 19 cF RM -1.788968277
20 20 cF RM -1.828974127
21 21 cF RM -2.22952264
22 22 cF RM 1.444215761
23 23 Bw RM 0.624473691
24 24 un RM 0.593635853
25 25 un RM 0.388536217
26 26 Nun RM 0.348805061
27 27 NcF RM -0.899908037
28 28 NcF RM 0.828383186
29 29 NcF RM 0.499278949
30 30 un RM 2.316891992
31 31 Nun RM -1.216437876
32 32 BV RM 0.18346742
33 33 un RM -0.057088997
34 34 BV RM -1.486598664
35 35 NBV RM -0.31906404
36 36 BV RM 0.379173977
37 37 BV RM -1.067540594
38 38 BV RM 0.876030348
39 39 BV RM 1.263694633
40 40 cF RM -0.716306437
I can't find my mistake. Would appreciate any suggestion. Thank you.