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()
}

dendrolenda package usage

Let's plot heatmap with aligned dendrograms using the following randomly generated data:

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)

Create dendrograms:

d1_dat <- create_dend(example_dat, "gene", "strain")
d2_dat <- create_dend(example_dat, "strain", "gene")
praise()

Create heatmap:

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

Create dendrogram plots:

d1 <- plot_dendrogram(d1_dat)
d2 <- plot_dendrogram(d2_dat) + coord_flip()

d1 + d2
praise("${Exclamation}! This is just ${adjective}!")

Arrange all three plots with legend positioned at the bottom:

arrange_dendrogram(hm + theme_bw() + theme(legend.position = "bottom"),
                   d1 + theme_void(),
                   d2 + theme_void())
praise()

Arrange all three plots with legend positioned in the top-right corner:

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 all three plots with adjusted heights and widths:

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

Arrange all three plots with colored labels on right dendrogram:

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


michbur/dendrolenda documentation built on Aug. 10, 2020, 4:55 a.m.