Last updated: 2021-06-15

Checks: 7 0

Knit directory: muse/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20200712) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 83190de. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Unstaged changes:
    Modified:   muse.Rproj

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/pheatmap.Rmd) and HTML (docs/pheatmap.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 83190de Dave Tang 2021-06-15 Own column order
html d8e64e3 Dave Tang 2021-04-14 Build site.
Rmd 500529b Dave Tang 2021-04-14 https://davetang.org/muse/2018/05/15/making-a-heatmap-in-r-with-the-pheatmap-package/#comment-8339
html c54efbf Dave Tang 2020-11-27 Build site.
Rmd 4da96c0 Dave Tang 2020-11-27 More clusters
html 2db13ea davetang 2020-11-10 Build site.
Rmd 268312f davetang 2020-11-10 wflow_publish(files = c(“analysis/index.Rmd”, “analysis/google_trends.Rmd”,
html 586b91f Dave Tang 2020-07-12 Build site.
Rmd b1d6edd Dave Tang 2020-07-12 pheatmap

Making a heatmap using the pheatmap package.

example_file <- "https://davetang.org/file/TagSeqExample.tab"
data <- read.delim(example_file, header = TRUE, row.names = "gene")
data_subset <- as.matrix(data[rowSums(data)>50000,])

Default heatmap using pheatmap.

pheatmap(data_subset)

cal_z_score <- function(x){
  (x - mean(x)) / sd(x)
}

data_subset_norm <- t(apply(data_subset, 1, cal_z_score))
pheatmap(data_subset_norm)

Using scale produces the same heatmap as using cal_z_score.

pheatmap(data_subset, scale = "row")

Two heatmaps.

one <- pheatmap(data_subset, silent = TRUE)
two <- pheatmap(data_subset, silent = TRUE)

grid.arrange(grobs = list(one[[4]], two[[4]]))

Reproduce the gene dendrogram.

par(mar = c(3.1, 2.1, 1.1, 5.1))

my_hclust_gene <- hclust(dist(data_subset), method = "complete")
my_hclust_gene$height
 [1]  2502.208  3771.244  4252.402  4366.211  4700.444  5069.851  5208.367
 [8]  6439.545  6474.863  6938.482  7983.369  8141.632  9198.185  9849.175
[15] 10818.256 10868.066 11127.621 11168.654 12699.557 12871.187 13511.763
[22] 13549.622 14483.876 14856.478 14860.904 15033.046 16304.877 16574.315
[29] 16935.384 17713.534 18798.131 18904.899 20250.185 22302.634 22512.593
[36] 24345.199 29826.722 30846.374 31530.137 31849.145 40048.202 43714.148
[43] 47029.264 48908.962 56038.953 67891.667 74124.247 95015.400
as.dendrogram(my_hclust_gene) %>%
  plot(horiz = TRUE)

Obtaining the gene IDs as per the order of the dendrogram (from top to bottom).

rev(row.names(data_subset)[my_hclust_gene$order])
 [1] "Gene_08819" "Gene_08743" "Gene_12940" "Gene_13540" "Gene_12804"
 [6] "Gene_11672" "Gene_16632" "Gene_17743" "Gene_07390" "Gene_16114"
[11] "Gene_00562" "Gene_14672" "Gene_08694" "Gene_14450" "Gene_09238"
[16] "Gene_08042" "Gene_03194" "Gene_02420" "Gene_11002" "Gene_05960"
[21] "Gene_03450" "Gene_02800" "Gene_09969" "Gene_07404" "Gene_08576"
[26] "Gene_09610" "Gene_03852" "Gene_12318" "Gene_04164" "Gene_15334"
[31] "Gene_09952" "Gene_10924" "Gene_12834" "Gene_03861" "Gene_13444"
[36] "Gene_06899" "Gene_17849" "Gene_07132" "Gene_05761" "Gene_02296"
[41] "Gene_09505" "Gene_12576" "Gene_17992" "Gene_15286" "Gene_17865"
[46] "Gene_10648" "Gene_05004" "Gene_14928" "Gene_02115"

Reproduce the sample dendrogram.

my_hclust_sample <- hclust(dist(t(data_subset)), method = "complete")

as.dendrogram(my_hclust_sample) %>%
  plot()

Add annotations.

my_gene_col <- cutree(tree = as.dendrogram(my_hclust_gene), k = 2)
my_gene_col <- data.frame(cluster = ifelse(test = my_gene_col == 1, yes = "cluster 1", no = "cluster 2"))

my_sample_col <- data.frame(sample = rep(c("tumour", "normal"), c(4,2)))
row.names(my_sample_col) <- colnames(data_subset)

set.seed(1984)
my_random <- as.factor(sample(x = 1:2, size = nrow(my_gene_col), replace = TRUE))
my_gene_col$random <- my_random

pheatmap(data_subset, annotation_row = my_gene_col, annotation_col = my_sample_col)

Define your own column order by modifying data_subset and setting cluster_cols to FALSE.

my_col_order <- c("N2", "T1a", "N1", "T1b", "T2", "T3")

pheatmap(data_subset[, my_col_order],
         annotation_col = my_sample_col,
         cluster_cols = FALSE
         )

More clusters.

my_gene_col <- cutree(tree = as.dendrogram(my_hclust_gene), k = 6)
my_gene_col <- data.frame(cluster = paste0("cluster ", my_gene_col), row.names = names(my_gene_col))

my_sample_col <- data.frame(sample = rep(c("tumour", "normal"), c(4,2)))
row.names(my_sample_col) <- colnames(data_subset)

set.seed(1984)
my_random <- as.factor(sample(x = 1:2, size = nrow(my_gene_col), replace = TRUE))
my_gene_col$random <- my_random

pheatmap(data_subset, annotation_row = my_gene_col, annotation_col = my_sample_col)

Change annotation colours and ordering.

my_gene_col <- cutree(tree = as.dendrogram(my_hclust_gene), k = 2)
my_gene_col <- data.frame(cluster = ifelse(test = my_gene_col == 1, yes = "cluster1", no = "cluster2"))

my_sample_col <- data.frame(sample = rep(c("tumour", "normal"), c(4,2)))
row.names(my_sample_col) <- colnames(data_subset)

# change order
my_sample_col$sample <- factor(my_sample_col$sample, levels = c("normal", "tumour"))

set.seed(1984)
my_random <- as.factor(sample(x = c("random1", "random2"), size = nrow(my_gene_col), replace = TRUE))
my_gene_col$random <- my_random

my_colour = list(
    sample = c(normal = "#5977ff", tumour = "#f74747"),
    random = c(random1 = "#82ed82", random2 = "#9e82ed"),
    cluster = c(cluster1 = "#e89829", cluster2 = "#cc4ee0")
)

p <- pheatmap(data_subset,
              annotation_colors = my_colour,
              annotation_row = my_gene_col,
              annotation_col = my_sample_col,
              cellheight = 7,
              cellwidth = 18)

save_pheatmap_png <- function(x, filename, width=1200, height=1000, res = 150) {
  png(filename, width = width, height = height, res = res)
  grid::grid.newpage()
  grid::grid.draw(x$gtable)
  dev.off()
}

# not run
# save_pheatmap_png(p, "heatmap_colour.png")

Introduce breaks by cutting the dendrogram.

pheatmap(data_subset,
         annotation_row = my_gene_col,
         annotation_col = my_sample_col,
         cutree_rows = 2,
         cutree_cols = 2)

Dendrogram results from pheatmap().

par(mar = c(3.1, 2.1, 1.1, 5.1))

my_heatmap <- pheatmap(data_subset, silent = TRUE)
names(my_heatmap)
[1] "tree_row" "tree_col" "kmeans"   "gtable"  
my_heatmap$tree_row %>%
  as.dendrogram() %>%
  plot(horiz = TRUE)


sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    
system code page: 932

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gridExtra_2.3     dendextend_1.15.1 pheatmap_1.0.12   workflowr_1.6.2  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6         highr_0.9          pillar_1.6.1       compiler_4.0.5    
 [5] later_1.2.0        RColorBrewer_1.1-2 git2r_0.28.0       viridis_0.6.1     
 [9] tools_4.0.5        digest_0.6.27      viridisLite_0.4.0  evaluate_0.14     
[13] lifecycle_1.0.0    tibble_3.1.1       gtable_0.3.0       pkgconfig_2.0.3   
[17] rlang_0.4.11       DBI_1.1.1          yaml_2.2.1         xfun_0.22         
[21] dplyr_1.0.6        stringr_1.4.0      knitr_1.33         generics_0.1.0    
[25] fs_1.5.0           vctrs_0.3.8        tidyselect_1.1.1   rprojroot_2.0.2   
[29] grid_4.0.5         glue_1.4.2         R6_2.5.0           fansi_0.4.2       
[33] rmarkdown_2.8      farver_2.1.0       purrr_0.3.4        ggplot2_3.3.3     
[37] magrittr_2.0.1     whisker_0.4        scales_1.1.1       promises_1.2.0.1  
[41] ellipsis_0.3.2     htmltools_0.5.1.1  assertthat_0.2.1   colorspace_2.0-1  
[45] httpuv_1.6.1       utf8_1.2.1         stringi_1.5.3      munsell_0.5.0     
[49] crayon_1.4.1