Debian Codegraph @

vignettes/ggdendro.Rmd

raw
1 ---
2 title: "Using 'ggdendro' to plot dendrograms"
3 output: rmarkdown::html_vignette
4 vignette: >
5 %\VignetteIndexEntry{Using 'ggdendro' to plot dendrograms}
6 %\VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
8 ---
9
10 ```{r, include = FALSE}
11 knitr::opts_chunk$set(
12 collapse = TRUE,
13 comment = "#>"
14 )
15 ```
16
17
18 The `ggdendro` package makes it easy to extract dendrogram and tree diagrams into a list of data frames. You can then use this list to recreate these types of plots using the `ggplot2` package.
19
20 ## Introduction
21
22 The `ggdendro` package provides a general framework to extract the plot data for dendrograms and tree diagrams.
23
24 It does this by providing generic function `dendro_data()` that extracts the appropriate segment and label data, returning the data as a list of data frames.
25
26 You can access these data frames using three accessor functions:
27
28 - `segment()`
29 - `label()`
30 - `leaf_label()`
31
32 The package also provides two convenient wrapper functions:
33
34
35 - `ggdendrogram()` is a wrapper around `ggplot()` to create a dendrogram using a single line of code. The resulting object is of class `ggplot`, so can be manipulated using the `ggplot2` tools.
36 - `theme_dendro()` is a `ggplot2` theme with a blank canvas, i.e. no axes, axis labels or tick marks.
37
38 The `ggplot2` package doesn't get loaded automatically, so remember to load it first:
39
40 ```{r init}
41 library(ggplot2)
42 library(ggdendro)
43 ```
44
45 ## Using the 'ggdendrogram()' wrapper
46
47 The `ggdendro` package extracts the plot data from dendrogram objects. Sometimes it is useful to have fine-grained control over the plot. Other times it might be more convenient to have a simple wrapper around `ggplot()` to produce a dendrogram with a small amount of code.
48
49 The function `ggdendrogram()` provides such a wrapper to produce a plot with a single line of code. It provides a few options for controlling the display of line segments, labels and plot rotation (rotated by 90 degrees or not).
50
51 ```{r dendrogram}
52 hc <- hclust(dist(USArrests), "ave")
53 ggdendrogram(hc, rotate = FALSE, size = 2)
54 ```
55
56
57 The next section shows how to take full control over the data extraction and subsequent plotting.
58
59 ## Extracting the dendrogram plot data using 'dendro_data()'
60
61 The `hclust()` and `dendrogram()` functions in R makes it easy to plot the results of hierarchical cluster analysis and other dendrograms in R. However, it is hard to extract the data from this analysis to customize these plots, since the `plot()` functions for both these classes prints directly without the option of returning the plot data.
62
63 ```{r dendro1}
64 model <- hclust(dist(USArrests), "ave")
65 dhc <- as.dendrogram(model)
66 # Rectangular lines
67 ddata <- dendro_data(dhc, type = "rectangle")
68 p <- ggplot(segment(ddata)) +
69 geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
70 coord_flip() +
71 scale_y_reverse(expand = c(0.2, 0))
72 p
73 ```
74
75
76
77 Of course, using `ggplot2` to create the dendrogram means one has full control over the appearance of the plot. For example, here is the same data, but this time plotted horizontally with a clean background. In `ggplot2` this means
78 passing a number of options to `theme`. The `ggdendro` packages exports a function, `theme_dendro()` that wraps these options into a convenient function.
79
80 ```{r dendro-2}
81 p +
82 coord_flip() +
83 theme_dendro()
84 ```
85
86
87 You can also draw dendrograms with triangular line segments (instead of rectangular segments). For example:
88
89 ```{r dendro-3}
90 ddata <- dendro_data(dhc, type = "triangle")
91 ggplot(segment(ddata)) +
92 geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
93 coord_flip() +
94 scale_y_reverse(expand = c(0.2, 0)) +
95 theme_dendro()
96 ```
97
98
99
100 ## Regression tree diagrams
101
102 The `tree()` function in package `tree` creates tree diagrams. To extract the plot data for these diagrams using `ggdendro`, you use the the same idiom as for plotting dendrograms:
103
104 ```{r tree}
105 if(require(tree)){
106 data(cpus, package = "MASS")
107 model <- tree(log10(perf) ~ syct + mmin + mmax + cach + chmin + chmax,
108 data = cpus)
109 tree_data <- dendro_data(model)
110 ggplot(segment(tree_data)) +
111 geom_segment(aes(x = x, y = y, xend = xend, yend = yend, size = n),
112 colour = "blue", alpha = 0.5) +
113 scale_size("n") +
114 geom_text(data = label(tree_data),
115 aes(x = x, y = y, label = label), vjust = -0.5, size = 3) +
116 geom_text(data = leaf_label(tree_data),
117 aes(x = x, y = y, label = label), vjust = 0.5, size = 2) +
118 theme_dendro()
119 }
120 ```
121
122
123
124 ## Classification tree diagrams
125
126 The `rpart()` function in package `rpart` creates classification diagrams. To extract the plot data for these diagrams using `ggdendro` follows the same basic pattern as dendrograms:
127
128 ```{r rpart}
129 if(require(rpart)){
130 model <- rpart(Kyphosis ~ Age + Number + Start,
131 method = "class", data = kyphosis)
132 ddata <- dendro_data(model)
133 ggplot() +
134 geom_segment(data = ddata$segments,
135 aes(x = x, y = y, xend = xend, yend = yend)) +
136 geom_text(data = ddata$labels,
137 aes(x = x, y = y, label = label), size = 3, vjust = 0) +
138 geom_text(data = ddata$leaf_labels,
139 aes(x = x, y = y, label = label), size = 3, vjust = 1) +
140 theme_dendro()
141 }
142 ```
143
144
145 ## Twins diagrams: 'agnes' and 'diana'
146
147 The `cluster` package allows you to draw `agnes` and `diana` diagrams.
148
149 ```{r twins}
150 if(require(cluster)){
151 model <- agnes(votes.repub, metric = "manhattan", stand = TRUE)
152 dg <- as.dendrogram(model)
153 ggdendrogram(dg)
154
155 model <- diana(votes.repub, metric = "manhattan", stand = TRUE)
156 dg <- as.dendrogram(model)
157 ggdendrogram(dg)
158 }
159
160 ```
161
162
163 ## Summary
164
165 The `ggdendro` package makes it easy to extract the line segment and label data from `hclust`, `dendrogram` and `tree` objects.