text: user2531: I have made a heatmap using RPKM values from a RNA-Seq dataset using the pheatmap() function in R. I have log2-transformed the data before performing Z-score standardisation of the data. I have also clustered the rows and columns of the heatmap. I have the following code:
  heatmap_data_log2 %>% pheatmap(color = colorRampPalette(c("blue2","white","red"))(100), scale = "column", cluster_cols = T, clustering_method = "ward.D2", angle_col = 45, fontsize_row = 7.5, fontsize_col = 8, border_color = NA, cutree_rows = 4)
  
  I have seen in a video tutorial that the pheatmap() function does the Z-score scaling before doing the clustering of the rows and columns. However, in this online article, it says that "The Z-scores are computed after the clustering, so that it only affects the graphical aesthetics and the color visualization is improved".
  As these two sources are giving contradictory information, I was wondering which is better. Should you do Z-score scaling of the gene expression values before doing the clustering or after? Any advice is appreciated.
  
  user776: Looking at the source for pheatmap, there is a function called scale_mat that is used to preprocess and normalize the input matrix, depending on the value of scale, which specifies one of either none, row, or column normalization options. Separate and similar functions are used for color value scaling downstream.
  I have not watched the video or read the linked article, so I'm not sure this is contradictory, but it may be just small confusion about how normalization is applied (and to what).
  Normalization is usually done before clustering to focus cluster construction on signal rows or columns that are not some multiple or scale factor of another — and in referring back to the source code, this is what pheatmap does, as well.
  
original_text: 2531: <p>I have made a heatmap using RPKM values from a RNA-Seq dataset using the pheatmap() function in R. I have log2-transformed the data before performing Z-score standardisation of the data. I have also clustered the rows and columns of the heatmap. I have the following code:</p>
  <pre><code>heatmap_data_log2 %&gt;% pheatmap(color = colorRampPalette(c(&quot;blue2&quot;,&quot;white&quot;,&quot;red&quot;))(100), scale = &quot;column&quot;, cluster_cols = T, clustering_method = &quot;ward.D2&quot;, angle_col = 45, fontsize_row = 7.5, fontsize_col = 8, border_color = NA, cutree_rows = 4)
  </code></pre>
  <p>I have seen in a <a href="https://www.youtube.com/watch?v=pTeTH9bz-_s&amp;list=LL&amp;index=14&amp;t=714s&amp;ab_channel=BabrahamBioinf" rel="nofollow noreferrer">video tutorial</a> that the pheatmap() function does the Z-score scaling before doing the clustering of the rows and columns. However, in this <a href="https://hbctraining.github.io/GCC-BOSC-2018/lessons/data_visualization.html" rel="nofollow noreferrer">online article</a>, it says that <em>&quot;The Z-scores are computed after the clustering, so that it only affects the graphical aesthetics and the color visualization is improved&quot;</em>.</p>
  <p>As these two sources are giving contradictory information, I was wondering which is better. Should you do Z-score scaling of the gene expression values before doing the clustering or after? Any advice is appreciated.</p>
   776: <p>Looking at the <a href="https://rdrr.io/cran/pheatmap/src/R/pheatmap.r" rel="nofollow noreferrer">source</a> for <code>pheatmap</code>, there is a function called <code>scale_mat</code> that is used to preprocess and normalize the input matrix, depending on the value of <code>scale</code>, which specifies one of either none, row, or column normalization options. Separate and similar functions are used for color value scaling downstream.</p>
  <p>I have not watched the video or read the linked article, so I'm not sure this is contradictory, but it may be just small confusion about how normalization is applied (and to what).</p>
  <p>Normalization is usually done before clustering to focus cluster construction on signal rows or columns that are not some multiple or scale factor of another — and in referring back to the source code, this is what <code>pheatmap</code> does, as well.</p>
  