<p>I'm not sure exactly what was going on with your data, but I read it in as a raw data.frame using <code>as.matrix(read.table(., row.names=1, header=T))</code>. The issue was that you were reading it in without a header, so the col.names were being read in as part of the matrix. But apart from that, the rest of your command was fine:</p>
<pre><code>library(gplots)

data = structure(c(-0.1152, 0.1548, 0.0037, 0, 0, 0, 0, 0, 0, 0.1369, 
0.1914, 0, 0.0499, 0, 0, 0, 0, 0.1259, 0, 0.1854, -0.4412, 0, 
-0.4425, 0, 0, -0.5141, 0, -0.3463, 0.1529, -0.1176, 0.4153, 
-0.4412, 0.2169, 0.1894, 0.3288, 0.5549, -0.2549, 0.1915, -0.0759, 
0.1255, 0.2269, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.4412, -0.1923, 
0.1502, 0, 0, 0, -0.1978, 0.0828, 0, 0.2214, 0.192, 0, -0.2651, 
0.192, -0.1412, -0.287, 0, -0.2299, 0, 0.1095, 0, 0.0166, 0, 
0.1617, -0.16, 0, 0, 0.1255, 0, 0.2298, 0.153, 0.1627, 0, 0.293, 
0.2476, -0.1565, 0.0899, 0, 0.2217, 0.2274, 0.1764, 0.1538, 0, 
0.1747, 0.1857, -0.2309, 0.0827, -0.0609, 0.0754, -0.2653, 0.1461, 
0.1611, -0.1201, 0, 0, 0.114, 0.237, 0.1738, 0, 0.2269, 0, 0.0068, 
0, 0.1288, -0.1727, -0.1187, -0.1502, -0.1502, 0, -0.4104, -0.1305, 
-0.1126, -0.2642, -0.1264, -0.2403, 0.1522, 0.0211, 0.1175, -0.0549, 
-0.2571, -0.1305, -0.2143, -0.2153, -0.0566, -0.2068, 0.0406, 
-0.087, 0.079, -0.0238), .Dim = c(10L, 14L), .Dimnames = list(
    c(&quot;AI&quot;, &quot;AUDC&quot;, &quot;AUDT&quot;, &quot;AUT&quot;, &quot;BD&quot;, &quot;CB&quot;, &quot;CD&quot;, &quot;DPW&quot;, &quot;MDD&quot;, 
    &quot;SZ&quot;), c(&quot;BI&quot;, &quot;BB&quot;, &quot;CD&quot;, &quot;CGC&quot;, &quot;CTF&quot;, &quot;HDT&quot;, &quot;MTS&quot;, &quot;NOI&quot;, 
    &quot;OFI&quot;, &quot;SRV&quot;, &quot;SAF&quot;, &quot;FFI&quot;, &quot;SUM&quot;, &quot;WIN&quot;)))


heatmap.2(data,
          trace =&quot;none&quot;, 
          density = &quot;none&quot;, 
          col = redblue(100),
         key=TRUE, 
         keysize=1,
          cexRow =1, 
          cexCol =1,
          margins = c(5,5),
          labCol = colnames(data),
          scale=&quot;row&quot;,
          hclust=function(x) hclust(x,method = &quot;complete&quot;))
