dendrolenda package usage

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

##   gene  strain value
## 1  uhz strain1     0
## 2  ism strain1     1
## 3  gdr strain1     0
## 4  thb strain1     1
## 5  dgz strain1     1
## 6  cph strain1     1

Create dendrograms:

d1_dat <- create_dend(example_dat, "gene", "strain")
d2_dat <- create_dend(example_dat, "strain", "gene")
## [1] "You are beautiful!"

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

## [1] "Such a bedazzling heatmap!"

Create dendrogram plots:

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

d1 + d2

## [1] "Mm! This is just terrific!"

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

## [1] "You are great!"

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

## [1] "Huh! This is just delightful!"

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

## [1] "You are super!"

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

## [1] "AHA! You have done this calmly!"