It's easy to create such plots using ggalign, which offers an integrative, composable visualization framework for ggplot2..
library(ggalign)
set.seed(1)
time <- 1:50
data <- t(data.frame(
A = cumsum(rnorm(50, 0, 1)),
B = cumsum(rnorm(50, 0, 1)),
C = cumsum(rnorm(50, 0.2, 1)),
D = cumsum(rnorm(50, -0.2, 1))
))
stack_discreteh(data) -
# set default theme for all plots in the layout
scheme_theme(theme_minimal()) +
# add line plot
ggfree(size = 3) +
scheme_data(function(d) {
d$time <- time[d$.column_index]
d$.panel <- factor(d$.panel)
names(d$.panel)
d
}) +
geom_line(aes(time, value)) +
geom_hline(yintercept = 0, linetype = "dashed") +
facet_grid(rows = vars(.panel)) +
theme(strip.text = element_blank(), strip.background = element_blank()) +
# add the dendrogram
align_dendro(aes(color = branch), k = 4, plot_cut_height = FALSE) +
scale_color_brewer(palette = "Dark2", guide = "none") +
theme(axis.text.y = element_text())
More examples: