Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT</strong></p> <p>From 8 August 2011 the <code>ggdendro</code> package is available on <a href="http://cran.r-project.org/web/packages/ggdendro/index.html" rel="noreferrer">CRAN</a> Note also that the dendrogram extraction function is now called <code>dendro_data</code> instead of <code>cluster_data</code></p> <hr> <p>Yes, it is. But for the time being you will have to jump through a few hoops:</p> <ol> <li>Install the <code>ggdendro</code> package (available from CRAN). This package will extract the cluster information from several types of cluster methods (including <code>Hclust</code> and <code>dendrogram</code>) with the express purpose of plotting in <code>ggplot</code>.</li> <li>Use grid graphics to create viewports and align three different plots.</li> </ol> <p><img src="https://i.stack.imgur.com/tJtAf.png" alt="enter image description here"></p> <p>The code:</p> <p>First load the libraries and set up the data for ggplot:</p> <pre><code>library(ggplot2) library(reshape2) library(ggdendro) data(mtcars) x &lt;- as.matrix(scale(mtcars)) dd.col &lt;- as.dendrogram(hclust(dist(x))) col.ord &lt;- order.dendrogram(dd.col) dd.row &lt;- as.dendrogram(hclust(dist(t(x)))) row.ord &lt;- order.dendrogram(dd.row) xx &lt;- scale(mtcars)[col.ord, row.ord] xx_names &lt;- attr(xx, "dimnames") df &lt;- as.data.frame(xx) colnames(df) &lt;- xx_names[[2]] df$car &lt;- xx_names[[1]] df$car &lt;- with(df, factor(car, levels=car, ordered=TRUE)) mdf &lt;- melt(df, id.vars="car") </code></pre> <p>Extract dendrogram data and create the plots</p> <pre><code>ddata_x &lt;- dendro_data(dd.row) ddata_y &lt;- dendro_data(dd.col) ### Set up a blank theme theme_none &lt;- theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.title.x = element_text(colour=NA), axis.title.y = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), axis.line = element_blank() #axis.ticks.length = element_blank() ) ### Create plot components ### # Heatmap p1 &lt;- ggplot(mdf, aes(x=variable, y=car)) + geom_tile(aes(fill=value)) + scale_fill_gradient2() # Dendrogram 1 p2 &lt;- ggplot(segment(ddata_x)) + geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) + theme_none + theme(axis.title.x=element_blank()) # Dendrogram 2 p3 &lt;- ggplot(segment(ddata_y)) + geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) + coord_flip() + theme_none </code></pre> <p>Use grid graphics and some manual alignment to position the three plots on the page</p> <pre><code>### Draw graphic ### grid.newpage() print(p1, vp=viewport(0.8, 0.8, x=0.4, y=0.4)) print(p2, vp=viewport(0.52, 0.2, x=0.45, y=0.9)) print(p3, vp=viewport(0.2, 0.8, x=0.9, y=0.4)) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload