# Hierarchically cluster USArrests
yclus <- hclust(dist(USArrests), "ave")
xclus <- hclust(dist(t(USArrests)), "ave")
# Melting USArrests
df <- data.frame(
State = rownames(USArrests)[row(USArrests)],
variable = colnames(USArrests)[col(USArrests)],
value = unname(do.call(c, USArrests))
)
# Supply the clustering to the scales
ggplot(df, aes(variable, State, fill = value)) +
geom_raster() +
scale_y_dendrogram(hclust = yclus) +
scale_x_dendrogram(hclust = xclus)
Run the code above in your browser using DataLab