Reorder dendrogram in R

395 views Asked by At

I want to plot a heatmap where the x-axis is clustered by "normal" and "KIRP" from left to right. Currently, my code clusters by dendrogram/similarity, and unfortunately I have one outlier "KIRP" on the left-most. I want to move this "KIRP" sample so that it appears after all the "normal" samples. Nevertheless, both groups "normal" and "KIRP" should still be clustered and arranged based on group similarity.

[![enter image description here][1]][1]

Code:

dge <- DGEList(counts=mat, group=group)
keep <- filterByExpr(dge)
v <- voom(mat, design, plot=TRUE)
vfit <- lmFit(v, design)
vfit <- contrasts.fit(vfit, contrasts=contrasts)
efit <- eBayes(vfit)
tfit <- treat(vfit, lfc=1)
dt <- decideTests(tfit)
de.common <- which(dt[,1]!=0)


kirp.vs.normal <- topTreat(tfit, coef=1, n=Inf)

topgenes <- rownames(kirp.vs.normal)[1:150]
i <- which(rownames(dge) %in% topgenes)
mycol <- colorpanel(1000,"#FFA500","white","#2E2787")
heatmap.2(lcpm[i,], scale="row",
          labRow=rownames(dge)[i], labCol=group, 
          col=mycol, trace="none", density.info="none", 
          margin=c(8,1), lhei=c(2,10), dendrogram="column", main="Differential Gene Expression in\nNormal vs KIRP Type II CIMP samples", cex.main=0.3, lmat=rbind(c(0,3,4), c(2,1,0)), lwid=c(0.5,10,3))

Data:

dput(dge[1:50,1:50])

new("DGEList", .Data = list(structure(c(2L, 47L, 5L, 185L, 124L, 
272L, 197L, 405L, 59L, 258L, 270L, 226L, 112L, 322L, 381L, 281L, 
145L, 53L, 325L, 107L, 103L, 375L, 70L, 298L, 131L, 79L, 297L, 
2L, 345L, 390L, 113L, 289L, 58L, 400L, 389L, 414L, 228L, 188L, 
392L, 222L, 86L, 355L, 20L, 49L, 211L, 311L, 96L, 304L, 378L, 
145L, 3L, 363L, 199L, 22L, 313L, 305L, 182L, 338L, 32L, 266L, 
314L, 35L, 384L, 361L, 37L, 241L, 4L, 340L, 356L, 26L, 100L, 
212L, 27L, 273L, 25L, 43L, 355L, 5L, 211L, 155L, 372L, 253L, 
180L, 380L, 105L, 13L, 242L, 221L, 401L, 215L, 197L, 233L, 345L, 
136L, 254L, 183L, 111L, 390L, 392L, 298L, 1L, 308L, 89L, 118L, 
306L, 219L, 50L, 100L, 352L, 286L, 229L, 340L, 135L, 194L, 130L, 
124L, 323L, 54L, 105L, 279L, 91L, 99L, 391L, 291L, 395L, 83L, 
353L, 1L, 322L, 185L, 196L, 263L, 33L, 274L, 362L, 265L, 234L, 
356L, 297L, 154L, 81L, 65L, 293L, 144L, 2L, 132L, 270L, 360L, 
371L, 5L, 2L, 95L, 1L, 93L, 248L, 317L, 269L, 373L, 71L, 192L, 
375L, 340L, 60L, 108L, 42L, 128L, 3L, 292L, 312L, 173L, 363L, 
178L, 17L, 387L, 143L, 329L, 385L, 2L, 252L, 118L, 413L, 16L, 
87L, 339L, 88L, 75L, 347L, 184L, 337L, 297L, 136L, 229L, 85L, 
358L, 8L, 283L, 162L, 316L, 45L, 7L, 1L, 319L, 2L, 117L, 137L, 
199L, 300L, 114L, 291L, 92L, 125L, 168L, 153L, 238L, 3L, 259L, 
192L, 360L, 125L, 230L, 80L, 262L, 34L, 266L, 220L, 237L, 272L, 
1L, 326L, 38L, 350L, 273L, 352L, 320L, 45L, 218L, 209L, 224L, 
288L, 145L, 372L, 192L, 307L, 203L, 2L, 277L, 280L, 233L, 368L, 
6L, 2L, 83L, 2L, 192L, 141L, 297L, 203L, 338L, 323L, 210L, 289L, 
275L, 91L, 263L, 3L, 4L, 2L, 28L, 259L, 264L, 317L, 198L, 361L, 
365L, 373L, 312L, 300L, 2L, 283L, 63L, 123L, 324L, 286L, 251L, 
253L, 104L, 284L, 143L, 371L, 237L, 325L, 314L, 16L, 208L, 1L, 
191L, 134L, 279L, 348L, 180L, 2L, 126L, 1L, 369L, 368L, 377L, 
305L, 314L, 38L, 24L, 407L, 223L, 320L, 66L, 3L, 136L, 2L, 240L, 
404L, 227L, 336L, 356L, 403L, 49L, 195L, 260L, 365L, 2L, 405L, 
350L, 302L, 351L, 11L, 358L, 225L, 37L, 340L, 132L, 380L, 276L, 
146L, 80L, 200L, 328L, 2L, 317L, 184L, 269L, 304L, 280L, 3L, 
147L, 4L, 183L, 279L, 198L, 69L, 90L, 337L, 192L, 9L, 173L, 201L, 
265L, 2L, 237L, 291L, 392L, 96L, 287L, 30L, 78L, 383L, 317L, 
325L, 333L, 275L, 1L, 354L, 12L, 37L, 245L, 378L, 316L, 51L, 
284L, 223L, 330L, 308L, 113L, 44L, 321L, 298L, 92L, 4L, 18L, 
241L, 269L, 336L, 22L, 1L, 272L, 4L, 114L, 134L, 224L, 315L, 
72L, 361L, 200L, 135L, 269L, 98L, 260L, 4L, 42L, 4L, 371L, 148L, 
168L, 110L, 323L, 48L, 271L, 33L, 49L, 345L, 2L, 285L, 95L, 79L, 
277L, 38L, 327L, 352L, 124L, 230L, 189L, 283L, 160L, 54L, 220L, 
357L, 211L, 2L, 287L, 273L, 275L, 339L, 2L, 2L, 302L, 203L, 210L, 
190L, 276L, 351L, 51L, 361L, 155L, 232L, 213L, 184L, 330L, 130L, 
56L, 342L, 79L, 209L, 178L, 163L, 86L, 375L, 337L, 96L, 286L, 
335L, 5L, 382L, 398L, 116L, 322L, 16L, 268L, 40L, 261L, 229L, 
263L, 359L, 181L, 117L, 71L, 400L, 113L, 1L, 390L, 23L, 329L, 
284L, 5L, 1L, 330L, 3L, 40L, 102L, 200L, 269L, 67L, 284L, 149L, 
186L, 145L, 93L, 296L, 4L, 321L, 1L, 35L, 53L, 148L, 57L, 283L, 
366L, 280L, 85L, 43L, 357L, 1L, 304L, 9L, 41L, 259L, 326L, 310L, 
106L, 153L, 229L, 214L, 243L, 172L, 30L, 289L, 331L, 174L, 111L, 
359L, 273L, 294L, 365L, 4L, 3L, 379L, 10L, 171L, 216L, 301L, 
151L, 70L, 40L, 34L, 394L, 245L, 390L, 142L, 3L, 146L, 10L, 341L, 
154L, 35L, 263L, 65L, 387L, 356L, 23L, 290L, 24L, 1L, 227L, 91L, 
323L, 389L, 376L, 275L, 55L, 369L, 328L, 257L, 256L, 304L, 102L, 
57L, 62L, 336L, 7L, 217L, 187L, 310L, 401L, 10L, 2L, 284L, 2L, 
141L, 222L, 278L, 376L, 67L, 342L, 276L, 142L, 179L, 74L, 310L, 
253L, 228L, 8L, 14L, 193L, 190L, 89L, 62L, 40L, 330L, 184L, 283L, 
324L, 2L, 244L, 390L, 183L, 277L, 402L, 357L, 388L, 156L, 256L, 
255L, 343L, 114L, 79L, 38L, 361L, 167L, 2L, 301L, 375L, 262L, 
356L, 205L, 1L, 111L, 1L, 58L, 109L, 315L, 210L, 23L, 18L, 218L, 
36L, 268L, 285L, 301L, 7L, 186L, 8L, 258L, 142L, 130L, 291L, 
335L, 71L, 19L, 16L, 385L, 69L, 2L, 276L, 375L, 128L, 42L, 369L, 
333L, 91L, 318L, 371L, 225L, 270L, 226L, 31L, 329L, 106L, 224L, 
2L, 172L, 88L, 292L, 35L, 143L, 2L, 23L, 2L, 74L, 207L, 257L, 
357L, 27L, 341L, 124L, 202L, 72L, 86L, 237L, 7L, 287L, 3L, 44L, 
224L, 221L, 116L, 35L, 30L, 305L, 71L, 337L, 350L, 1L, 365L, 
353L, 62L, 292L, 17L, 288L, 21L, 143L, 228L, 253L, 271L, 178L, 
39L, 382L, 363L, 238L, 6L, 137L, 374L, 331L, 346L, 100L, 1L, 
26L, 114L, 270L, 218L, 370L, 151L, 361L, 48L, 121L, 345L, 68L, 
280L, 308L, 326L, 217L, 2L, 234L, 93L, 40L, 73L, 102L, 85L, 265L, 
335L, 301L, 375L, 1L, 163L, 201L, 123L, 260L, 109L, 357L, 208L, 
319L, 286L, 108L, 252L, 284L, 184L, 181L, 235L, 240L, 2L, 56L, 
194L, 248L, 20L, 232L, 1L, 379L, 4L, 39L, 188L, 291L, 352L, 17L, 
363L, 57L, 177L, 215L, 127L, 300L, 3L, 112L, 5L, 23L, 77L, 199L, 
32L, 385L, 47L, 311L, 139L, 277L, 346L, 1L, 305L, 60L, 162L, 
284L, 13L, 332L, 36L, 159L, 224L, 230L, 304L, 228L, 53L, 376L, 
371L, 190L, 2L, 366L, 380L, 258L, 386L, 132L, 2L, 34L, 5L, 120L, 
375L, 338L, 126L, 41L, 24L, 173L, 33L, 387L, 54L, 146L, 198L, 
292L, 6L, 15L, 226L, 267L, 333L, 153L, 335L, 57L, 380L, 148L, 
32L, 3L, 349L, 189L, 298L, 49L, 403L, 350L, 88L, 94L, 343L, 260L, 
322L, 311L, 92L, 80L, 371L, 388L, 1L, 274L, 73L, 47L, 280L, 229L, 
1L, 316L, 1L, 118L, 261L, 254L, 347L, 58L, 388L, 209L, 264L, 
298L, 56L, 288L, 365L, 227L, 365L, 321L, 179L, 92L, 86L, 135L, 
35L, 279L, 133L, 190L, 353L, 2L, 314L, 93L, 223L, 329L, 45L, 
379L, 16L, 211L, 216L, 262L, 306L, 202L, 108L, 90L, 319L, 204L, 
4L, 273L, 381L, 332L, 15L, 118L, 3L, 211L, 138L, 48L, 255L, 302L, 
283L, 22L, 368L, 29L, 228L, 363L, 405L, 309L, 58L, 350L, 286L, 
301L, 78L, 159L, 65L, 149L, 399L, 306L, 74L, 198L, 336L, 1L, 
327L, 91L, 312L, 259L, 108L, 345L, 168L, 70L, 251L, 221L, 314L, 
253L, 169L, 97L, 231L, 177L, 4L, 325L, 291L, 293L, 386L, 138L, 
2L, 181L, 1L, 10L, 214L, 292L, 311L, 28L, 382L, 163L, 262L, 347L, 
77L, 242L, 404L, 340L, 9L, 416L, 232L, 168L, 157L, 95L, 14L, 
297L, 303L, 113L, 354L, 129L, 313L, 70L, 190L, 296L, 67L, 355L, 
17L, 103L, 266L, 257L, 377L, 226L, 98L, 132L, 345L, 241L, 7L, 
108L, 54L, 320L, 395L, 205L, 196L, 265L, 3L, 140L, 257L, 190L, 
74L, 95L, 315L, 177L, 151L, 130L, 169L, 235L, 5L, 188L, 311L, 
9L, 72L, 277L, 23L, 16L, 354L, 302L, 378L, 353L, 229L, 2L, 338L, 
19L, 156L, 262L, 352L, 288L, 274L, 228L, 200L, 351L, 310L, 127L, 
28L, 355L, 237L, 78L, 2L, 385L, 273L, 304L, 330L, 1L, 2L, 377L, 
138L, 141L, 22L, 286L, 318L, 399L, 38L, 230L, 97L, 295L, 57L, 
312L, 2L, 263L, 4L, 385L, 240L, 149L, 58L, 24L, 45L, 300L, 104L, 
263L, 367L, 1L, 319L, 100L, 169L, 298L, 36L, 383L, 130L, 91L, 
241L, 159L, 320L, 233L, 120L, 347L, 351L, 194L, 4L, 372L, 355L, 
329L, 394L, 8L, 3L, 110L, 1L, 311L, 143L, 373L, 178L, 181L, 379L, 
393L, 307L, 274L, 121L, 86L, 277L, 3L, 202L, 365L, 124L, 378L, 
374L, 107L, 348L, 57L, 370L, 304L, 18L, 139L, 255L, 325L, 286L, 
13L, 391L, 293L, 46L, 271L, 345L, 116L, 372L, 242L, 213L, 248L, 
149L, 295L, 4L, 106L, 263L, 303L, 279L, 119L, 2L, 262L, 2L, 242L, 
373L, 304L, 246L, 216L, 18L, 265L, 255L, 199L, 30L, 291L, 6L, 
357L, 133L, 359L, 168L, 266L, 80L, 114L, 352L, 15L, 157L, 389L, 
61L, 40L, 60L, 273L, 91L, 130L, 156L, 329L, 387L, 292L, 397L, 
403L, 323L, 301L, 215L, 332L, 177L, 375L, 9L, 161L, 181L, 330L, 
335L, 254L, 2L, 178L, 120L, 70L, 49L, 352L, 287L, 351L, 368L, 
214L, 389L, 221L, 44L, 60L, 2L, 121L, 272L, 363L, 36L, 388L, 
284L, 63L, 38L, 57L, 47L, 263L, 342L, 237L, 312L, 300L, 228L, 
22L, 373L, 201L, 116L, 77L, 347L, 168L, 318L, 257L, 107L, 27L, 
61L, 326L, 5L, 390L, 229L, 349L, 279L, 120L, 1L, 205L, 6L, 108L, 
141L, 129L, 47L, 83L, 327L, 23L, 54L, 184L, 181L, 249L, 188L, 
143L, 188L, 102L, 107L, 228L, 35L, 82L, 343L, 290L, 12L, 311L, 
232L, 1L, 339L, 53L, 159L, 212L, 346L, 239L, 316L, 305L, 158L, 
313L, 280L, 139L, 40L, 68L, 320L, 84L, 4L, 143L, 276L, 275L, 
310L, 2L, 1L, 147L, 1L, 339L, 67L, 301L, 117L, 329L, 347L, 372L, 
276L, 258L, 24L, 310L, 1L, 5L, 1L, 335L, 368L, 47L, 74L, 209L, 
262L, 83L, 371L, 28L, 341L, 1L, 305L, 31L, 9L, 357L, 325L, 259L, 
30L, 313L, 344L, 216L, 311L, 210L, 100L, 48L, 234L, 330L, 6L, 
19L, 285L, 350L, 275L, 180L, 8L, 128L, 2L, 120L, 162L, 266L, 
309L, 35L, 375L, 83L, 192L, 258L, 147L, 202L, 142L, 41L, 2L, 
313L, 9L, 197L, 126L, 75L, 368L, 323L, 14L, 199L, 63L, 5L, 380L, 
91L, 114L, 311L, 29L, 296L, 301L, 203L, 276L, 246L, 335L, 182L, 
45L, 11L, 387L, 215L, 1L, 15L, 383L, 364L, 332L, 7L, 2L, 132L, 
2L, 116L, 130L, 218L, 321L, 47L, 351L, 264L, 192L, 171L, 126L, 
227L, 2L, 363L, 229L, 379L, 210L, 188L, 154L, 285L, 11L, 314L, 
338L, 177L, 323L, 2L, 362L, 382L, 49L, 237L, 375L, 310L, 244L, 
185L, 254L, 241L, 346L, 111L, 27L, 230L, 383L, 175L, 2L, 232L, 
386L, 284L, 287L, 1L, 1L, 347L, 9L, 95L, 285L, 339L, 245L, 397L, 
91L, 229L, 298L, 26L, 396L, 385L, 401L, 24L, 8L, 347L, 264L, 
122L, 146L, 128L, 28L, 306L, 93L, 390L, 415L, 2L, 300L, 207L, 
262L, 287L, 127L, 51L, 230L, 43L, 297L, 194L, 348L, 209L, 213L, 
193L, 365L, 165L, 3L, 69L, 17L, 342L, 382L, 131L, 2L, 328L, 128L, 
109L, 207L, 199L, 305L, 43L, 357L, 269L, 207L, 209L, 162L, 251L, 
4L, 195L, 190L, 104L, 275L, 153L, 117L, 302L, 51L, 307L, 69L, 
358L, 341L, 4L, 283L, 359L, 46L, 238L, 12L, 293L, 312L, 212L, 
215L, 271L, 348L, 136L, 15L, 353L, 52L, 174L, 190L, 18L, 123L, 
321L, 335L, 1L, 2L, 301L, 5L, 365L, 226L, 285L, 206L, 28L, 395L, 
297L, 245L, 13L, 75L, 281L, 32L, 324L, 7L, 393L, 94L, 181L, 125L, 
153L, 368L, 268L, 10L, 191L, 369L, 1L, 326L, 180L, 188L, 314L, 
58L, 375L, 53L, 122L, 236L, 235L, 265L, 207L, 156L, 165L, 313L, 
171L, 5L, 385L, 256L, 276L, 29L, 129L, 1L, 216L, 128L, 72L, 117L, 
233L, 287L, 378L, 353L, 204L, 80L, 257L, 65L, 315L, 190L, 201L, 
227L, 12L, 198L, 146L, 103L, 15L, 48L, 280L, 155L, 308L, 359L, 
1L, 253L, 144L, 19L, 288L, 49L, 329L, 119L, 113L, 200L, 151L, 
284L, 196L, 102L, 295L, 358L, 240L, 128L, 241L, 281L, 264L, 349L, 
3L, 2L, 20L, 3L, 131L, 144L, 180L, 356L, 57L, 331L, 232L, 175L, 
199L, 114L, 215L, 191L, 74L, 5L, 88L, 144L, 202L, 135L, 355L, 
30L, 283L, 291L, 98L, 350L, 1L, 371L, 26L, 251L, 288L, 398L, 
270L, 353L, 93L, 237L, 225L, 333L, 174L, 45L, 8L, 391L, 197L, 
6L, 65L, 330L, 346L, 367L, 122L, 1L, 244L, 2L, 95L, 191L, 166L, 
360L, 63L, 305L, 118L, 182L, 172L, 110L, 236L, 6L, 17L, 6L, 365L, 
237L, 173L, 52L, 322L, 23L, 290L, 373L, 354L, 307L, 1L, 340L, 
47L, 97L, 273L, 329L, 284L, 316L, 203L, 219L, 253L, 315L, 142L, 
377L, 325L, 321L, 274L, 2L, 40L, 336L, 321L, 351L, 1L, 4L, 236L, 
5L, 278L, 195L, 243L, 348L, 40L, 9L, 230L, 215L, 364L, 39L, 196L, 
58L, 332L, 114L, 297L, 130L, 165L, 65L, 151L, 75L, 288L, 322L, 
200L, 380L, 4L, 227L, 198L, 217L, 233L, 111L, 276L, 293L, 167L, 
211L, 303L, 287L, 160L, 128L, 177L, 308L, 147L, 1L, 136L, 376L, 
274L, 335L, 176L, 1L, 397L, 1L, 46L, 137L, 315L, 271L, 373L, 
393L, 323L, 196L, 232L, 351L, 130L, 207L, 174L, 1L, 197L, 229L, 
123L, 228L, 368L, 36L, 383L, 140L, 322L, 342L, 2L, 200L, 345L, 
336L, 294L, 249L, 227L, 276L, 88L, 307L, 119L, 390L, 234L, 70L, 
46L, 64L, 241L, 205L, 132L, 208L, 270L, 343L, 13L, 1L, 221L, 
1L, 168L, 38L, 266L, 333L, 272L, 358L, 335L, 79L, 241L, 110L, 
33L, 1L, 1L, 2L, 41L, 295L, 165L, 287L, 368L, 332L, 337L, 357L, 
30L, 284L, 117L, 324L, 356L, 162L, 309L, 270L, 306L, 49L, 7L, 
282L, 155L, 12L, 194L, 68L, 259L, 208L, 85L, 1L, 214L, 63L, 252L, 
305L, 117L, 1L, 398L, 131L, 399L, 221L, 323L, 142L, 373L, 397L, 
262L, 277L, 389L, 315L, 324L, 98L, 393L, 8L, 190L, 285L, 58L, 
56L, 194L, 87L, 340L, 192L, 239L, 382L, 1L, 246L, 400L, 254L, 
269L, 161L, 314L, 148L, 146L, 276L, 200L, 334L, 284L, 185L, 147L, 
341L, 140L, 203L, 280L, 16L, 330L, 392L, 325L, 6L, 271L, 6L, 
75L, 241L, 205L, 14L, 108L, 363L, 190L, 148L, 269L, 169L, 226L, 
6L, 34L, 137L, 398L, 234L, 276L, 104L, 381L, 355L, 341L, 12L, 
322L, 325L, 1L, 360L, 123L, 224L, 291L, 400L, 301L, 285L, 245L, 
232L, 359L, 344L, 145L, 64L, 369L, 272L, 194L, 4L, 26L, 326L, 
309L, 323L, 6L, 1L, 376L, 2L, 102L, 109L, 202L, 298L, 72L, 366L, 
230L, 213L, 221L, 84L, 242L, 2L, 339L, 8L, 42L, 247L, 204L, 111L, 
28L, 17L, 281L, 375L, 150L, 337L, 1L, 349L, 24L, 229L, 262L, 
378L, 277L, 358L, 170L, 254L, 261L, 285L, 205L, 48L, 10L, 333L, 
198L, 2L, 324L, 309L, 355L, 359L, 4L, 1L, 55L, 4L, 91L, 386L, 
289L, 45L, 13L, 347L, 263L, 370L, 303L, 384L, 127L, 2L, 131L, 
6L, 353L, 165L, 333L, 271L, 295L, 105L, 18L, 109L, 20L, 14L, 
1L, 305L, 30L, 380L, 397L, 12L, 293L, 385L, 213L, 357L, 201L, 
312L, 282L, 23L, 318L, 72L, 301L, 4L, 146L, 96L, 302L, 379L, 
2L, 2L, 379L, 2L, 188L, 174L, 223L, 304L, 388L, 392L, 312L, 278L, 
182L, 144L, 334L, 5L, 283L, 195L, 59L, 310L, 179L, 192L, 30L, 
316L, 335L, 68L, 387L, 332L, 3L, 374L, 402L, 143L, 305L, 20L, 
358L, 21L, 208L, 239L, 214L, 407L, 133L, 97L, 326L, 42L, 256L, 
123L, 63L, 45L, 395L, 276L, 124L, 1L, 282L, 204L, 21L, 248L, 
311L, 74L, 62L, 416L, 347L, 278L, 276L, 69L, 385L, 20L, 297L, 
6L, 324L, 176L, 188L, 127L, 108L, 331L, 307L, 214L, 93L, 390L, 
2L, 300L, 80L, 349L, 344L, 109L, 398L, 339L, 173L, 244L, 323L, 
272L, 259L, 185L, 172L, 366L, 226L, 129L, 35L, 303L, 404L, 391L, 
206L, 2L, 346L, 4L, 109L, 30L, 218L, 171L, 17L, 332L, 240L, 239L, 
384L, 102L, 73L, 2L, 290L, 245L, 350L, 221L, 39L, 214L, 233L, 
74L, 378L, 329L, 286L, 359L, 4L, 315L, 115L, 353L, 306L, 328L, 
226L, 305L, 255L, 297L, 167L, 356L, 228L, 392L, 244L, 398L, 210L, 
1L, 170L, 92L, 264L, 300L, 8L, 4L, 197L, 2L, 7L, 231L, 352L, 
118L, 320L, 24L, 176L, 252L, 392L, 13L, 133L, 3L, 209L, 258L, 
121L, 186L, 204L, 80L, 168L, 379L, 374L, 335L, 271L, 39L, 279L, 
44L, 148L, 262L, 95L, 99L, 388L, 37L, 309L, 365L, 136L, 333L, 
291L, 164L, 215L, 27L, 343L, 3L, 115L, 233L, 368L, 342L, 51L, 
4L, 329L, 2L, 14L, 37L, 301L, 125L, 31L, 315L, 389L, 34L, 218L, 
38L, 312L, 11L, 4L, 227L, 271L, 228L, 102L, 357L, 208L, 316L, 
47L, 20L, 369L, 45L, 4L, 390L, 302L, 94L, 19L, 362L, 251L, 394L, 
335L, 32L, 258L, 325L, 256L, 73L, 52L, 110L, 288L, 8L, 58L, 113L, 
353L, 320L, 136L, 2L, 230L, 2L, 42L, 123L, 327L, 222L, 354L, 
338L, 345L, 236L, 128L, 102L, 41L, 135L, 7L, 3L, 31L, 293L, 347L, 
328L, 296L, 16L, 47L, 385L, 86L, 294L, 2L, 19L, 246L, 164L, 371L, 
317L, 297L, 100L, 346L, 352L, 206L, 379L, 252L, 40L, 363L, 54L, 
186L, 1L, 339L, 139L, 274L, 391L, 194L, 1L, 287L, 1L, 128L, 53L, 
268L, 180L, 343L, 13L, 213L, 377L, 270L, 80L, 204L, 119L, 118L, 
118L, 10L, 103L, 375L, 202L, 21L, 63L, 316L, 393L, 342L, 344L, 
1L, 353L, 93L, 86L, 324L, 327L, 336L, 319L, 230L, 308L, 167L, 
355L, 219L, 43L, 32L, 389L, 199L, 3L, 96L, 386L, 290L, 388L, 
6L), dim = c(50L, 50L), dimnames = list(c("A2ML1", "ABCA4", "ABCB5", 
"ABHD1", "ACRBP", "ACSL5", "ACSM5", "ACSS3", "ACVRL1", "ADH1C", 
"ADRB2", "AEBP1", "AFMID", "AIF1", "AIM2", "AKR1B10", "AKR1C4", 
"AKR7L", "ALDH3B2", "ALDH8A1", "ALDOC", "ALOX5AP", "ALPK3", "AMFR", 
"ANKRD22", "ANKRD2", "ANKRD45", "ANXA8L2", "ANXA9", "AOC3", "APBB1IP", 
"APH1B", "APOBEC3C", "APOL3", "APOL4", "APOM", "APP", "AQP1", 
"ARFRP1", "ARHGAP29", "ARHGDIB", "ARL11", "ARL4D", "ARRDC3", 
"ASCL3", "B3GNT3", "B3GNT8", "BAMBI", "BAZ2B", "BCL2L14"), c("TCGA.BQ.7051.11A", 
"TCGA.DZ.6132.11A", "TCGA.CZ.4864.11A", "TCGA.KN.8426.11A", "TCGA.CZ.5982.11A", 
"TCGA.A4.A4ZT.11A", "TCGA.CZ.5468.11A", "TCGA.BQ.5894.11A", "TCGA.B0.5699.11A", 
"TCGA.KL.8339.11A", "TCGA.CZ.5988.11A", "TCGA.CZ.5461.11A", "TCGA.CJ.6030.11A", 
"TCGA.B8.5549.11A", "TCGA.CW.5587.11A", "TCGA.CZ.5987.11A", "TCGA.CJ.5677.11A", 
"TCGA.CZ.5470.11A", "TCGA.B2.5636.11A", "TCGA.CJ.5676.11A", "TCGA.KN.8435.11A", 
"TCGA.BQ.5877.11A", "TCGA.CZ.5984.11A", "TCGA.CZ.5457.11A", "TCGA.CZ.4863.11A", 
"TCGA.CZ.5467.11A", "TCGA.A3.3387.11A", "TCGA.CZ.5456.11A", "TCGA.B9.4115.11A", 
"TCGA.GL.6846.11A", "TCGA.B0.5402.11A", "TCGA.DZ.6133.11A", "TCGA.B0.5691.11A", 
"TCGA.B0.4700.11A", "TCGA.B0.5696.11A", "TCGA.CW.5581.11A", "TCGA.BQ.7045.11A", 
"TCGA.KN.8427.11A", "TCGA.GL.A59R.11A", "TCGA.CW.5584.11A", "TCGA.BQ.5878.11A", 
"TCGA.CW.5589.11A", "TCGA.CJ.5672.11A", "TCGA.BQ.7044.11A", "TCGA.CZ.5466.11A", 
"TCGA.BQ.5887.11A", "TCGA.CZ.4865.11A", "TCGA.CZ.5458.11A", "TCGA.Y8.A8RY.11A", 
"TCGA.KN.8422.11A"))), structure(list(group = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), levels = "normal", class = "factor"), lib.size = c(94225, 
93733, 87671, 94478, 81956, 81966, 91604, 87469, 81048, 91004, 
81264, 92424, 91496, 85877, 87734, 83846, 88254, 92553, 89254, 
91736, 96220, 84907, 90231, 87189, 90384, 87166, 81495, 81436, 
90285, 83664, 95495, 84763, 91291, 85265, 87117, 81741, 88278, 
92099, 81840, 90942, 88312, 89034, 89739, 90942, 95178, 88887, 
91263, 85695, 87717, 90705), norm.factors = c(1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1)), row.names = c("TCGA.BQ.7051.11A", "TCGA.DZ.6132.11A", 
"TCGA.CZ.4864.11A", "TCGA.KN.8426.11A", "TCGA.CZ.5982.11A", "TCGA.A4.A4ZT.11A", 
"TCGA.CZ.5468.11A", "TCGA.BQ.5894.11A", "TCGA.B0.5699.11A", "TCGA.KL.8339.11A", 
"TCGA.CZ.5988.11A", "TCGA.CZ.5461.11A", "TCGA.CJ.6030.11A", "TCGA.B8.5549.11A", 
"TCGA.CW.5587.11A", "TCGA.CZ.5987.11A", "TCGA.CJ.5677.11A", "TCGA.CZ.5470.11A", 
"TCGA.B2.5636.11A", "TCGA.CJ.5676.11A", "TCGA.KN.8435.11A", "TCGA.BQ.5877.11A", 
"TCGA.CZ.5984.11A", "TCGA.CZ.5457.11A", "TCGA.CZ.4863.11A", "TCGA.CZ.5467.11A", 
"TCGA.A3.3387.11A", "TCGA.CZ.5456.11A", "TCGA.B9.4115.11A", "TCGA.GL.6846.11A", 
"TCGA.B0.5402.11A", "TCGA.DZ.6133.11A", "TCGA.B0.5691.11A", "TCGA.B0.4700.11A", 
"TCGA.B0.5696.11A", "TCGA.CW.5581.11A", "TCGA.BQ.7045.11A", "TCGA.KN.8427.11A", 
"TCGA.GL.A59R.11A", "TCGA.CW.5584.11A", "TCGA.BQ.5878.11A", "TCGA.CW.5589.11A", 
"TCGA.CJ.5672.11A", "TCGA.BQ.7044.11A", "TCGA.CZ.5466.11A", "TCGA.BQ.5887.11A", 
"TCGA.CZ.4865.11A", "TCGA.CZ.5458.11A", "TCGA.Y8.A8RY.11A", "TCGA.KN.8422.11A"
), class = "data.frame")))
1

