knitr::opts_chunk$set(echo = TRUE) library(knitr) library(ggplot2) library(tidyr) library(dplyr) library(grid) library(gridExtra) library(ggdendro) library(dendrolenda) library(patchwork) library(praise) create_dend <- function(x, col_name1, col_name2) { pivot_wider(x, names_from = c(col_name2), values_from = "value") %>% as.data.frame() %>% { rownames(.) <- .[[col_name1]] . } %>% select(-1) %>% as.matrix() %>% dist() %>% hclust() %>% as.dendrogram() }
example_dat <- expand.grid(gene = sapply(1L:10, function(dummy) paste0(sample(letters, 3, replace = TRUE), collapse = "")), strain = paste0("strain", 1L:6)) %>% mutate(value = sample(0L:1, size = 60, replace = TRUE)) head(example_dat)
d1_dat <- create_dend(example_dat, "gene", "strain") d2_dat <- create_dend(example_dat, "strain", "gene")
praise()
hm_dat <- mutate(example_dat, gene = factor(gene), gene = factor(gene, levels = levels(gene)[order.dendrogram(d1_dat)]), strain = factor(strain), strain = factor(strain, levels = levels(strain)[order.dendrogram(d2_dat)])) hm <- ggplot(hm_dat, aes(x = gene, y = strain, fill = factor(value))) + geom_tile(color = "black") + theme(legend.position = "right") hm
praise("Such a ${adjective} heatmap!")
d1 <- plot_dendrogram(d1_dat) d2 <- plot_dendrogram(d2_dat) + coord_flip() d1 + d2
praise("${Exclamation}! This is just ${adjective}!")
arrange_dendrogram(hm + theme_bw() + theme(legend.position = "bottom"), d1 + theme_void(), d2 + theme_void())
praise()
arrange_dendrogram(hm + theme_bw() + theme(legend.position = "none"), d1 + theme_void(), d2 + theme_void(), top_right = get_legend(hm))
praise("${Exclamation}! This is just ${adjective}!")
arrange_dendrogram(hm + theme_bw() + theme(legend.position = "bottom"), d1 + theme_void(), d2 + theme_void(), widths = c(0.9, 0.1), heights = c(0.1, 0.9))
praise()
label_colors <- c(rep("red", 3), rep("blue", 2), "green") arrange_dendrogram(hm + theme_bw() + theme(legend.position = "bottom", axis.text.y = element_text(color = label_colors)), d1 + theme_void(), d2 + theme_void())
praise("${EXCLAMATION}! You have done this ${adverb_manner}!")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.