# Hierarchically cluster data, separately for rows and columns
car_clust <- hclust(dist(scale(mtcars)), "ave")
var_clust <- hclust(dist(scale(t(mtcars))), "ave")
long_mtcars <- data.frame(
car = rownames(mtcars)[row(mtcars)],
var = colnames(mtcars)[col(mtcars)],
value = as.vector(scale(mtcars))
)
# A standard heatmap adorned with dendrograms
p <- ggplot(long_mtcars, aes(var, car, fill = value)) +
geom_tile() +
scale_x_dendro(var_clust) +
scale_y_dendro(car_clust)
p
# Styling the dendrograms
p +
guides(
y = guide_axis_dendro(key_dendro(type = "triangle")),
x = guide_axis_dendro(space = rel(5))
) +
theme(
axis.text.y.left = element_text(margin = margin(r = 3, l = 3)),
axis.ticks.y = element_line("red"),
axis.ticks.x = element_line(linetype = "dotted")
)
# In polar coordinates, plus some formatting
p +
coord_radial(
theta = "y", inner.radius = 0.5,
start = 0.25 * pi, end = 1.75 * pi
) +
guides(
theta = primitive_labels(angle = 90),
theta.sec = primitive_segments("dendro", vanish = TRUE),
r = guide_axis_dendro(angle = 0)
)
Run the code above in your browser using DataLab