There are 1 answers

0
knitz3 On

I used some simulated data to try this, since it seems that the question can be generalized to other datasets. I also ran into errors with your subset of data.

set.seed(123)
data <- as.matrix(data.frame(
    a1 = rnorm(100, 0, 1),
    a2 = rnorm(100, 0, 1),
    a3 = rnorm(100, 0, 1),
    b1 = rnorm(100, 0, 1.2),
    b2 = rnorm(100, 0, 1.2),
    b3 = rnorm(100, 0, 1.2)
))

Here we see that b1 would look nicer next to b3 and b2.

library(gplots)
ht <- heatmap.2(
    x = data,
    ColSideColors = c(rep("#b66363", 3), rep("#8fa8c0", 3)),
    col = colorpanel(1000,"#FFA500","white","#2E2787"),
    trace = "none",
    key = FALSE,
    dendrogram = "column",
    main = "Example Data",
    labRow = FALSE
)

Example Data

Looking at the structure of the heatmap output, the dendrogram is stored as a "dendrogram" class object, which can be manipulated with with the reorder() generic.

The documentation doesn't reveal too much, but the second argument wts describes arbitrary weights to determine a reordered dendrogram. From what I can tell, large values generally get placed to the right. From trial-and-error, it appears that supplying weights in the original order of the columns worked out. This essentially flips branches without affecting the distance metrics.

