1

I am trying to generate a proper dendrogram in R with ggplot2 (more specifically ggdendro::ggdendrogram) because I would like to share it over plotly with my collaborators and currently I do not think it is possible with the api to generate a dendrogram with plotly.

I am mainly having troubles with the "axis" labeling when I rotate the dendrogram:

require(ggplot2)
require(ggdendro)
hc<-hclust(dist(iris[,1:4]),method="ward.D2")
ggd<-ggdendrogram(hc,rotate=TRUE)
ggd+scale_y_reverse()

This results in the following plot, where the tick labels are on the wrong side: Clustering with tick labels on the wrong side

I would like to obtain a similar result as from R base graphics when executing plot(as.dendrogram(hc),horiz=TRUE) Correct labels base graph

As a bonus, I would like to be able to color the leaf (tick) labels separately as well as draw rect.hclust-like boxes, but this is not strictly necessary. I do know my way a little bit around ggplot2 syntax so no need to explain every possible solution in great length.

If relevant please find my sessionInfo() below, I am using R through Rstudio server on an Ubuntu 12.04 LTS server:

sessionInfo()
 R version 3.1.0 (2014-04-10)
 Platform: x86_64-pc-linux-gnu (64-bit)

 locale:
 [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C         LC_TIME=C                LC_COLLATE=C         LC_MONETARY=C        LC_MESSAGES=C        LC_PAPER=C          
 [8] LC_NAME=C            LC_ADDRESS=C         LC_TELEPHONE=C       LC_MEASUREMENT=C     LC_IDENTIFICATION=C 

 attached base packages:
 [1] stats     graphics  grDevices utils     datasets  methods   base     

 other attached packages:
 [1] ggdendro_0.1-14       cluster_1.15.2        plotly_0.4            RJSONIO_1.2-0.2       RCurl_1.95-4.1        bitops_1.0-6          devtools_1.5         
 [8] extrafont_0.16        RColorBrewer_1.0-5    scales_0.2.4          vegan_2.0-10          lattice_0.20-29       permute_0.8-3         splitstackshape_1.2.0
 [15] data.table_1.9.2      BurStMisc_1.00        reshape2_1.4          xlsx_0.5.5            xlsxjars_0.6.0        rJava_0.9-6           ggplot2_0.9.3.1      
 [22] plyr_1.8.1           

 loaded via a namespace (and not attached):
 [1] MASS_7.3-32      Rcpp_0.11.1      Rttf2pt1_1.3     colorspace_1.2-4 digest_0.6.4     evaluate_0.5.5   extrafontdb_1.0  formatR_0.10     grid_3.1.0      
 [10] gtable_0.1.2     httr_0.3         knitr_1.6        labeling_0.2     memoise_0.2.1    munsell_0.4.2    parallel_3.1.0   proto_0.3-10     stringr_0.6.2   
 [19] tools_3.1.0      whisker_0.3-2
FM Kerckhof
  • 1,270
  • 1
  • 14
  • 31
  • 3
    You have to add the text using `geom_text(...)` and turn off the axes. See [this example](http://stackoverflow.com/questions/21474388/colorize-clusters-in-dendogram-with-ggplot2/21486985#21486985) and [this example](http://stackoverflow.com/questions/24140339/tree-cut-and-rectangles-around-clusters-for-a-horizontal-dendrogram-in-r/24147534#24147534). The latter simulates `rect.hclust(...)`. – jlhoward Jul 08 '14 at 16:17
  • thanks, the odd thing is though that in this RPub it appears to work out of the box http://rpubs.com/gaston/dendrograms therefore I thought it was an issue but clearly it has been answered elsewhere. Can't believe I missed it. – FM Kerckhof Jul 09 '14 at 00:55

0 Answers0