<p>If you order your matrix/data.frame you can sort your columns by decreasing &quot;Average&quot;, e.g.</p>
<pre class="lang-r prettyprint-override"><code>library(pheatmap)

heat &lt;- structure(c(3.05574677859363, -0.451189292623471, -0.398066832453666, 
                    -0.456927931251448, -0.393418432279742, -0.270234667496526, -0.451189292623471, 
                    -0.398066832453666, -0.456927931251448, -0.393418432279742, -0.270234667496526, 
                    -0.451189292623471, -0.398066832453666, -0.456927931251448, -0.393418432279742, 
                    -0.270234667496526, -0.451189292623471, -0.398066832453666, 2.0338091136055, 
                    -0.393418432279742, -0.270234667496526, 1.76078923919148, 1.96896026174603, 
                    1.97960246116808, -0.393418432279742), .Dim = c(5L, 5L), .Dimnames = list(
                      c(&quot;hsa-miR-548b-5p&quot;, &quot;hsa-miR-181c-5p&quot;, &quot;hsa-miR-181d-5p&quot;, 
                        &quot;hsa-miR-125a-3p&quot;, &quot;hsa-miR-4286&quot;), c(&quot;30566-001&quot;, &quot;30566-002&quot;, 
                                                              &quot;30566-003&quot;, &quot;30566-004&quot;, &quot;30566-005&quot;)))

m &lt;- structure(list(HealthStatus = c(&quot;Healthy&quot;, &quot;Healthy&quot;, &quot;Healthy&quot;, 
                                     &quot;Healthy&quot;, &quot;Healthy&quot;), Average = c(108.915677, 103.148873, 40.3359607, 
                                                                        88.4329528, 87.7756577)), row.names = c(&quot;30566-001&quot;, &quot;30566-002&quot;, 
                                                                                                                &quot;30566-003&quot;, &quot;30566-004&quot;, &quot;30566-005&quot;), class = &quot;data.frame&quot;)

pheatmap(heat[,order(m[,&quot;Average&quot;], decreasing = TRUE)], 
         cluster_rows=TRUE, show_rownames=T,
         cluster_cols=FALSE, annotation_col=m)
</code></pre>
<p><img src="https://i.imgur.com/LLbnkUG.png" alt="" /></p>
<p><sup>Created on 2023-09-06 with <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex v2.0.2</a></sup></p>

CC BY-SA 4.0
2023-09-05T23:33:33.710Z
2023-09-08T11:22:59.960Z
2023-09-08T11:22:59.960Z
jared_mamrot
<p>I want to plot a heat map in header I have the average of gene expression validated in lab for each sample</p>
<pre class="lang-r prettyprint-override"><code>dput(heat[1:5,1:5])

structure(c(3.05574677859363, -0.451189292623471, -0.398066832453666, 
-0.456927931251448, -0.393418432279742, -0.270234667496526, -0.451189292623471, 
-0.398066832453666, -0.456927931251448, -0.393418432279742, -0.270234667496526, 
-0.451189292623471, -0.398066832453666, -0.456927931251448, -0.393418432279742, 
-0.270234667496526, -0.451189292623471, -0.398066832453666, 2.0338091136055, 
-0.393418432279742, -0.270234667496526, 1.76078923919148, 1.96896026174603, 
1.97960246116808, -0.393418432279742), .Dim = c(5L, 5L), .Dimnames = list(
    c(&quot;hsa-miR-548b-5p&quot;, &quot;hsa-miR-181c-5p&quot;, &quot;hsa-miR-181d-5p&quot;, 
    &quot;hsa-miR-125a-3p&quot;, &quot;hsa-miR-4286&quot;), c(&quot;30566-001&quot;, &quot;30566-002&quot;, 
    &quot;30566-003&quot;, &quot;30566-004&quot;, &quot;30566-005&quot;)))


dput(m[1:5,c(2,7)])

structure(list(HealthStatus = c(&quot;Healthy&quot;, &quot;Healthy&quot;, &quot;Healthy&quot;, 
&quot;Healthy&quot;, &quot;Healthy&quot;), Average = c(108.915677, 103.148873, 40.3359607, 
88.4329528, 87.7756577)), row.names = c(&quot;30566-001&quot;, &quot;30566-002&quot;, 
&quot;30566-003&quot;, &quot;30566-004&quot;, &quot;30566-005&quot;), class = &quot;data.frame&quot;)