colden <- ht$colDendrogram
colden_reordered <- reorder(colden, c(10, 1, 1, 100, 300, 200))
plot(colden, main = "original dendrogram")
plot(colden_reordered, main = "modified dendrogram")

original dendrogram modified dendrogram

When can then plot the heatmap with the new dendrogram using the Colv option.

ht2 <- heatmap.2(
    x = data,
    ColSideColors = c(rep("#b66363", 3), rep("#8fa8c0", 3)),
    col = colorpanel(1000,"#FFA500","white","#2E2787"),
    trace = "none",
    key = FALSE,
    dendrogram = "column",
    Colv = colden_reordered,
    main = "Reordered manually",
    labRow = FALSE
)

Reordered manually

The dendsort package may be a better go-to tool for sorting dendrograms. In short, the dendsort() moves clusters with smaller average distances to the left. Using this alone seems to solve the issue. With larger heatmaps, the benefit may be apparent when looking for patterns in the data. Seems preferable to manual reordering, when possible. Below I've modified the option Colv to use this function.

library(dendsort)

ht3 <- heatmap.2(
    x = data,
    ColSideColors = c(rep("#b66363", 3), rep("#8fa8c0", 3)),
    col = colorpanel(1000,"#FFA500","white","#2E2787"),
    trace = "none",
    density.info = "none",
    key = FALSE,
    dendrogram = "column",
    Colv = dendsort(colden),
    main = "Reordered w/ dendsort()",
    labRow = FALSE
)

Reordered with dendsort

For more options for heatmaps concerning clustering and groups, I use the ComplexHeatmap package, which has wonderful documentation. They have many options for splitting columns; for instance, slicing up the columns into groups first, and then clustering within those slices. See this section 2.7: Chapter 2 A Single Heatmap | ComplexHeatmap Complete Reference.