| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/ggdend.R |
| 3 |
\name{ggdend} |
| 4 |
\alias{ggdend} |
| 5 |
\alias{as.ggdend} |
| 6 |
\alias{as.ggdend.dendrogram} |
| 7 |
\alias{prepare.ggdend} |
| 8 |
\alias{ggplot.ggdend} |
| 9 |
\alias{ggplot.dendrogram} |
| 10 |
\alias{print.ggdend} |
| 11 |
\title{Creates dendrogram plot using ggplot.} |
| 12 |
\source{ |
| 13 |
These are extended versions of the functions \link[ggdendro]{ggdendrogram}, \link[ggdendro]{dendro_data} (and the hidden dendrogram_data) |
| 14 |
from Andrie de Vries's ggdendro package. The motivation for this fork is the need to add more graphical parameters |
| 15 |
to the plotted tree. This required a strong mixter of functions from ggdendro and dendextend (to the point that |
| 16 |
it seemed better to just fork the code into its current form) |
| 17 |
} |
| 18 |
\usage{ |
| 19 |
ggdend(...) |
| 20 |
|
| 21 |
as.ggdend(dend, ...) |
| 22 |
|
| 23 |
\method{as.ggdend}{dendrogram}(dend, type = c("rectangle", "triangle"), edge.root = FALSE, ...) |
| 24 |
|
| 25 |
prepare.ggdend(data, ...) |
| 26 |
|
| 27 |
\method{ggplot}{ggdend}( |
| 28 |
data = NULL, |
| 29 |
mapping = aes(), |
| 30 |
..., |
| 31 |
segments = TRUE, |
| 32 |
labels = TRUE, |
| 33 |
nodes = TRUE, |
| 34 |
horiz = FALSE, |
| 35 |
theme = theme_dendro(), |
| 36 |
offset_labels = 0, |
| 37 |
na.rm = TRUE, |
| 38 |
environment = parent.frame() |
| 39 |
) |
| 40 |
|
| 41 |
\method{ggplot}{dendrogram}(data, ...) |
| 42 |
|
| 43 |
\method{print}{ggdend}(x, ...) |
| 44 |
} |
| 45 |
\arguments{ |
| 46 |
\item{...}{mostly ignored.} |
| 47 |
|
| 48 |
\item{dend}{a \link{dendrogram} tree (to be turned into a ggdend object)} |
| 49 |
|
| 50 |
\item{type}{The type of plot, indicating the shape of the dendrogram. "rectangle" will draw |
| 51 |
rectangular lines, while "triangle" will draw triangular lines.} |
| 52 |
|
| 53 |
\item{edge.root}{currently ignored. One day it might do the following: logical; if true, draw an edge to the root node.} |
| 54 |
|
| 55 |
\item{data, x}{a ggdend class object (passed to ggplot.dendrogram or print.ggdend).} |
| 56 |
|
| 57 |
\item{mapping}{(passed in ggplot.ggdend) Default list of aesthetic mappings to use for plot. If not specified, must be supplied in each layer added to the plot.} |
| 58 |
|
| 59 |
\item{segments}{a logical (TRUE) if to plot the segments (branches).} |
| 60 |
|
| 61 |
\item{labels}{a logical (TRUE) if to plot the labels.} |
| 62 |
|
| 63 |
\item{nodes}{a logical (TRUE) if to plot the nodes (points).} |
| 64 |
|
| 65 |
\item{horiz}{a logical (TRUE) indicating if the dendrogram should be drawn horizontally or not.} |
| 66 |
|
| 67 |
\item{theme}{the ggplot2 theme to use (default is \link{theme_dendro}, can also be NULL |
| 68 |
for the default ggplot2 theme)} |
| 69 |
|
| 70 |
\item{offset_labels}{a numeric value to offset the labels from the leaves} |
| 71 |
|
| 72 |
\item{na.rm}{A logical (TRUE) to control removal of missing values. Passed to |
| 73 |
\link[ggplot2]{geom_line} and \link[ggplot2]{geom_point}} |
| 74 |
|
| 75 |
\item{environment}{(passed in ggplot.ggdend) deprecated / ignored.} |
| 76 |
} |
| 77 |
\value{ |
| 78 |
\itemize{ |
| 79 |
\item{\code{as.ggdend} - returns an object of class ggdend which is a list with 3 componants: segments, labels, nodes. |
| 80 |
Each one contains the graphical parameters from the original dendrogram, but in a tabular form that |
| 81 |
can be used by ggplot2+geom_segment+geom_text to create a dendrogram plot.} |
| 82 |
\item{\code{prepare.ggdend} - a \code{ggdend} object (after filling it with various default values)} |
| 83 |
\item{\code{ggplot.ggdend} - a \link[ggplot2]{ggplot} object} |
| 84 |
} |
| 85 |
} |
| 86 |
\description{ |
| 87 |
Several functions for creating a dendrogram plot using ggplot2. |
| 88 |
The core process is to transform a dendrogram into a ggdend object using as.ggdend, |
| 89 |
and then plot it using ggplot. These two steps can be done in one command with either the function |
| 90 |
ggplot or ggdend. |
| 91 |
|
| 92 |
The reason we want to have as.ggdend (and not only ggplot.dendrogram), is (1) so that you could |
| 93 |
create your own mapping of ggdend and, (2) since as.ggdend might be slow for large trees, |
| 94 |
it is probably better to be able to run it only once for such cases. |
| 95 |
|
| 96 |
A ggdend class object is a list with 3 componants: segments, labels, nodes. |
| 97 |
Each one contains the graphical parameters from the original dendrogram, but in a tabular form that |
| 98 |
can be used by ggplot2+geom_segment+geom_text to create a dendrogram plot. |
| 99 |
} |
| 100 |
\details{ |
| 101 |
\code{prepare.ggdend} is used by \code{plot.ggdend} to take the \code{ggdend} object |
| 102 |
and prepare it for plotting. This is because the defaults of various parameters in \link{dendrogram}'s |
| 103 |
are not always stored in the object itself, but are built-in into the \link{plot.dendrogram} function. |
| 104 |
For example, the color of the labels is not (by default) specified in the dendrogram (only if we change it |
| 105 |
from black to something else). Hence, when taking the object into a different plotting engine (say ggplot2), we |
| 106 |
want to prepare the object by filling-in various defaults. |
| 107 |
This function is autmatically invoked within the \code{plot.ggdend} function. You would probably use |
| 108 |
it only if you'd wish to build your own ggplot2 mapping. |
| 109 |
} |
| 110 |
\examples{ |
| 111 |
|
| 112 |
\dontrun{ |
| 113 |
|
| 114 |
library(dendextend) |
| 115 |
# library(ggdendro) |
| 116 |
# Create a complex dend: |
| 117 |
dend <- iris[1:30, -5] \%>\% |
| 118 |
dist() \%>\% |
| 119 |
hclust() \%>\% |
| 120 |
as.dendrogram() \%>\% |
| 121 |
set("branches_k_color", k = 3) \%>\% |
| 122 |
set("branches_lwd", c(1.5, 1, 1.5)) \%>\% |
| 123 |
set("branches_lty", c(1, 1, 3, 1, 1, 2)) \%>\% |
| 124 |
set("labels_colors") \%>\% |
| 125 |
set("labels_cex", c(.9, 1.2)) |
| 126 |
# plot the dend in usual "base" plotting engine: |
| 127 |
plot(dend) |
| 128 |
# Now let's do it in ggplot2 :) |
| 129 |
ggd1 <- as.ggdend(dend) |
| 130 |
library(ggplot2) |
| 131 |
ggplot(ggd1) # reproducing the above plot in ggplot2 :) |
| 132 |
|
| 133 |
# Triangle version: |
| 134 |
plot(dend, type = "triangle") |
| 135 |
ggd2 <- as.ggdend(dend, type = "triangle") |
| 136 |
ggplot(ggd2) |
| 137 |
|
| 138 |
|
| 139 |
# More modifications: |
| 140 |
labels(dend) <- paste0(labels(dend), "00000") |
| 141 |
ggd1 <- as.ggdend(dend) |
| 142 |
# Use ylim to deal with long labels in ggplot2 |
| 143 |
ggplot(ggd1) + ylim(-.4, max(get_branches_heights(dend))) |
| 144 |
|
| 145 |
|
| 146 |
ggplot(ggd1, horiz = TRUE) # horiz plot in ggplot2 |
| 147 |
# Adding some extra spice to it... |
| 148 |
# creating a radial plot: |
| 149 |
ggplot(ggd1) + scale_y_reverse(expand = c(0.2, 0)) + coord_polar(theta = "x") |
| 150 |
# The text doesn't look so great, so let's remove it: |
| 151 |
ggplot(ggd1, labels = FALSE) + scale_y_reverse(expand = c(0.2, 0)) + coord_polar(theta = "x") |
| 152 |
|
| 153 |
# This can now be sent to plot.ly - which adds zoom-in abilities, and more. |
| 154 |
# Here is how it might look like: https://plot.ly/~talgalili/6/y-vs-x/ |
| 155 |
|
| 156 |
## Quick guide: |
| 157 |
# install.packages("devtools") |
| 158 |
# library("devtools") |
| 159 |
# devtools::install_github("ropensci/plotly") |
| 160 |
# library(plotly) |
| 161 |
# set_credentials_file(...) |
| 162 |
# you'll need to get it from here: https://plot.ly/ggplot2/getting-started/ |
| 163 |
|
| 164 |
# ggplot(ggd1) |
| 165 |
# py <- plotly() |
| 166 |
# py$ggplotly() |
| 167 |
|
| 168 |
# And you'll get something like this: https://plot.ly/~talgalili/6/y-vs-x/ |
| 169 |
|
| 170 |
# Another example: https://plot.ly/ggplot2/ |
| 171 |
} |
| 172 |
} |
| 173 |
\seealso{ |
| 174 |
\link{dendrogram}, \link{get_nodes_attr}, \link{get_leaves_nodePar}, |
| 175 |
\link[ggplot2]{ggplot}, |
| 176 |
\link[ggdendro]{ggdendrogram}, \link[ggdendro]{dendro_data}, |
| 177 |
} |
| 178 |
\author{ |
| 179 |
Tal Galili, using code modified from Andrie de Vries |
| 180 |
} |