├── .Rbuildignore ├── .gitignore ├── DESCRIPTION ├── Examples.R ├── Makefile ├── NAMESPACE ├── NEWS.md ├── R ├── autoplot.R ├── geom-line-cutree.R ├── geom-rect-subtree.R ├── ggtreeDendro-package.R ├── scale-color-subtree.R ├── wgcna.R └── zzz.R ├── README.md ├── inst └── CITATION ├── man ├── autoplot.Rd ├── geom-line-cutree.Rd ├── geom-rect-subtree.Rd ├── ggtreeDendro-package.Rd └── scale-color-subtree.Rd ├── tests ├── testthat.R └── testthat │ └── test-autoplot.R └── vignettes └── ggtreeDendro.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | Makefile 2 | ^Makefile$ 3 | ^README\.md$ 4 | ^README\.Rmd$ 5 | ^CONDUCT\.md$ 6 | ^\.Rproj\.user$ 7 | ^\.Rproj$ 8 | ^R/wgcna\.R$ 9 | Examples.R 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | gh-pages 2 | .DS_Store 3 | .RData 4 | .Rhistory 5 | .Rproj.user 6 | vignettes/*.html 7 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: ggtreeDendro 2 | Title: Drawing 'dendrogram' using 'ggtree' 3 | Version: 1.11.0 4 | Authors@R: c( 5 | person("Guangchuang", "Yu", email = "guangchuangyu@gmail.com", role = c("aut", "cre", "cph"), 6 | comment = c(ORCID = "0000-0002-6485-8781")), 7 | person("Shuangbin", "Xu", email = "xshuangbin@163.com", role = "ctb", 8 | comment = c(ORCID="0000-0003-3513-5362")), 9 | person("Chuanjie", "Zhang", email = "959264868@qq.com", role = "ctb") 10 | ) 11 | Description: Offers a set of 'autoplot' methods to visualize tree-like structures (e.g., hierarchical clustering and classification/regression trees) using 'ggtree'. You can adjust graphical parameters using grammar of graphic syntax and integrate external data to the tree. 12 | Depends: 13 | ggtree (>= 3.5.3) 14 | Imports: 15 | ggplot2, 16 | stats, 17 | tidytree, 18 | utils 19 | Suggests: 20 | aplot, 21 | cluster, 22 | knitr, 23 | MASS, 24 | mdendro, 25 | prettydoc, 26 | pvclust, 27 | rmarkdown, 28 | testthat (>= 3.0.0), 29 | treeio, 30 | yulab.utils 31 | License: Artistic-2.0 32 | VignetteBuilder: knitr 33 | ByteCompile: true 34 | Encoding: UTF-8 35 | biocViews: Clustering, Classification, DecisionTree, Phylogenetics, Visualization 36 | Roxygen: list(markdown = TRUE) 37 | RoxygenNote: 7.3.0 38 | Config/testthat/edition: 3 39 | -------------------------------------------------------------------------------- /Examples.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## `hclust` and `dendrogram` objects 4 | 5 | # genieclust::gclust() also output `hclust` object 6 | # amap::hcluster() also output `hclust` object 7 | # FactoClass::ward.cluster() also output `hclust` object 8 | 9 | 10 | d <- dist(USArrests) 11 | 12 | hc <- hclust(d, "ave") 13 | den <- as.dendrogram(hc) 14 | 15 | p1 <- autoplot(hc) + geom_tiplab() 16 | p2 <- autoplot(den) + geom_rect_subtree(4) 17 | plot_list(p1, p2, ncol=2) 18 | 19 | 20 | 21 | ## `linkage` object 22 | 23 | 24 | library("mdendro") 25 | lnk <- linkage(d, digits = 1, method = "complete") 26 | autoplot(lnk, layout = 'circular') + geom_tiplab() + 27 | scale_color_subtree(4) + theme_tree() 28 | 29 | 30 | ## `agnes`, `diana` and `twins` objects 31 | 32 | library(cluster) 33 | x1 <- agnes(mtcars) 34 | x2 <- diana(mtcars) 35 | 36 | p1 <- autoplot(x1) + geom_tiplab() 37 | p2 <- autoplot(x2) + geom_tiplab() 38 | plot_list(p1, p2, ncol=2) 39 | 40 | 41 | 42 | ## `pvclust` object 43 | 44 | 45 | library(pvclust) 46 | data(Boston, package = "MASS") 47 | 48 | set.seed(123) 49 | result <- pvclust(Boston, method.dist="cor", method.hclust="average", nboot=1000, parallel=TRUE) 50 | autoplot(result, label_edge=TRUE, pvrect = TRUE) + geom_tiplab() 51 | 52 | 53 | ## `bclust` object 54 | 55 | ### S4 56 | library(flexclust) 57 | data(iris) 58 | bc1 <- bclust(iris[,1:4], 3, base.k=5) 59 | autoplot(bc1) 60 | 61 | ### S3 62 | library(e1071) 63 | bc2 <- bclust(iris[,1:4], 3, base.centers=5) 64 | autoplot(bc2) 65 | 66 | ## `protoclust` object 67 | 68 | library(protoclust) 69 | data(Boston, package = "MASS") 70 | d <- dist(Boston) 71 | pc <- protoclust(d) 72 | plotwithprototypes(pc) 73 | autoplot(pc) 74 | 75 | 76 | ## `hdbscan` object 77 | 78 | library(dbscan) 79 | res <- hdbscan(moons, minPts = 5) 80 | autoplot(res) 81 | 82 | 83 | ## `hkmeans` object 84 | 85 | library(factoextra) 86 | # Load data 87 | data(USArrests) 88 | # Scale the data 89 | df <- scale(USArrests) 90 | 91 | # Compute hierarchical k-means clustering 92 | res.hk <-hkmeans(df, 4) 93 | 94 | hkmeans_tree(res.hk, cex = 0.6) 95 | fviz_dend(res.hk, cex = 0.6) 96 | 97 | library(ggtreeDendro) 98 | autoplot(res.hk) + geom_rect_subtree(4, color=c("red", "blue", "green", "purple")) 99 | autoplot(res.hk$hclust) + scale_color_subtree(4) 100 | 101 | 102 | # ClusterExperiment 103 | 104 | library(clusterExperiment) 105 | data(simData) 106 | 107 | #create a clustering, for 8 clusters (truth was 3) 108 | cl <-clusterSingle(simData, subsample=FALSE, 109 | sequential=FALSE, 110 | mainClusterArgs=list(clusterFunction="pam", clusterArgs=list(k=8))) 111 | 112 | #create dendrogram of clusters and then 113 | # merge clusters based ondendrogram: 114 | cl <- makeDendrogram(cl) 115 | cl <- mergeClusters(cl,mergeMethod="adjP",DEMethod="limma", 116 | cutoff=0.1,plot=FALSE) 117 | 118 | plotDendrogram(cl,leafType="samples",whichClusters="all",plotType="colorblock") 119 | 120 | library(ggtreeDendro) 121 | autoplot(cl) 122 | 123 | ## dendro 124 | 125 | library(ggplot2) 126 | library(ggtree) 127 | library(ggdendro) 128 | library(ggtreeDendro) 129 | 130 | hc <- hclust(dist(USArrests), "ave") 131 | x <- dendro_data(hc) 132 | 133 | autoplot(x) + geom_tiplab() 134 | 135 | 136 | ## HGC 137 | 138 | library(HGC) 139 | data(Pollen) 140 | Pollen.PCs <- Pollen[["PCs"]] 141 | Pollen.Label.Tissue <- Pollen[["Tissue"]] 142 | Pollen.Label.CellLine <- Pollen[["CellLine"]] 143 | 144 | Pollen.SNN <- SNN.Construction(Pollen.PCs) 145 | rownames(Pollen.SNN) <- rownames(Pollen.PCs) 146 | Pollen.ClusteringTree <- HGC.dendrogram(G = Pollen.SNN) 147 | Pollen.labels <- data.frame(Tissue = Pollen.Label.Tissue, 148 | CellLine = Pollen.Label.CellLine) 149 | HGC.PlotDendrogram(tree = Pollen.ClusteringTree, 150 | k = 5, plot.label = TRUE, 151 | labels = Pollen.labels) 152 | 153 | 154 | ## 155 | 156 | library(treeio) 157 | library(ggtree) 158 | library(ggtreeDendro) 159 | 160 | x <- as.phylo(as.dendrogram(Pollen.ClusteringTree)) 161 | 162 | p1 <- ggtree(x, layout = 'dendrogram', ladderize = FALSE) 163 | 164 | p <- ggtree(x, layout = 'dendrogram', ladderize = FALSE, branch.length = 'none') 165 | 166 | g <- gheatmap(p, Pollen.labels[,1, drop=FALSE], width=.05, colnames_angle = 0, colnames_offset_y = -20, color=NA) 167 | g <- g + ggnewscale::new_scale_fill() 168 | g <- gheatmap(g, Pollen.labels[,2, drop=FALSE], width=.05, colnames_angle = 0, colnames_offset_y = -20, color=NA, offset = 1) 169 | 170 | g + scale_color_subtree(5) 171 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PKGNAME := $(shell sed -n "s/Package: *\([^ ]*\)/\1/p" DESCRIPTION) 2 | PKGVERS := $(shell sed -n "s/Version: *\([^ ]*\)/\1/p" DESCRIPTION) 3 | PKGSRC := $(shell basename `pwd`) 4 | BIOCVER := RELEASE_3_21 5 | 6 | all: rd check clean 7 | 8 | alldocs: rd readme 9 | 10 | rd: 11 | Rscript -e 'library(methods); devtools::document()' 12 | 13 | readme: 14 | Rscript -e 'rmarkdown::render("README.Rmd")' 15 | 16 | codemetar: 17 | Rscript -e 'codemetar::write_codemeta()' 18 | 19 | sticker: 20 | Rscript -e 'source("treeio_sticker.R")'; 21 | rm Rplots.pdf 22 | 23 | build: 24 | #cd ..;\ 25 | #R CMD build $(PKGSRC) 26 | Rscript -e 'devtools::build()' 27 | 28 | build2: 29 | cd ..;\ 30 | R CMD build --no-build-vignettes $(PKGSRC) 31 | 32 | install: 33 | cd ..;\ 34 | R CMD INSTALL $(PKGNAME)_$(PKGVERS).tar.gz 35 | 36 | check: rd 37 | #cd ..;\ 38 | #Rscript -e 'rcmdcheck::rcmdcheck("$(PKGNAME)_$(PKGVERS).tar.gz")' 39 | Rscript -e 'devtools::check()' 40 | 41 | check2: rd build 42 | cd ..;\ 43 | R CMD check $(PKGNAME)_$(PKGVERS).tar.gz 44 | 45 | check3: rd build2 46 | cd ..;\ 47 | R CMD check --ignore-vignettes $(PKGNAME)_$(PKGVERS).tar.gz 48 | 49 | bignore: 50 | Rscript -e 'usethis::use_build_ignore(c("Makefile", "README.md", "README.Rmd", "CONDUCT.md", ".Rproj.user", ".Rproj"))' 51 | 52 | gignore: 53 | Rscript -e 'usethis::use_git_ignore(c(".DS_Store", ".RData", ".Rhistory", ".Rproj.user", "vignettes/*.html"))' 54 | 55 | bioccheck: 56 | cd ..;\ 57 | Rscript -e 'BiocCheck::BiocCheck("$(PKGNAME)_$(PKGVERS).tar.gz")' 58 | 59 | gpcheck: 60 | Rscript -e 'goodpractice::gp()' 61 | 62 | clean: 63 | cd ..;\ 64 | $(RM) -r $(PKGNAME).Rcheck/ 65 | 66 | vignettes: 67 | Rscript -e 'rmarkdown::render("gh-pages/index.Rmd")' 68 | 69 | publish: 70 | cd gh-pages;\ 71 | git add . ;\ 72 | git commit -m 'update docs';\ 73 | git push -u origin gh-pages 74 | 75 | gitmaintain: 76 | git gc --auto;\ 77 | git prune -v;\ 78 | git fsck --full 79 | 80 | rmrelease: 81 | git branch -D $(BIOCVER) 82 | 83 | release: 84 | git checkout $(BIOCVER);\ 85 | git fetch --all 86 | 87 | update: 88 | git fetch --all;\ 89 | git checkout devel;\ 90 | git merge upstream/devel;\ 91 | git merge origin/devel 92 | 93 | push: update 94 | git push upstream devel;\ 95 | git push origin devel 96 | 97 | biocinit: 98 | git remote add upstream git@git.bioconductor.org:packages/$(PKGNAME).git;\ 99 | git fetch --all 100 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(autoplot,ClusterExperiment) 4 | S3method(autoplot,agnes) 5 | S3method(autoplot,bclust) 6 | S3method(autoplot,dendro) 7 | S3method(autoplot,dendrogram) 8 | S3method(autoplot,diana) 9 | S3method(autoplot,genoMatriXeR) 10 | S3method(autoplot,hclust) 11 | S3method(autoplot,hdbscan) 12 | S3method(autoplot,hkmeans) 13 | S3method(autoplot,linkage) 14 | S3method(autoplot,multiLocalZScore) 15 | S3method(autoplot,pvclust) 16 | S3method(autoplot,twins) 17 | S3method(ggplot_add,color_subtree) 18 | S3method(ggplot_add,line_cutree) 19 | S3method(ggplot_add,rect_subtree) 20 | export(autoplot) 21 | export(geom_line_cutree) 22 | export(geom_rect_subtree) 23 | export(scale_color_subtree) 24 | export(scale_colour_subtree) 25 | importFrom(ggplot2,aes_) 26 | importFrom(ggplot2,autoplot) 27 | importFrom(ggplot2,geom_vline) 28 | importFrom(ggplot2,ggplot_add) 29 | importFrom(ggplot2,margin) 30 | importFrom(ggplot2,scale_color_manual) 31 | importFrom(ggplot2,theme) 32 | importFrom(ggtree,geom_hilight) 33 | importFrom(ggtree,geom_nodelab) 34 | importFrom(ggtree,geom_tiplab) 35 | importFrom(ggtree,ggtree) 36 | importFrom(ggtree,groupOTU) 37 | importFrom(ggtree,theme_dendrogram) 38 | importFrom(ggtree,theme_tree2) 39 | importFrom(stats,as.hclust) 40 | importFrom(stats,cutree) 41 | importFrom(tidytree,MRCA) 42 | importFrom(tidytree,as.phylo) 43 | importFrom(tidytree,as.treedata) 44 | importFrom(tidytree,as_tibble) 45 | importFrom(tidytree,offspring) 46 | importFrom(tidytree,rootnode) 47 | importFrom(utils,modifyList) 48 | importFrom(utils,packageDescription) 49 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # ggtreeDendro 1.10.0 2 | 3 | + Bioconductor RELEASE_3_21 (2025-04-17, Thu) 4 | 5 | # ggtreeDendro 1.8.0 6 | 7 | + Bioconductor RELEASE_3_20 (2024-10-30, Wed) 8 | 9 | # ggtreeDendro 1.6.0 10 | 11 | + Bioconductor RELEASE_3_19 (2024-05-15, Wed) 12 | 13 | # ggtreeDendro 1.4.0 14 | 15 | + Bioconductor RELEASE_3_18 (2023-10-25, Wed) 16 | 17 | # ggtreeDendro 1.3.2 18 | 19 | + update according to the change of tidytree (2023-08-18, Fri) 20 | 21 | # ggtreeDendro 1.3.1 22 | 23 | + `autoplot` method for 'dendro' object (`ggdendro::dendro_data()` output) (2023-03-02, Thu) 24 | - see also 25 | 26 | # ggtreeDendro 1.2.0 27 | 28 | + Bioconductor RELEASE_3_17 (2023-05-03, Wed) 29 | 30 | # ggtreeDendro 1.1.3 31 | 32 | + `autoplot` method for 'genoMatriXeR' and 'multiLocalZScore' objects (both defined in the 'regioneReloaded' package) (2023-02-11, Sat) 33 | + `autoplot` method for 'ClusterExperiment' object (2022-11-28, Mon) 34 | 35 | # ggtreeDendro 1.1.2 36 | 37 | + `plot_wgcna()` for visualizing WGCNA dendrogram (2022-11-06, Sun) 38 | + supports `hkmeans` object defined in the `factoextra` package (2022-11-06, Sun) 39 | + set default `options` to align `geom_rect_subtree()` and adjust width extension. 40 | 41 | # ggtreeDendro 1.1.1 42 | 43 | + `geom_rect_subtree` layer to hilight subtrees (2022-11-03, Thu) 44 | + supports `bclust` object (S3) output by `e1071::bclust()` 45 | + supports `hdbscan` object 46 | 47 | # ggtreeDendro 1.0.0 48 | 49 | + Bioconductor RELEASE_3_16 (2022-11-02, Wed) 50 | 51 | ## ggtreeDendro 0.99.11 52 | 53 | + `autoplot` method for `bclust` object (S4) (2022-10-08, Sat) 54 | + `scale_color_subtree` from `ggtree` package (2022-09-23, Fri) 55 | + `geom_line_cutree` to add a line at the position where the tree was cut by `cutree()` 56 | + solve issues requested by Bioconductor review (2022-09-21, Wed) 57 | - 58 | + add paramters `label_edge` and `alpha` in `autoplot.pvclust` (2022-08-18, Thu) 59 | + add examples of `pvclust` in vignettes (2022-08-17, Wed) 60 | + `autoplot()` method for `pvclust` object (2022-08-15, Mon) 61 | + fixed `BiocCheck()` (2022-08-12, Fri) 62 | + add `biocViews` in DESCRIPTION (2022-08-12, Fri) 63 | + `autoplot()` methods for `linkage`, `hclust`, `dendrogram`, `agnes`, `diana` and `twins` objects (2022-08-12, Fri) 64 | -------------------------------------------------------------------------------- /R/autoplot.R: -------------------------------------------------------------------------------- 1 | ##' @rdname autoplot 2 | ##' @importFrom ggplot2 autoplot 3 | ##' @export 4 | ggplot2::autoplot 5 | 6 | 7 | ## autoplot methods 8 | ## autoplot methods for hierarchical clustering results 9 | 10 | ##' @title autoplot 11 | ##' @rdname autoplot 12 | ##' @param object input object 13 | ##' @param layout layout for plotting the tree 14 | ##' @param ladderize logical whether ladderize the tree (default FALSE) 15 | ##' @param hang numeric The fraction of the tree plot height by which labels 16 | ##' should hang below the rest of the plot. A negative value will cause the 17 | ##' labels to hang down from 0. 18 | ##' @param label_edge logical whether display the label of edge (only for 19 | ##' pvclust object), default is FALSE. 20 | ##' @param pvrect logical whether display the clusters with relatively high/low 21 | ##' p-values, default is FALSE. (only for pvclust object) 22 | ##' @param alpha numeric the threshold value for p-values, default is 0.95 (only 23 | ##' for pvclust object). 24 | ##' @param ... additional paramters that passed to ggtree 25 | ##' @return ggtree object 26 | ##' @importFrom ggtree ggtree 27 | ##' @importFrom ggtree geom_tiplab 28 | ##' @importFrom ggtree theme_dendrogram 29 | ##' @importFrom ggtree theme_tree2 30 | ##' @importFrom ggplot2 theme 31 | ##' @importFrom ggplot2 margin 32 | ##' @method autoplot hclust 33 | ##' @export 34 | ##' @examples 35 | ##' d <- dist(USArrests) 36 | ##' hc <- hclust(d, "ave") 37 | ##' autoplot(hc) + geom_tiplab() 38 | autoplot.hclust <- function(object, layout = "dendrogram", ladderize = FALSE, hang = 0.1, ...) { 39 | p <- ggtree(object, ladderize = ladderize, layout=layout, hang = hang, ...) 40 | #geom_tiplab() + 41 | 42 | if (is.function(layout)) { 43 | thm <- NULL 44 | } else if (layout %in% c("fan", "circular", "inward_circular", "radial", 45 | "unrooted", "equal_angle", "daylight", "ape")) { 46 | thm <- NULL 47 | } else if (layout == "dendrogram") { 48 | thm <- theme_dendrogram(plot.margin=margin(t=6, r=6, b=80, l=6, unit='pt')) 49 | } else { 50 | thm <- theme_tree2(plot.margin=margin(t=6, r=80, b=6, l=6, unit='pt')) 51 | } 52 | 53 | p + thm 54 | } 55 | 56 | ##' @rdname autoplot 57 | ##' @method autoplot linkage 58 | ##' @export 59 | autoplot.linkage <- autoplot.hclust 60 | 61 | ##' @rdname autoplot 62 | ##' @method autoplot dendrogram 63 | ##' @export 64 | autoplot.dendrogram <- autoplot.hclust 65 | 66 | ##' @rdname autoplot 67 | ##' @method autoplot agnes 68 | ##' @export 69 | autoplot.agnes <- autoplot.hclust 70 | 71 | ##' @rdname autoplot 72 | ##' @method autoplot diana 73 | ##' @export 74 | autoplot.diana <- autoplot.hclust 75 | 76 | ##' @rdname autoplot 77 | ##' @method autoplot twins 78 | ##' @export 79 | autoplot.twins <- autoplot.hclust 80 | 81 | ##' @rdname autoplot 82 | ##' @method autoplot bclust 83 | ##' @export 84 | autoplot.bclust <- function(object, ...) { 85 | if (is.list(object)) { 86 | # S3 class defined in "e1071" 87 | hc <- object$hclust 88 | } else { 89 | # S4 class defined in "flexclust" 90 | hc <- object@hclust 91 | } 92 | autoplot.hclust(hc, ...) 93 | } 94 | 95 | ##' @rdname autoplot 96 | ##' @method autoplot hdbscan 97 | ##' @export 98 | autoplot.hdbscan <- function(object, ...) { 99 | autoplot.hclust(object@hc, ...) 100 | } 101 | 102 | 103 | ##' @rdname autoplot 104 | ##' @method autoplot hkmeans 105 | ##' @export 106 | autoplot.hkmeans <- function(object, ...) { 107 | autoplot.hclust(object@hclust, ...) 108 | } 109 | 110 | ##' @rdname autoplot 111 | ##' @method autoplot dendro 112 | ##' @export 113 | autoplot.dendro <- function(object, ...) { 114 | autoplot.hclust(object, ...) 115 | } 116 | 117 | ##' @rdname autoplot 118 | ##' @method autoplot pvclust 119 | ##' @importFrom ggtree geom_nodelab 120 | ##' @importFrom ggtree geom_hilight 121 | ##' @importFrom ggplot2 aes_ 122 | ##' @importFrom ggplot2 scale_color_manual 123 | ##' @importFrom tidytree as.treedata 124 | ##' @importFrom tidytree as_tibble 125 | ##' @importFrom tidytree rootnode as.phylo 126 | ##' @importFrom tidytree offspring 127 | ##' @export 128 | autoplot.pvclust <- function(object, layout = "dendrogram", ladderize = FALSE, 129 | label_edge = FALSE, pvrect = FALSE, alpha = 0.95, hang = 0.1, ...) { 130 | 131 | x <- as.treedata(object, hang = hang) 132 | 133 | p <- autoplot.hclust(x, layout=layout, ladderize=ladderize, ...) + 134 | geom_nodelab(aes_(label=~au, color="au"), angle=0, vjust=-.5, hjust=1.3) + 135 | geom_nodelab(aes_(label=~bp, color="bp"), angle=0, vjust=-.5, hjust=-.2) + 136 | scale_color_manual(values=c("au"="#97c497", "bp" = "#e06663"), name = 'p-values') 137 | 138 | if (label_edge) { 139 | p <- p + geom_nodelab(aes_(label=~sub("_edge", "", label)), 140 | color="grey50", hjust=0.5, angle=0, vjust=1.2, size=3.5) 141 | 142 | } 143 | 144 | if (pvrect) { 145 | d <- as_tibble(x) 146 | 147 | n <- d$node[d$au > alpha * 100] 148 | n <- n[!is.na(n)] 149 | n <- n[n != rootnode(as.phylo(x))] 150 | 151 | os <- offspring(x, n) 152 | xx <- do.call('rbind', lapply(os, function(i) n %in% i)) 153 | n <- n[!apply(xx, 2, any)] 154 | 155 | options(clade_align = FALSE) 156 | on.exit(options(clade_align = TRUE)) 157 | p <- p + geom_hilight(node = n, fill=NA, colour='red', 158 | to.bottom=TRUE, extendto=-.2) 159 | } 160 | 161 | return(p) 162 | } 163 | 164 | ##' @rdname autoplot 165 | ##' @method autoplot ClusterExperiment 166 | ##' @importFrom ggplot2 aes_ 167 | ##' @export 168 | autoplot.ClusterExperiment <- function(object, layout = 'rectangular', ...) { 169 | x <- as.treedata(object@dendro_clusters) 170 | p <- ggtree(x, layout = layout) 171 | if(any(!is.na(x@data$ClusterIdMerge))) { 172 | p <- p + geom_hilight(mapping=aes_(subset=~!is.na(ClusterIdMerge), 173 | fill=~ClusterIdMerge)) 174 | } 175 | 176 | return(p) 177 | } 178 | 179 | ##' @rdname autoplot 180 | ##' @method autoplot genoMatriXeR 181 | ##' @export 182 | autoplot.genoMatriXeR <- function(object, hctype = "rows", ...) { 183 | hctype <- match.arg(hctype, c("rows", "cols")) 184 | 185 | obj <- object@matrix 186 | if (hctype == "rows") { 187 | hc <- obj[["FitRow"]] 188 | } else { 189 | hc <- obj[["FitCol"]] 190 | } 191 | autoplot.hclust(hc, ...) 192 | } 193 | 194 | ##' @rdname autoplot 195 | ##' @method autoplot multiLocalZScore 196 | ##' @export 197 | autoplot.multiLocalZScore <- function(object, ...) { 198 | autoplot.hclust(object@matrix[["FitRow"]], ...) 199 | } 200 | 201 | 202 | -------------------------------------------------------------------------------- /R/geom-line-cutree.R: -------------------------------------------------------------------------------- 1 | ##' @title geom_line_cutree 2 | ##' @rdname geom-line-cutree 3 | ##' @param group output of cutree or number of subtree 4 | ##' @param linetype linetype 5 | ##' @param offset offset of the line 6 | ##' @param ... additional parameters to set the line (e.g., color, size, etc.) 7 | ##' @return line layer 8 | ##' @export 9 | ##' @examples 10 | ##' d <- dist(USArrests) 11 | ##' hc <- hclust(d, "ave") 12 | ##' autoplot(hc) + geom_line_cutree(4) 13 | geom_line_cutree <- function(group, linetype = "dashed", offset = 0, ...) { 14 | structure(list( 15 | group = group, 16 | linetype = linetype, 17 | offset = offset, 18 | ... 19 | ), 20 | class = "line_cutree" 21 | ) 22 | } 23 | 24 | 25 | hc_cluster <- function(hc, group) { 26 | if (is.numeric(group) && length(group) == 1) { 27 | group <- cutree(hc, group) 28 | } 29 | return(group) 30 | } 31 | 32 | ##' @importFrom ggplot2 ggplot_add 33 | ##' @method ggplot_add line_cutree 34 | ##' @importFrom ggplot2 geom_vline 35 | ##' @importFrom tidytree as.phylo 36 | ##' @export 37 | ggplot_add.line_cutree <- function(object, plot, object_name) { 38 | hc <- as.hclust(as.phylo(plot$data)) 39 | 40 | group <- hc_cluster(hc, object$group) 41 | 42 | offset <- object$offset 43 | 44 | params <- object 45 | params$group <- NULL 46 | params$offset <- NULL 47 | 48 | n <- length(hc$order) 49 | k <- length(unique(group)) 50 | height <- hc$height[n-k]/2 51 | params$xintercept <- (height + offset) * -1 52 | 53 | ly <- do.call(geom_vline, params) 54 | ggplot_add(ly, plot, object_name) 55 | } 56 | 57 | -------------------------------------------------------------------------------- /R/geom-rect-subtree.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | ##' @title geom_rect_subtree 4 | ##' @rdname geom-rect-subtree 5 | ##' @param group output of cutree or number of subtree 6 | ##' @param color border color to highlight subtrees 7 | ##' @param ... additional parameters pass to 'ggtree::hilight()' 8 | ##' @return rect layer 9 | ##' @seealso 10 | ##' [geom_hilight][ggtree::geom_hilight]; 11 | ##' @export 12 | ##' @examples 13 | ##' d <- dist(USArrests) 14 | ##' hc <- hclust(d, "ave") 15 | ##' autoplot(hc) + geom_rect_subtree(4) 16 | geom_rect_subtree <- function(group=NULL, color = "red", ...) { 17 | structure(list( 18 | group = group, 19 | color = color, 20 | ... 21 | ), 22 | class = "rect_subtree") 23 | } 24 | 25 | 26 | ##' @method ggplot_add rect_subtree 27 | ##' @importFrom utils modifyList 28 | ##' @importFrom tidytree MRCA 29 | ##' @export 30 | ggplot_add.rect_subtree <- function(object, plot, object_name) { 31 | set_subtree_rec() 32 | on.exit(unset_subtree_rec()) 33 | 34 | if (is.null(object$node)) { 35 | hc <- as.hclust(as.phylo(plot$data)) 36 | group <- hc_cluster(hc, object$group) 37 | object$node <- sapply(split(names(group), group), 38 | FUN = function(ii) MRCA(as.phylo(plot$data), ii) 39 | ) 40 | } 41 | 42 | params <- object 43 | params$group <- NULL 44 | default_params <- list(fill = NA, to.bottom = TRUE) 45 | params <- modifyList(default_params, params) 46 | ly <- do.call(geom_hilight, params) 47 | ggplot_add(ly, plot, object_name) 48 | } 49 | 50 | set_subtree_rec <- function() { 51 | options(clade_width_extend = 0.35) 52 | options(clade_align = TRUE) 53 | } 54 | 55 | unset_subtree_rec <- function() { 56 | options(clade_width_extend = 0.5) 57 | options(clade_align = FALSE) 58 | } 59 | 60 | -------------------------------------------------------------------------------- /R/ggtreeDendro-package.R: -------------------------------------------------------------------------------- 1 | #' @keywords internal 2 | "_PACKAGE" 3 | 4 | -------------------------------------------------------------------------------- /R/scale-color-subtree.R: -------------------------------------------------------------------------------- 1 | ##' scale tree color by subtree (e.g., output of cutree, kmeans, or other clustering algorithm) 2 | ##' 3 | ##' 4 | ##' @title scale_color_subtree 5 | ##' @rdname scale-color-subtree 6 | ##' @param group taxa group information 7 | ##' @return updated tree view 8 | ##' @export 9 | ##' @examples 10 | ##' d <- dist(USArrests) 11 | ##' hc <- hclust(d, "ave") 12 | ##' autoplot(hc) + scale_color_subtree(3) 13 | ##' @author Guangchuang Yu 14 | scale_color_subtree <- function(group) { 15 | if (inherits(group, 'kmeans')) { 16 | group <- group$cluster 17 | } 18 | 19 | structure(group, 20 | class = 'color_subtree' 21 | ) 22 | } 23 | 24 | ##' @rdname scale-color-subtree 25 | ##' @importFrom stats as.hclust 26 | ##' @importFrom stats cutree 27 | ##' @importFrom ggtree groupOTU 28 | ##' @export 29 | scale_colour_subtree <- scale_color_subtree 30 | 31 | scale_color_subtree_ <- function(p, group) { 32 | 33 | if (is.numeric(group) && length(group) == 1) { 34 | group <- cutree(as.hclust(as.phylo(p$data)), group) 35 | } 36 | 37 | g <- split(names(group), group) 38 | groupOTU(p, g, group_name = 'subtree') + 39 | aes_(color = ~subtree) 40 | } 41 | 42 | 43 | ##' @method ggplot_add color_subtree 44 | ##' @export 45 | ggplot_add.color_subtree <- function(object, plot, object_name) { 46 | scale_color_subtree_(plot, object) 47 | } 48 | 49 | -------------------------------------------------------------------------------- /R/wgcna.R: -------------------------------------------------------------------------------- 1 | ##' plot wgcna result 2 | ##' 3 | ##' Display WGCNA modules using ggtree. This function is similar to WGCNA::plotDendroAndColors() 4 | ##' @title plot_wgcna 5 | ##' @param x `WGCNA::blockwiseModules()`` output 6 | ##' @return ggtree object 7 | ##' @importFrom yulab.utils get_fun_from_pkg 8 | ##' @importFrom ggplot2 labs 9 | ##' @importFrom ggplot2 scale_fill_identity 10 | ##' @importFrom ggtree gheatmap 11 | ##' @export 12 | ##' @author Fengbei Li and Guangchuang Yu 13 | plot_wgcna <- function(x) { 14 | colors <- x$colors 15 | labels2colors <- yulab.utils::get_fun_from_pkg(fun="labels2colors", pkg="WGCNA") 16 | if(is.numeric(colors)) { 17 | colors <- labels2colors(colors) 18 | } 19 | 20 | d <- data.frame(Module=as.character(colors)[x$blockGenes[[1]]]) 21 | rownames(d) <- seq_along(d[,1]) 22 | 23 | p <- ggtree(x$dendrograms[[1]], layout = "dendrogram", ladderize = FALSE, size=.2) 24 | 25 | gheatmap(p, d, color=NA, width = .1) + scale_fill_identity() + 26 | labs(caption = as.character(as.expression(x$dendrograms[[1]]$call))) 27 | 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | ##' @importFrom utils packageDescription 2 | .onAttach <- function(libname, pkgname) { 3 | pkgVersion <- packageDescription(pkgname, fields="Version") 4 | #msg <- paste0(pkgname, " v", pkgVersion, " ", 5 | # "For help: https://guangchuangyu.github.io/software/", pkgname, "\n\n") 6 | msg <- "\n" 7 | 8 | citation <- paste0("If you use ", pkgname, " in published research, please cite:\n", 9 | "Guangchuang Yu. (2022). ", 10 | "Data Integration, Manipulation and Visualization of Phylogenetic Trees (1st edition). ", 11 | "Chapman and Hall/CRC. doi:10.1201/9781003279242") 12 | 13 | packageStartupMessage(paste0(msg, citation)) 14 | 15 | } 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This package offers `autoplot` methods to allow automatically visualizing tree objects (`hclust`, `dendrogram`, etc.) using `ggtree`. 2 | 3 | 4 | 5 | 6 | Coverage: 7 | 8 | 9 | 10 | 11 | + `agnes` object defined in `cluster` package 12 | + `bclust` object defined in `flexclust` (S4) or `e1071` (S3) package 13 | + `ClusterExperiment` object defined in `ClusterExperiment` package 14 | + `dendro` object defined in `ggdendro` package 15 | + `dendrogram` object defined in `stats` package 16 | - `FCPS::HierarchicalDBSCAN()[['Dendrogram']]` 17 | - `FCPS::HierarchicalClustering()[['Dendrogram']]` 18 | - `BHC::bhc()` 19 | + `diana` object defined in `cluster` package 20 | + `genoMatriXeR` object defined in the `regioneReloaded` package 21 | + `hclust` object defined in `stats` package 22 | - `amap::hcluster()` 23 | - `fastcluster::hclust()` 24 | - `genieclust::gclust()` 25 | - `FactoClass::ward.cluster()` 26 | - `goSTAG::performHierarchicalClustering()` 27 | - `HGC::HGC.dendrogram()` 28 | + `hdbscan` object defined in `dbscan` package 29 | + `hkmeans` object defined in `factoextra` package 30 | + `linkage` object defined in the `mdendro` package 31 | + `multiLocalZScore` object defined in the `regioneReloaded` package 32 | + `protoclust` object defined in `protoclust` package 33 | + `pvclust` object defined in `pvclust` package 34 | + `twins` object defined in `cluster` package 35 | 36 | 37 | 38 | It also provides `plot_xx` functions to visualize analysis output that is stored using R's built-in data type (e.g., `list`). 39 | 40 | + `plot_wgcna()` for `WGCNA::blockwiseModules()` output 41 | 42 | -------------------------------------------------------------------------------- /inst/CITATION: -------------------------------------------------------------------------------- 1 | citHeader("To cite ggtreeDendro in publications use:") 2 | 3 | citEntry( 4 | entry = "book", 5 | title = "Data Integration, Manipulation and Visualization of Phylogenetic Treess", 6 | author = person("Guangchuang", "Yu"), 7 | publisher = "Chapman and Hall/{CRC}", 8 | year = "2022", 9 | edition = "1st edition", 10 | doi = "10.1201/9781003279242", 11 | url = "https://www.amazon.com/Integration-Manipulation-Visualization-Phylogenetic-Computational-ebook/dp/B0B5NLZR1Z/", 12 | textVersion = paste("Guangchuang Yu. (2022).", 13 | "Data Integration, Manipulation and Visualization of Phylogenetic Trees (1st edition).", 14 | "Chapman and Hall/CRC. doi:10.1201/9781003279242") 15 | ) 16 | -------------------------------------------------------------------------------- /man/autoplot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/autoplot.R 3 | \name{autoplot} 4 | \alias{autoplot} 5 | \alias{autoplot.hclust} 6 | \alias{autoplot.linkage} 7 | \alias{autoplot.dendrogram} 8 | \alias{autoplot.agnes} 9 | \alias{autoplot.diana} 10 | \alias{autoplot.twins} 11 | \alias{autoplot.bclust} 12 | \alias{autoplot.hdbscan} 13 | \alias{autoplot.hkmeans} 14 | \alias{autoplot.dendro} 15 | \alias{autoplot.pvclust} 16 | \alias{autoplot.ClusterExperiment} 17 | \alias{autoplot.genoMatriXeR} 18 | \alias{autoplot.multiLocalZScore} 19 | \title{autoplot} 20 | \usage{ 21 | autoplot(object, ...) 22 | 23 | \method{autoplot}{hclust}(object, layout = "dendrogram", ladderize = FALSE, hang = 0.1, ...) 24 | 25 | \method{autoplot}{linkage}(object, layout = "dendrogram", ladderize = FALSE, hang = 0.1, ...) 26 | 27 | \method{autoplot}{dendrogram}(object, layout = "dendrogram", ladderize = FALSE, hang = 0.1, ...) 28 | 29 | \method{autoplot}{agnes}(object, layout = "dendrogram", ladderize = FALSE, hang = 0.1, ...) 30 | 31 | \method{autoplot}{diana}(object, layout = "dendrogram", ladderize = FALSE, hang = 0.1, ...) 32 | 33 | \method{autoplot}{twins}(object, layout = "dendrogram", ladderize = FALSE, hang = 0.1, ...) 34 | 35 | \method{autoplot}{bclust}(object, ...) 36 | 37 | \method{autoplot}{hdbscan}(object, ...) 38 | 39 | \method{autoplot}{hkmeans}(object, ...) 40 | 41 | \method{autoplot}{dendro}(object, ...) 42 | 43 | \method{autoplot}{pvclust}( 44 | object, 45 | layout = "dendrogram", 46 | ladderize = FALSE, 47 | label_edge = FALSE, 48 | pvrect = FALSE, 49 | alpha = 0.95, 50 | hang = 0.1, 51 | ... 52 | ) 53 | 54 | \method{autoplot}{ClusterExperiment}(object, layout = "rectangular", ...) 55 | 56 | \method{autoplot}{genoMatriXeR}(object, hctype = "rows", ...) 57 | 58 | \method{autoplot}{multiLocalZScore}(object, ...) 59 | } 60 | \arguments{ 61 | \item{object}{input object} 62 | 63 | \item{...}{additional paramters that passed to ggtree} 64 | 65 | \item{layout}{layout for plotting the tree} 66 | 67 | \item{ladderize}{logical whether ladderize the tree (default FALSE)} 68 | 69 | \item{hang}{numeric The fraction of the tree plot height by which labels 70 | should hang below the rest of the plot. A negative value will cause the 71 | labels to hang down from 0.} 72 | 73 | \item{label_edge}{logical whether display the label of edge (only for 74 | pvclust object), default is FALSE.} 75 | 76 | \item{pvrect}{logical whether display the clusters with relatively high/low 77 | p-values, default is FALSE. (only for pvclust object)} 78 | 79 | \item{alpha}{numeric the threshold value for p-values, default is 0.95 (only 80 | for pvclust object).} 81 | } 82 | \value{ 83 | ggtree object 84 | } 85 | \description{ 86 | autoplot 87 | } 88 | \examples{ 89 | d <- dist(USArrests) 90 | hc <- hclust(d, "ave") 91 | autoplot(hc) + geom_tiplab() 92 | } 93 | -------------------------------------------------------------------------------- /man/geom-line-cutree.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/geom-line-cutree.R 3 | \name{geom_line_cutree} 4 | \alias{geom_line_cutree} 5 | \title{geom_line_cutree} 6 | \usage{ 7 | geom_line_cutree(group, linetype = "dashed", offset = 0, ...) 8 | } 9 | \arguments{ 10 | \item{group}{output of cutree or number of subtree} 11 | 12 | \item{linetype}{linetype} 13 | 14 | \item{offset}{offset of the line} 15 | 16 | \item{...}{additional parameters to set the line (e.g., color, size, etc.)} 17 | } 18 | \value{ 19 | line layer 20 | } 21 | \description{ 22 | geom_line_cutree 23 | } 24 | \examples{ 25 | d <- dist(USArrests) 26 | hc <- hclust(d, "ave") 27 | autoplot(hc) + geom_line_cutree(4) 28 | } 29 | -------------------------------------------------------------------------------- /man/geom-rect-subtree.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/geom-rect-subtree.R 3 | \name{geom_rect_subtree} 4 | \alias{geom_rect_subtree} 5 | \title{geom_rect_subtree} 6 | \usage{ 7 | geom_rect_subtree(group = NULL, color = "red", ...) 8 | } 9 | \arguments{ 10 | \item{group}{output of cutree or number of subtree} 11 | 12 | \item{color}{border color to highlight subtrees} 13 | 14 | \item{...}{additional parameters pass to 'ggtree::hilight()'} 15 | } 16 | \value{ 17 | rect layer 18 | } 19 | \description{ 20 | geom_rect_subtree 21 | } 22 | \examples{ 23 | d <- dist(USArrests) 24 | hc <- hclust(d, "ave") 25 | autoplot(hc) + geom_rect_subtree(4) 26 | } 27 | \seealso{ 28 | \link[ggtree:geom-hilight]{geom_hilight}; 29 | } 30 | -------------------------------------------------------------------------------- /man/ggtreeDendro-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/ggtreeDendro-package.R 3 | \docType{package} 4 | \name{ggtreeDendro-package} 5 | \alias{ggtreeDendro} 6 | \alias{ggtreeDendro-package} 7 | \title{ggtreeDendro: Drawing 'dendrogram' using 'ggtree'} 8 | \description{ 9 | Offers a set of 'autoplot' methods to visualize tree-like structures (e.g., hierarchical clustering and classification/regression trees) using 'ggtree'. You can adjust graphical parameters using grammar of graphic syntax and integrate external data to the tree. 10 | } 11 | \author{ 12 | \strong{Maintainer}: Guangchuang Yu \email{guangchuangyu@gmail.com} (\href{https://orcid.org/0000-0002-6485-8781}{ORCID}) [copyright holder] 13 | 14 | Other contributors: 15 | \itemize{ 16 | \item Shuangbin Xu \email{xshuangbin@163.com} (\href{https://orcid.org/0000-0003-3513-5362}{ORCID}) [contributor] 17 | \item Chuanjie Zhang \email{959264868@qq.com} [contributor] 18 | } 19 | 20 | } 21 | \keyword{internal} 22 | -------------------------------------------------------------------------------- /man/scale-color-subtree.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/scale-color-subtree.R 3 | \name{scale_color_subtree} 4 | \alias{scale_color_subtree} 5 | \alias{scale_colour_subtree} 6 | \title{scale_color_subtree} 7 | \usage{ 8 | scale_color_subtree(group) 9 | 10 | scale_colour_subtree(group) 11 | } 12 | \arguments{ 13 | \item{group}{taxa group information} 14 | } 15 | \value{ 16 | updated tree view 17 | } 18 | \description{ 19 | scale tree color by subtree (e.g., output of cutree, kmeans, or other clustering algorithm) 20 | } 21 | \examples{ 22 | d <- dist(USArrests) 23 | hc <- hclust(d, "ave") 24 | autoplot(hc) + scale_color_subtree(3) 25 | } 26 | \author{ 27 | Guangchuang Yu 28 | } 29 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | # This file is part of the standard setup for testthat. 2 | # It is recommended that you do not modify it. 3 | # 4 | # Where should you do additional test configuration? 5 | # Learn more about the roles of various files in: 6 | # * https://r-pkgs.org/tests.html 7 | # * https://testthat.r-lib.org/reference/test_package.html#special-files 8 | 9 | library(testthat) 10 | library(ggtreeDendro) 11 | 12 | test_check("ggtreeDendro") 13 | -------------------------------------------------------------------------------- /tests/testthat/test-autoplot.R: -------------------------------------------------------------------------------- 1 | context('autoplot for hclust class') 2 | 3 | library(ggtree) 4 | library(ggtreeDendro) 5 | 6 | data(USArrests) 7 | d <- dist(USArrests) 8 | hc <- hclust(d, 'ave') 9 | 10 | test_that('autoplot support hclust class', { 11 | p <- autoplot(hc) 12 | expect_true(inherits(p, 'ggtree')) 13 | }) 14 | -------------------------------------------------------------------------------- /vignettes/ggtreeDendro.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Visualizing Dendrogram using ggtree" 3 | author: 4 | - name: Guangchuang Yu 5 | email: guangchuangyu@gmail.com 6 | affiliation: Department of Bioinformatics, School of Basic Medical Sciences, Southern Medical University 7 | date: "`r Sys.Date()`" 8 | output: 9 | prettydoc::html_pretty: 10 | theme: cayman 11 | highlight: github 12 | pdf_document: 13 | toc: true 14 | vignette: > 15 | %\VignetteIndexEntry{Visualizing Dendrogram using ggtree} 16 | %\VignetteEngine{knitr::rmarkdown} 17 | %\usepackage[utf8]{inputenc} 18 | %\VignetteEncoding{UTF-8} 19 | --- 20 | 21 | ```{r style, echo=FALSE, results="asis", message=FALSE} 22 | knitr::opts_chunk$set(tidy = FALSE, 23 | warning = FALSE, 24 | message = FALSE) 25 | library(yulab.utils) 26 | ``` 27 | 28 | # Introduction 29 | 30 | ```{r} 31 | library(ggtree) 32 | library(ggtreeDendro) 33 | library(aplot) 34 | scale_color_subtree <- ggtreeDendro::scale_color_subtree 35 | ``` 36 | 37 | Clustering is very importance method to classify items into different categories and to infer functions since similar objects tend to behavior similarly. There are more than 200 packages in Bioconductor implement clustering algorithms or employ clustering methods for omic-data analysis. 38 | 39 | Albeit the methods are important for data analysis, the visualization is quite limited. Most the the packages only have the ability to visualize the hierarchical tree structure using `stats:::plot.hclust()`. This package is design to visualize hierarchical tree structure with associated data (e.g., clinical information collected with the samples) using the powerful in-house developed `r Biocpkg("ggtree")` package. 40 | 41 | This package implements a set of `autoplot()` methods to display tree structure. We will implement more `autoplot()` methods to support more objects. The output of these `autoplot()` methods is a `ggtree` object, which can be further annotated by adding layers using `r CRANpkg("ggplot2")` syntax. Integrating associated data to annotate the tree is also supported by `r Biocpkg("ggtreeExtra")` package. 42 | 43 | Here are some demonstrations of using `autoplot()` methods to visualize common hierarchical clustering tree objects. 44 | 45 | ## `hclust` and `dendrogram` objects 46 | 47 | These two classes are defined in the `r CRANpkg("stats")` package. 48 | 49 | ```{r fig.width=16, fig.height=6} 50 | d <- dist(USArrests) 51 | 52 | hc <- hclust(d, "ave") 53 | den <- as.dendrogram(hc) 54 | 55 | p1 <- autoplot(hc) + geom_tiplab() 56 | p2 <- autoplot(den) 57 | plot_list(p1, p2, ncol=2) 58 | ``` 59 | 60 | 61 | ## `linkage` object 62 | 63 | The class `linkage` is defined in the `r CRANpkg("mdendro")` package. 64 | 65 | ```{r fig.width=8, fig.height=8} 66 | library("mdendro") 67 | lnk <- linkage(d, digits = 1, method = "complete") 68 | autoplot(lnk, layout = 'circular') + geom_tiplab() + 69 | scale_color_subtree(4) + theme_tree() 70 | ``` 71 | 72 | ## `agnes`, `diana` and `twins` objects 73 | 74 | These classes are defined in the `r CRANpkg("cluster")` package. 75 | 76 | ```{r fig.width=16, height=6} 77 | library(cluster) 78 | x1 <- agnes(mtcars) 79 | x2 <- diana(mtcars) 80 | 81 | p1 <- autoplot(x1) + geom_tiplab() 82 | p2 <- autoplot(x2) + geom_tiplab() 83 | plot_list(p1, p2, ncol=2) 84 | ``` 85 | 86 | 87 | ## `pvclust` object 88 | 89 | The `pvclust` class is defined in the `r CRANpkg("pvclust")` package. 90 | 91 | 92 | ```{r fig.width=8, height=6} 93 | library(pvclust) 94 | data(Boston, package = "MASS") 95 | 96 | set.seed(123) 97 | result <- pvclust(Boston, method.dist="cor", method.hclust="average", nboot=1000, parallel=TRUE) 98 | autoplot(result, label_edge=TRUE, pvrect = TRUE) + geom_tiplab() 99 | ``` 100 | 101 | The `pvclust` object contains two types of p-values: AU (Approximately Unbiased) p-value and BP (Boostrap Probability) value. These values will be automatically labelled on the tree. 102 | 103 | 104 | # Session information 105 | 106 | Here is the output of sessionInfo() on the system on which this document was compiled: 107 | 108 | ```{r, echo=FALSE} 109 | sessionInfo() 110 | ``` 111 | --------------------------------------------------------------------------------