1

I need to reproduce this heatmap as best as possible in the next couple of days.

enter image description here

My data is an xlsx file with multiple sheets, I cleaned it as good as I could and it should be in an okay format. Do you have an Idea how to create a heatmap as similar as possible to this ? And what would be the most convenient way.

Whenever I use Pheatmap I get this error.. not sure how to fix it.

     Error in hclust(d, method = method) :
     NA/NaN/Inf in foreign function call (arg 10)

Issue was solved with: (much thanks to @starja)

So I tried to do it to the best of my abilities and I got a heatmap. This was the code in the end!

all_data2 <- cbind(amino,sphingo,hexoses,phospha,lyso,all_data)
matrix_data <- as.matrix(all_data2[, 3:73])
log(matrix_data)
rownames(matrix_data) <- all_data2$`Sample Identification`
pheatmap(
 mat = matrix_data
)

Is there any way I can do a seperation into the analyte classes, like in the example given which are basically the data from the different sheets ? and is there a way to enlarge it a bit ?
How could I imputate the NA´s easily, so the resulting data is not changed.

this is the heatmap I do need help improving it!! so anyone who has tips to Improve on my code is very welcome! :)

enter image description here

Here is one of the sheets of the sample data

Heatmap_data <- structure(list(`Sample Identification` = c(2, 2.2, 3, 3.2, 4, 
4.2, 6, 6.2, 7, 7.2, 8, 8.2, 9, 9.2, 10, 10.2, 11, 11.2, 12, 
12.2, 13, 13.2, 14, 14.2, 15, 15.2, 16, 16.2, 17, 17.2, 18, 18.2, 
20, 20.2, 22, 22.2, 23, 23.2, 24, 24.2, 25, 25.2, 26, 26.2, 28, 
28.2, 29, 29.2, 33, 33.2, 34, 34.2, 36, 36.2, 37, 37.2, 38, 38.2, 
39, 39.2, 40, 40.2, 41, 41.2, 42, 42.2, 43, 43.2, 44, 44.2, 45, 
45.2, 46, 46.2, 47, 47.2, 48, 48.2, 49, 49.2, 50, 50.2, 51, 51.2, 
52, 52.2, 54, 54.2, 56, 56.2), `Time point` = c("T0", "T1", "T0", 
"T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", 
"T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", 
"T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", 
"T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", 
"T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", 
"T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", 
"T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", 
"T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1", "T0", "T1"), 
    C0 = c(62.9, 37.8, 30.8, 25.3, 30.6, 34.6, 38.7, 43.3, 27.4, 
    35.3, 28.1, 24, 38.7, 37.1, 59.7, 20.4, 21.6, 20, 29.2, 26.7, 
    49.7, 40.8, 38.8, 37.9, 29.9, 23.6, 46.7, 52.1, 37.6, 40.4, 
    38.8, 34.9, 50.1, 45.4, 46.5, 43, 13.8, 25.4, 36.5, 48.8, 
    19.6, 26.5, 45.8, 13.5, 34.3, 42.6, 28.5, 27.5, 31.8, 35.7, 
    42.2, 42.6, 41.3, 40.4, 39.1, 39.5, 33.7, 34.1, 49, 37.4, 
    37.1, 51.2, 51.6, 40.2, 40.6, 35.5, 38.2, 46.5, 25, 26.2, 
    22.4, 22, 42.8, 39.4, 50.7, 46, 45.7, 47.2, 50.7, 24.4, 31.8, 
    52.8, 46.9, 39.9, 12.2, 19.7, 26.3, 40.7, 20.5, 16.2), C2 = c(17.8, 
    6.67, 5.41, 4.56, 11.4, 6.73, 8.24, 7.68, 10.8, 11.9, 7.56, 
    6.21, 4.71, 5.9, 12.3, 7.09, 5.24, 3.68, 11.5, 15, 8.18, 
    7.96, 7.86, 7.5, 6.68, 5.65, 21.5, 17.7, 8.91, 7.6, 9.33, 
    9.27, 19.3, 17.8, 8.64, 10.1, 5.08, 4.88, 6.06, 5.07, 6.79, 
    11.2, 27.4, 6.57, 7.99, 11.4, 11.3, 9.69, 3.51, 3.7, 19.5, 
    13.2, 10.1, 5.39, 6.47, 6.09, 9.92, 5.76, 14.3, 9.44, 12.3, 
    9.57, 11.3, 9, 6.53, 7.01, 9.16, 7.05, 9.06, 5.38, 11.4, 
    4.29, 7.35, 10.4, 20.1, 12.6, 6.85, 8.07, 7.58, 4.07, 9.85, 
    17.2, 10.3, 14.3, 4.82, 5.04, 7.1, 7.64, 5.81, 4.17), C3 = c(0.645, 
    0.543, 0.296, 0.245, 0.267, 0.282, 0.437, 0.528, 0.262, 0.316, 
    0.204, 0.218, 0.358, 0.32, 0.676, 0.248, 0.278, 0.291, 0.23, 
    0.263, 0.656, 0.617, 0.438, 0.412, 0.304, 0.273, 0.68, 0.998, 
    0.429, 0.477, 0.337, 0.267, 0.531, 0.504, 0.716, 0.545, 0.215, 
    0.652, 0.36, 0.543, 0.199, 0.445, 0.432, 0.231, 0.377, 0.749, 
    0.409, 0.224, 0.182, 0.259, 0.454, 0.437, 0.36, 0.435, 0.452, 
    0.473, 0.247, 0.325, 0.493, 0.302, 0.687, 1.02, 0.341, 0.305, 
    0.247, 0.228, 0.289, 0.525, 0.192, 0.316, 0.17, 0.148, 0.425, 
    0.513, 0.743, 0.692, 0.395, 0.457, 0.388, 0.318, 0.292, 0.455, 
    0.604, 0.68, 0.18, 0.275, 0.314, 0.549, 0.182, 0.163), `C3-DC (C4-OH)` = c(0.091, 
    0.068, NA, NA, 0.089, NA, 0.063, 0.044, 0.072, 0.069, 0.049, 
    0.063, 0.048, 0.048, 0.113, 0.08, 0.049, NA, 0.117, 0.168, 
    0.054, 0.055, 0.064, 0.047, 0.051, NA, 0.168, 0.11, 0.056, 
    0.092, 0.064, 0.083, 0.108, 0.102, 0.084, 0.07, 0.048, NA, 
    NA, NA, 0.117, 0.097, 0.137, 0.123, 0.066, 0.052, 0.068, 
    0.06, NA, NA, 0.08, 0.12, 0.062, 0.055, NA, 0.051, 0.093, 
    0.07, 0.062, 0.046, 0.075, 0.081, 0.073, 0.059, 0.046, 0.046, 
    0.064, NA, 0.06, 0.059, 0.096, NA, 0.275, 0.315, 0.109, 0.072, 
    NA, 0.046, 0.057, 0.057, 0.077, 0.177, 0.061, 0.07, 0.07, 
    0.056, 0.06, 0.057, 0.052, 0.05), C4 = c(0.401, 0.252, 0.176, 
    0.145, 0.168, 0.158, 0.171, 0.181, 0.413, 0.602, 0.174, 0.198, 
    0.162, 0.205, 0.795, 0.335, 0.258, 0.215, 0.143, 0.153, 0.515, 
    0.348, 0.233, 0.264, 0.773, 0.647, 0.537, 0.496, 0.202, 0.856, 
    0.247, 0.188, 0.7, 1.05, 0.185, 0.2, 0.424, 0.383, 0.202, 
    0.33, 0.357, 0.541, 0.627, 0.671, 0.643, 0.669, 0.256, 0.156, 
    0.175, 0.212, 0.66, 0.569, 0.257, 0.287, 0.202, 0.325, 0.157, 
    0.175, 0.535, 0.356, 0.256, 0.475, 0.217, 0.273, 0.19, 0.191, 
    0.273, 0.438, 0.263, 0.464, 0.437, 0.123, 0.155, 0.18, 0.753, 
    0.543, 0.186, 0.242, 0.241, 0.322, 0.266, 0.201, 0.505, 0.707, 
    0.165, 0.254, 0.213, 0.325, 0.146, 0.153), C5 = c(0.14, 0.148, 
    0.114, 0.131, 0.081, 0.105, 0.108, 0.147, 0.088, 0.128, 0.074, 
    0.11, 0.108, 0.105, 1.22, 0.581, 0.105, 0.125, 0.073, 0.105, 
    0.425, 0.172, 0.119, 0.13, 0.149, 0.176, 0.163, 0.142, 0.125, 
    1.28, 0.118, 0.124, 0.928, 1.28, 0.13, 0.12, 0.431, 0.161, 
    0.071, 0.154, 0.176, 0.269, 0.163, 2.81, 0.874, 0.492, 0.089, 
    0.098, 0.094, 0.094, 0.708, 0.948, 0.105, 0.114, 0.105, 0.174, 
    0.087, 0.112, 0.216, 0.187, 0.094, 0.222, 0.113, 0.476, 0.091, 
    0.098, 0.097, 0.171, 0.087, 0.844, 0.483, 0.081, 0.065, 0.188, 
    0.124, 0.105, 0.098, 0.148, 0.092, 0.188, 0.099, 0.138, 0.148, 
    0.198, 0.073, 0.104, 0.082, 0.155, 0.061, 0.077), C10 = c(0.183, 
    0.24, 0.137, 0.144, 0.259, 0.189, 0.175, 0.205, 0.264, 0.235, 
    0.214, 0.163, 0.203, 0.341, 0.192, 0.146, 0.218, 0.133, 0.224, 
    0.344, 0.293, 0.233, 0.334, 0.203, 0.21, 0.282, 0.209, 0.309, 
    0.179, 0.155, 0.422, 0.221, 0.264, 0.29, 0.161, 0.177, 0.185, 
    0.211, 0.186, 0.127, 0.194, 0.364, 0.805, 0.189, 0.342, 0.231, 
    0.402, 0.429, 0.156, NA, 0.28, 0.376, 0.176, NA, 0.164, 0.219, 
    0.141, 0.16, 0.297, 0.277, 0.154, 0.151, 0.162, 0.304, 0.171, 
    0.227, 0.209, 0.205, 0.2, 0.183, 0.326, NA, 0.188, 0.264, 
    0.221, 0.154, 0.129, 0.205, 0.194, 0.145, 0.141, 0.221, 0.308, 
    0.475, 0.188, 0.234, 0.239, 0.241, 0.19, 0.206), `C14:1` = c(0.114, 
    0.082, 0.097, 0.073, 0.129, 0.105, 0.072, 0.088, 0.183, 0.156, 
    0.094, 0.066, 0.086, 0.122, 0.107, 0.079, 0.121, 0.067, 0.108, 
    0.168, 0.11, 0.088, 0.092, 0.08, 0.081, 0.089, 0.179, 0.227, 
    0.078, 0.049, 0.146, 0.079, 0.155, 0.17, 0.082, 0.097, 0.077, 
    0.067, 0.082, 0.051, 0.098, 0.181, 0.23, 0.114, 0.135, 0.112, 
    0.217, 0.165, 0.071, 0.056, 0.169, 0.104, 0.124, 0.051, 0.084, 
    0.08, 0.137, 0.086, 0.111, 0.101, 0.072, 0.055, 0.124, 0.194, 
    0.101, 0.129, 0.088, 0.077, 0.11, 0.087, 0.198, 0.065, 0.105, 
    0.185, 0.227, 0.124, 0.069, 0.073, 0.11, 0.08, 0.086, 0.165, 
    0.106, 0.173, 0.143, 0.137, 0.096, 0.094, 0.119, 0.102), 
    `C14:2` = c(0.033, 0.023, NA, NA, 0.036, 0.022, NA, 0.025, 
    0.054, 0.042, 0.021, 0.021, 0.025, 0.037, 0.024, NA, 0.041, 
    NA, NA, 0.024, 0.027, NA, 0.028, NA, 0.025, 0.033, 0.055, 
    0.076, 0.027, NA, 0.028, 0.022, 0.045, 0.048, 0.026, 0.041, 
    NA, NA, NA, NA, 0.024, 0.059, 0.088, 0.023, 0.054, 0.045, 
    0.053, 0.045, NA, NA, 0.061, 0.034, 0.034, NA, NA, NA, 0.038, 
    0.021, 0.042, 0.041, NA, NA, 0.034, 0.073, 0.027, 0.033, 
    0.022, 0.021, 0.028, NA, 0.061, NA, 0.024, 0.046, 0.055, 
    0.033, NA, 0.03, 0.028, 0.025, 0.022, 0.038, 0.03, 0.061, 
    0.037, 0.039, 0.027, 0.023, 0.031, 0.025), C16 = c(0.099, 
    0.133, 0.152, 0.087, 0.116, 0.099, 0.092, 0.13, 0.181, 0.154, 
    0.112, 0.095, 0.157, 0.161, 0.159, 0.145, 0.176, 0.095, 0.12, 
    0.14, 0.109, 0.109, 0.099, 0.078, 0.1, 0.074, 0.117, 0.152, 
    0.098, 0.063, 0.169, 0.127, 0.233, 0.174, 0.111, 0.134, 0.099, 
    0.076, 0.123, 0.098, 0.109, 0.099, 0.131, 0.096, 0.196, 0.106, 
    0.109, 0.105, 0.071, 0.066, 0.127, 0.094, 0.116, 0.07, 0.114, 
    0.099, 0.137, 0.105, 0.12, 0.108, 0.099, 0.094, 0.128, 0.147, 
    0.108, 0.119, 0.116, 0.13, 0.088, 0.089, 0.137, 0.076, 0.105, 
    0.163, 0.18, 0.149, 0.112, 0.111, 0.096, 0.133, 0.123, 0.149, 
    0.147, 0.178, 0.093, 0.131, 0.097, 0.115, 0.128, 0.111), 
    C18 = c(0.038, 0.06, 0.066, 0.06, 0.048, 0.046, 0.05, 0.067, 
    0.078, 0.069, 0.051, 0.05, 0.087, 0.08, 0.057, 0.041, 0.084, 
    0.057, 0.039, 0.046, 0.057, 0.055, 0.039, 0.036, 0.05, 0.037, 
    0.038, 0.043, 0.046, 0.041, 0.054, 0.056, 0.098, 0.084, 0.041, 
    0.053, 0.058, 0.026, 0.058, 0.054, 0.036, 0.043, 0.047, 0.082, 
    0.081, 0.037, 0.041, 0.037, 0.037, 0.032, 0.053, 0.033, 0.037, 
    0.033, 0.046, 0.057, 0.061, 0.048, 0.048, 0.049, 0.056, 0.052, 
    0.07, 0.062, 0.06, 0.055, 0.049, 0.043, 0.037, 0.037, 0.053, 
    0.026, 0.053, 0.068, 0.072, 0.056, 0.046, 0.044, 0.033, 0.067, 
    0.05, 0.047, 0.052, 0.072, 0.029, 0.052, 0.053, 0.055, 0.071, 
    0.07), `C18:1` = c(0.137, 0.145, 0.181, 0.114, 0.142, 0.104, 
    0.108, 0.191, 0.247, 0.21, 0.167, 0.107, 0.237, 0.209, 0.217, 
    0.197, 0.23, 0.108, 0.153, 0.192, 0.123, 0.097, 0.148, 0.146, 
    0.163, 0.126, 0.186, 0.234, 0.177, 0.072, 0.241, 0.13, 0.33, 
    0.243, 0.13, 0.197, 0.118, 0.077, 0.161, 0.091, 0.133, 0.16, 
    0.24, 0.096, 0.176, 0.1, 0.173, 0.18, 0.115, 0.066, 0.149, 
    0.104, 0.14, 0.073, 0.132, 0.11, 0.154, 0.127, 0.168, 0.162, 
    0.139, 0.094, 0.179, 0.183, 0.129, 0.154, 0.149, 0.131, 0.125, 
    0.127, 0.251, 0.151, 0.143, 0.236, 0.307, 0.242, 0.12, 0.127, 
    0.135, 0.166, 0.167, 0.171, 0.164, 0.252, 0.16, 0.21, 0.147, 
    0.156, 0.167, 0.127), `C18:2` = c(0.038, 0.04, 0.038, 0.033, 
    0.04, 0.026, 0.027, 0.07, 0.06, 0.053, 0.032, 0.026, 0.061, 
    0.054, 0.045, 0.046, 0.065, 0.039, 0.018, 0.022, 0.033, 0.023, 
    0.043, 0.033, 0.054, 0.048, 0.049, 0.064, 0.056, 0.033, 0.047, 
    0.032, 0.079, 0.055, 0.047, 0.083, 0.022, 0.015, 0.034, 0.021, 
    0.023, 0.043, 0.06, 0.017, 0.058, 0.044, 0.028, 0.029, 0.028, 
    0.022, 0.047, 0.031, 0.033, 0.026, 0.029, 0.023, 0.047, 0.038, 
    0.052, 0.053, 0.033, 0.023, 0.045, 0.05, 0.032, 0.037, 0.033, 
    0.029, 0.025, 0.026, 0.068, 0.059, 0.04, 0.046, 0.074, 0.068, 
    0.033, 0.06, 0.027, 0.055, 0.042, 0.038, 0.036, 0.068, 0.034, 
    0.055, 0.03, 0.039, 0.04, 0.034)), row.names = c(NA, -90L
), class = c("tbl_df", "tbl", "data.frame"))
doc elfein
  • 27
  • 5
  • This might be helpful https://www.reddit.com/r/rstats/comments/ipea5b/help_hey_i_am_a_beginner_to_programming_and_r_and/ – doc elfein Sep 09 '20 at 19:10
  • Welcome to SO! Please also provide example data (e.g. with using `dput`), otherwise it is difficult to help you. See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – starja Sep 09 '20 at 19:17
  • hey! is this what you meant? – doc elfein Sep 09 '20 at 19:26
  • yes, but what data exactly is it? `all_data`? and what is the `col_group` data? – starja Sep 09 '20 at 19:32
  • ```all_data``` is the data I just sent in with dput, its an excel sheet. the col_group was in the code i copied, I tried to replace it with the ```all_data```so I could use it, found it on this link https://slowkow.com/notes/pheatmap-tutorial/ – doc elfein Sep 09 '20 at 19:35
  • sorry if I´m making stupid mistakes, I just started out with R and need to produce a heatmap of multiple excel sheets in the next couple of days... – doc elfein Sep 09 '20 at 19:36
  • Can you clarify what you are trying to map? The example data you supplied (Heatmap_data) is a data frame with 15 variables, but a heat map generally displays only 3 dimensions or variables. The call to `Heatmap_data %>% pheatmap()` appears to fail because you are passing a data frame to a function that expects a matrix. – Brian Fisher Sep 09 '20 at 19:52
  • I tried to just be able to produce a heatmap with the ```pheatmap()```function, now my task would be to take and combine 6 of these excel sheets which are structured similarly with different measurements and metabolites and create a heatmap. I am trying to understand how to reproduce the Heatmap example loaded in the beginning of this question ```https://imgur.com/a/HdDAvlB``` – doc elfein Sep 09 '20 at 20:06
  • @BrianFisher a heatmap can show an arbitrary large number of columns – starja Sep 09 '20 at 20:07