</code></pre>
<p><a href="https://i.sstatic.net/a7voK.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/a7voK.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2020-10-10T15:46:24.723Z
Super answer, thank you and thanks for editing and clarifying the original question.
M__
CC BY-SA 4.0
2020-10-11T02:31:20.807Z
Thanks for the help. I have run the code exactly as you have sent it, but still no key.It is really annoying.
Aranyak Goswami
CC BY-SA 4.0
2020-10-11T10:12:14.387Z
@AranyakGoswami Could you edit your main post to show the exact command you have used and post a picture of the figure you created (without the Key), as well as the results of ``sessionInfo()``? Thanks.
user438383
CC BY-SA 4.0
2020-10-11T23:37:55.010Z
Hi @AranyakGoswami this is a really good answer. Could please mark this resolved and start a new question? I wasn't familiar with the gplots package. Sectioning a project into a series of questions is the best way to resolve it and fairest way for the respondee
M__
CC BY-SA 4.0
2020-10-12T00:41:04.323Z
the issue is resolved.
Aranyak Goswami
CC BY-SA 4.0
2020-10-12T12:37:17.290Z
Hi @AranyakGoswami Great! Please could you sign the answer as 'resolved' and a green tick will appear besides the answer? This avoids the question (which has been well answered) being recycled at a later date and gives credit accordingly.
M__
CC BY-SA 4.0
2020-10-12T14:19:06.190Z
resolved. However I am still unable to have the color key on my heatmap.
Aranyak Goswami
CC BY-SA 4.0
2020-10-10T13:13:46.363Z
2020-10-10T13:13:46.363Z
user438383
<pre><code>library(gplots)

data=structure(c(-0.1152, 0.1548, 0.0037, 0, 0, 0, 0, 0, 0, 0.1369, 
0.1914, 0, 0.0499, 0, 0, 0, 0, 0.1259, 0, 0.1854, -0.4412, 0, 
-0.4425, 0, 0, -0.5141, 0, -0.3463, 0.1529, -0.1176, 0.4153, 
-0.4412, 0.2169, 0.1894, 0.3288, 0.5549, -0.2549, 0.1915, -0.0759, 
0.1255, 0.2269, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.4412, -0.1923, 
0.1502, 0, 0, 0, -0.1978, 0.0828, 0, 0.2214, 0.192, 0, -0.2651, 
0.192, -0.1412, -0.287, 0, -0.2299, 0, 0.1095, 0, 0.0166, 0, 
0.1617, -0.16, 0, 0, 0.1255, 0, 0.2298, 0.153, 0.1627, 0, 0.293, 
0.2476, -0.1565, 0.0899, 0, 0.2217, 0.2274, 0.1764, 0.1538, 0, 
0.1747, 0.1857, -0.2309, 0.0827, -0.0609, 0.0754, -0.2653, 0.1461, 
0.1611, -0.1201, 0, 0, 0.114, 0.237, 0.1738, 0, 0.2269, 0, 0.0068, 
0, 0.1288, -0.1727, -0.1187, -0.1502, -0.1502, 0, -0.4104, -0.1305, 
-0.1126, -0.2642, -0.1264, -0.2403, 0.1522, 0.0211, 0.1175, -0.0549, 
-0.2571, -0.1305, -0.2143, -0.2153, -0.0566, -0.2068, 0.0406, 
-0.087, 0.079, -0.0238), .Dim = c(10L, 14L), .Dimnames = list(
    c(&quot;AI&quot;, &quot;AUDC&quot;, &quot;AUDT&quot;, &quot;AUT&quot;, &quot;BD&quot;, &quot;CB&quot;, &quot;CD&quot;, &quot;DPW&quot;, &quot;MDD&quot;, 
    &quot;SZ&quot;), c(&quot;BI&quot;, &quot;BB&quot;, &quot;CD&quot;, &quot;CGC&quot;, &quot;CTF&quot;, &quot;HDT&quot;, &quot;MTS&quot;, &quot;NOI&quot;, 
    &quot;OFI&quot;, &quot;SRV&quot;, &quot;SAF&quot;, &quot;FFI&quot;, &quot;SUM&quot;, &quot;WIN&quot;)))