pheatmap(heat, cluster_rows=T, show_rownames=T,
         cluster_cols=FALSE, annotation_col=m[,c(2,7)],
         annotation_colors=annot_colors)
</code></pre>
<p>How I can order <code>average</code> in a descending way? <a href="https://i.sstatic.net/nsyYq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nsyYq.png" alt="enter image description here" /></a></p>

<heatmap>
Ordering average of gene expression in heat map
# Ordering average of gene expression in heat map

(Asked by: username_0 on 2023-09-05)

I want to plot a heat map in header I have the average of gene expression validated in lab for each sample
dput(heat[1:5,1:5])

structure(c(3.05574677859363, -0.451189292623471, -0.398066832453666, 
-0.456927931251448, -0.393418432279742, -0.270234667496526, -0.451189292623471, 
-0.398066832453666, -0.456927931251448, -0.393418432279742, -0.270234667496526, 
-0.451189292623471, -0.398066832453666, -0.456927931251448, -0.393418432279742, 
-0.270234667496526, -0.451189292623471, -0.398066832453666, 2.0338091136055, 
-0.393418432279742, -0.270234667496526, 1.76078923919148, 1.96896026174603, 
1.97960246116808, -0.393418432279742), .Dim = c(5L, 5L), .Dimnames = list(
    c("hsa-miR-548b-5p", "hsa-miR-181c-5p", "hsa-miR-181d-5p", 
    "hsa-miR-125a-3p", "hsa-miR-4286"), c("30566-001", "30566-002", 
    "30566-003", "30566-004", "30566-005")))


dput(m[1:5,c(2,7)])

structure(list(HealthStatus = c("Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy"), Average = c(108.915677, 103.148873, 40.3359607, 
88.4329528, 87.7756577)), row.names = c("30566-001", "30566-002", 
"30566-003", "30566-004", "30566-005"), class = "data.frame")


pheatmap(heat, cluster_rows=T, show_rownames=T,
         cluster_cols=FALSE, annotation_col=m[,c(2,7)],
         annotation_colors=annot_colors)

How I can order average in a descending way?

**Tags:** heatmap

---

## 1 answers

### Answer by username_1 on 2023-09-05. Score: 4 | Accepted answer

If you order your matrix/data.frame you can sort your columns by decreasing "Average", e.g.
library(pheatmap)

heat <- structure(c(3.05574677859363, -0.451189292623471, -0.398066832453666, 
                    -0.456927931251448, -0.393418432279742, -0.270234667496526, -0.451189292623471, 
                    -0.398066832453666, -0.456927931251448, -0.393418432279742, -0.270234667496526, 
                    -0.451189292623471, -0.398066832453666, -0.456927931251448, -0.393418432279742, 
                    -0.270234667496526, -0.451189292623471, -0.398066832453666, 2.0338091136055, 
                    -0.393418432279742, -0.270234667496526, 1.76078923919148, 1.96896026174603, 
                    1.97960246116808, -0.393418432279742), .Dim = c(5L, 5L), .Dimnames = list(
                      c("hsa-miR-548b-5p", "hsa-miR-181c-5p", "hsa-miR-181d-5p", 
                        "hsa-miR-125a-3p", "hsa-miR-4286"), c("30566-001", "30566-002", 
                                                              "30566-003", "30566-004", "30566-005")))

m <- structure(list(HealthStatus = c("Healthy", "Healthy", "Healthy", 
                                     "Healthy", "Healthy"), Average = c(108.915677, 103.148873, 40.3359607, 
                                                                        88.4329528, 87.7756577)), row.names = c("30566-001", "30566-002", 
                                                                                                                "30566-003", "30566-004", "30566-005"), class = "data.frame")

pheatmap(heat[,order(m[,"Average"], decreasing = TRUE)], 
         cluster_rows=TRUE, show_rownames=T,
         cluster_cols=FALSE, annotation_col=m)


Created on 2023-09-06 with reprex v2.0.2


---

