Is this possible to reproduce this lattice plot with ggplot2?
library(latticeExtra)
data(mtcars)
x <- t(as.matrix(scale(mtcars)))
dd.row <- as.dendrogram(hclust(dist(x)))
row.ord <- order.dendrogram(dd.row)
dd.col <- as.dendrogram(hclust(dist(t(x))))
col.ord <- order.dendrogram(dd.col)
library(lattice)
levelplot(x[row.ord, col.ord],
aspect = "fill",
scales = list(x = list(rot = 90)),
colorkey = list(space = "left"),
legend =
list(right =
list(fun = dendrogramGrob,
args =
list(x = dd.col, ord = col.ord,
side = "right",
size = 10)),
top =
list(fun = dendrogramGrob,
args =
list(x = dd.row,
side = "top",
size = 10))))
As Ben says, everything is possible. Some work to support dendrograms has been done. Andrie de Vries has made a fortify method of tree objects. However, the resulting graphic is not pretty as you can see.
The tile would be easy to do. For the dendrogram I would inspect
plot.dendrogram
(usinggetAnywhere
) to see how the coordinates for the segments are calculated. Extract those coordinates and use geom_segment to plot the dendrogram. Then use viewports to plot the tiles and the dendrogram together. Sorry I can't give a example, it's a lot of work and it's too late.I hope this helps
Cheers
These links provide a solution for heatmaps with dendrograms in ggplot2:
https://gist.github.com/chr1swallace/4672065
https://github.com/chr1swallace/random-functions/blob/master/R/ggplot-heatmap.R
and also this one:
Align ggplot2 plots vertically
EDIT
From 8 August 2011 the
ggdendro
package is available on CRAN Note also that the dendrogram extraction function is now calleddendro_data
instead ofcluster_data
Yes, it is. But for the time being you will have to jump through a few hoops:
ggdendro
package (available from CRAN). This package will extract the cluster information from several types of cluster methods (includingHclust
anddendrogram
) with the express purpose of plotting inggplot
.The code:
First load the libraries and set up the data for ggplot:
Extract dendrogram data and create the plots
Use grid graphics and some manual alignment to position the three plots on the page
Doubtful. I do not see any functions in the Index for ggplot2 that would suggest support for dendrograms, and when this blogger put together a set of translations of the illustrations in Sarkar's Lattice book, he was unable to get a ggplot dendrogram legend:
http://learnr.wordpress.com/2009/08/10/ggplot2-version-of-figures-in-lattice-multivariate-data-visualization-with-r-part-9/