# I want to display color key on my heatmap

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.stack.imgur.com/jlwf4.png

• I ran your code and it already displays a colour key - what's the issue?] – user438383 Oct 10 '20 at 6:42
• 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__ Oct 10 '20 at 10:21

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"))


• Super answer, thank you and thanks for editing and clarifying the original question. – M__ Oct 10 '20 at 15:46
• 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 Oct 11 '20 at 2:31
• @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 Oct 11 '20 at 10:12
• 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__ Oct 11 '20 at 23:37
• the issue is resolved. – Aranyak Goswami Oct 12 '20 at 0:41