data &lt;- read.csv(&quot;C:/Users/Lenovo/Desktop/Correlation matrix VITD _Disorder_R/Z1.txt&quot;, header=TRUE, row.names=1,sep=&quot;&quot;)
data = as.matrix(data[![enter image description here][1]][1])
heatmap.2(data,
          trace =&quot;none&quot;, 
          density = &quot;none&quot;, 
          col = redblue(100),
         key=TRUE, 
         keysize=1,
          cexRow =1, 
          cexCol =1,
          margins = c(5,5),
          labCol = colnames(data),
          scale=&quot;row&quot;,
          hclust=function(x) hclust(x,method = &quot;complete&quot;))


  [1]: https://i.sstatic.net/jlwf4.png
</code></pre>

CC BY-SA 4.0
2020-10-10T06:42:41.490Z
I ran your code and it already displays a colour key - what's the issue?]
user438383
CC BY-SA 4.0
2020-10-10T10:21:47.640Z
Ah @user438383 your edit makes the data look alot better. I suspect that will now run fine. You could just post your edit as an answer. In fact I would  recommend it because otherwise a resolved question will be scored as an unresolved, unanswered question.
M__
<heatmap>
I want to display color key on my heatmap
# I want to display color key on my heatmap

(Asked by: username_0 on 2020-10-10)

library(gplots)

data=structure(c(-0.1152, 0.1548, 0.0037, 0, 0, 0, 0, 0, 0, 0.1369, 
0.1914, 0, 0.0499, 0, 0, 0, 0, 0.1259, 0, 0.1854, -0.4412, 0, 
-0.4425, 0, 0, -0.5141, 0, -0.3463, 0.1529, -0.1176, 0.4153, 
-0.4412, 0.2169, 0.1894, 0.3288, 0.5549, -0.2549, 0.1915, -0.0759, 
0.1255, 0.2269, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.4412, -0.1923, 
0.1502, 0, 0, 0, -0.1978, 0.0828, 0, 0.2214, 0.192, 0, -0.2651, 
0.192, -0.1412, -0.287, 0, -0.2299, 0, 0.1095, 0, 0.0166, 0, 
0.1617, -0.16, 0, 0, 0.1255, 0, 0.2298, 0.153, 0.1627, 0, 0.293, 
0.2476, -0.1565, 0.0899, 0, 0.2217, 0.2274, 0.1764, 0.1538, 0, 
0.1747, 0.1857, -0.2309, 0.0827, -0.0609, 0.0754, -0.2653, 0.1461, 
0.1611, -0.1201, 0, 0, 0.114, 0.237, 0.1738, 0, 0.2269, 0, 0.0068, 
0, 0.1288, -0.1727, -0.1187, -0.1502, -0.1502, 0, -0.4104, -0.1305, 
-0.1126, -0.2642, -0.1264, -0.2403, 0.1522, 0.0211, 0.1175, -0.0549, 
-0.2571, -0.1305, -0.2143, -0.2153, -0.0566, -0.2068, 0.0406, 
-0.087, 0.079, -0.0238), .Dim = c(10L, 14L), .Dimnames = list(
    c("AI", "AUDC", "AUDT", "AUT", "BD", "CB", "CD", "DPW", "MDD", 
    "SZ"), c("BI", "BB", "CD", "CGC", "CTF", "HDT", "MTS", "NOI", 
    "OFI", "SRV", "SAF", "FFI", "SUM", "WIN")))

data <- read.csv("C:/Users/Lenovo/Desktop/Correlation matrix VITD _Disorder_R/Z1.txt", header=TRUE, row.names=1,sep="")
data = as.matrix(data[![enter image description here][1]][1])
heatmap.2(data,
          trace ="none", 
          density = "none", 
          col = redblue(100),
         key=TRUE, 
         keysize=1,
          cexRow =1, 
          cexCol =1,
          margins = c(5,5),
          labCol = colnames(data),
          scale="row",
          hclust=function(x) hclust(x,method = "complete"))


  [1]: https://i.sstatic.net/jlwf4.png

**Tags:** heatmap
> **Comments:**
> **username_1**: I ran your code and it already displays a colour key - what's the issue?]  
> **username_2**: Ah @user438383 your edit makes the data look alot better. I suspect that will now run fine. You could just post your edit as an answer. In fact I would  recommend it because otherwise a resolved question will be scored as an unresolved, unanswered question.  