1 Answers1

0

Here is a basic heatmap with which you can work. I guess problem was that you've had non-numerical values still in your matrix. Notice that for the actual data I only used the numerical values and set the names as the rownames of the matrix.

library(pheatmap)
matrix_data <- as.matrix(Heatmap_data[, 3:15])
rownames(matrix_data) <- Heatmap_data$`Sample Identification`

pheatmap(
  mat = matrix_data
)

Created on 2020-09-09 by the reprex package (v0.3.0)

starja
  • 9,887
  • 1
  • 13
  • 28
  • This is amazing!! Thank you! is there a clever way on how to combine multiple excel sheets with a similar structur to this one in a heatmap ? like in the given example heatmap? – doc elfein Sep 09 '20 at 19:55
  • and how could I log transform the Data ? Still blown away thanks for the quick help!! – doc elfein Sep 09 '20 at 19:58
  • you can use `log(matrix_data)`. If you want a heatmap for every excel sheet, you can use a `for` loop; if you want all data together and the columns are the same in every sheet, you could bind the rows together with `rbind` – starja Sep 09 '20 at 20:06
  • Sample Identification would be the same column for all the sheets but they have a different number of columns with different data while its always 90 rows. , its similar but other metabolites with name. – doc elfein Sep 09 '20 at 20:20
  • so the rows are always the same samples, but the columns are the measurements for different metabolites? Then you can bind the columns together with `cbind` – starja Sep 09 '20 at 20:25
  • So I try to do it to the best of my abilities and I got a heatmap. this was the code in the end! ```library(pheatmap) all_data2 <- cbind(amino,sphingo,hexoses,phospha,lyso,all_data) matrix_data <- as.matrix(all_data2[, 3:73]) log(matrix_data) rownames(matrix_data) <- Heatmap_data$`Sample Identification` pheatmap( mat = matrix_data ) ``` – doc elfein Sep 09 '20 at 20:34
  • I changed the ```(all_data2[, 3:73]```from 15 to 73 because that was the maximum amount of columns in one of the sheets I hope this is correct. Is there any way I can do a seperation into the analyte classes, like in the example given which are basically the data from the different sheets ? and is there a way to enlarge it a bit ? – doc elfein Sep 09 '20 at 20:36
  • oh and I have another question, what does ```pheatmap()``` do to the NAs? imputate them automatically ? thats sth else I was not sure how to do – doc elfein Sep 09 '20 at 20:38
  • You can edit your original question with your final code/image. For annotations, maybe you can use `annotation_row`/`annotation_col`. The `NA`s are ignored (shown in grey); maybe you want to scale your data for the heatmap. To get a bigger version, you can use `pdf` and specify the size – starja Sep 09 '20 at 20:43
  • what do you mean with scaling it ? I will try to imputate the Na´s then! Thank you I will try it with ```pdf``` – doc elfein Sep 09 '20 at 20:50
  • I think there was an error in the one I commenteed the Sample Identification is missing the new one is not as pretty anymore .. https://i.imgur.com/rEZRzxq.jpg – doc elfein Sep 09 '20 at 21:15
  • This is why you should scale your data, so that the ranges are comparable – starja Sep 10 '20 at 05:38
  • Thanks yes I did! Here is how far I got up until now and what is left to do! Thanks so much !! https://stackoverflow.com/questions/63833198/improving-my-heatmap-in-r-with-pheatmap – doc elfein Sep 10 '20 at 16:09