---

## 1 answers

### Answer by username_1 on 2020-10-10. Score: 1

I'm not sure exactly what was going on with your data, but I read it in as a raw data.frame using as.matrix(read.table(., row.names=1, header=T)). The issue was that you were reading it in without a header, so the col.names were being read in as part of the matrix. But apart from that, the rest of your command was fine:
library(gplots)

data = structure(c(-0.1152, 0.1548, 0.0037, 0, 0, 0, 0, 0, 0, 0.1369, 
0.1914, 0, 0.0499, 0, 0, 0, 0, 0.1259, 0, 0.1854, -0.4412, 0, 
-0.4425, 0, 0, -0.5141, 0, -0.3463, 0.1529, -0.1176, 0.4153, 
-0.4412, 0.2169, 0.1894, 0.3288, 0.5549, -0.2549, 0.1915, -0.0759, 
0.1255, 0.2269, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.4412, -0.1923, 
0.1502, 0, 0, 0, -0.1978, 0.0828, 0, 0.2214, 0.192, 0, -0.2651, 
0.192, -0.1412, -0.287, 0, -0.2299, 0, 0.1095, 0, 0.0166, 0, 
0.1617, -0.16, 0, 0, 0.1255, 0, 0.2298, 0.153, 0.1627, 0, 0.293, 
0.2476, -0.1565, 0.0899, 0, 0.2217, 0.2274, 0.1764, 0.1538, 0, 
0.1747, 0.1857, -0.2309, 0.0827, -0.0609, 0.0754, -0.2653, 0.1461, 
0.1611, -0.1201, 0, 0, 0.114, 0.237, 0.1738, 0, 0.2269, 0, 0.0068, 
0, 0.1288, -0.1727, -0.1187, -0.1502, -0.1502, 0, -0.4104, -0.1305, 
-0.1126, -0.2642, -0.1264, -0.2403, 0.1522, 0.0211, 0.1175, -0.0549, 
-0.2571, -0.1305, -0.2143, -0.2153, -0.0566, -0.2068, 0.0406, 
-0.087, 0.079, -0.0238), .Dim = c(10L, 14L), .Dimnames = list(
    c("AI", "AUDC", "AUDT", "AUT", "BD", "CB", "CD", "DPW", "MDD", 
    "SZ"), c("BI", "BB", "CD", "CGC", "CTF", "HDT", "MTS", "NOI", 
    "OFI", "SRV", "SAF", "FFI", "SUM", "WIN")))


heatmap.2(data,
          trace ="none", 
          density = "none", 
          col = redblue(100),
         key=TRUE, 
         keysize=1,
          cexRow =1, 
          cexCol =1,
          margins = c(5,5),
          labCol = colnames(data),
          scale="row",
          hclust=function(x) hclust(x,method = "complete"))

> **Comments:**
> **username_2**: Super answer, thank you and thanks for editing and clarifying the original question.  
> **username_0**: Thanks for the help. I have run the code exactly as you have sent it, but still no key.It is really annoying.  
> **username_1**: @AranyakGoswami Could you edit your main post to show the exact command you have used and post a picture of the figure you created (without the Key), as well as the results of ``sessionInfo()``? Thanks.  
> **username_2**: Hi @AranyakGoswami this is a really good answer. Could please mark this resolved and start a new question? I wasn't familiar with the gplots package. Sectioning a project into a series of questions is the best way to resolve it and fairest way for the respondee  
> **username_0**: the issue is resolved.  
> **username_2**: Hi @AranyakGoswami Great! Please could you sign the answer as 'resolved' and a green tick will appear besides the answer? This avoids the question (which has been well answered) being recycled at a later date and gives credit accordingly.  
> **username_0**: resolved. However I am still unable to have the color key on my heatmap.  

---

