| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/tanglegram.R |
| 3 |
\name{tanglegram} |
| 4 |
\alias{tanglegram} |
| 5 |
\alias{tanglegram.default} |
| 6 |
\alias{tanglegram.hclust} |
| 7 |
\alias{tanglegram.phylo} |
| 8 |
\alias{tanglegram.dendlist} |
| 9 |
\alias{tanglegram.dendrogram} |
| 10 |
\alias{dendbackback} |
| 11 |
\title{Tanglegram plot} |
| 12 |
\source{ |
| 13 |
The function is based on code from Johan Renaudie (plannapus), after major revisions. See: |
| 14 |
\url{https://stackoverflow.com/questions/12456768/duelling-dendrograms-in-r-placing-dendrograms-back-to-back-in-r} |
| 15 |
|
| 16 |
As far as I could tell, this code was originally inspired by Dylan Beaudette |
| 17 |
function \code{dueling.dendrograms} from the sharpshootR package: |
| 18 |
\url{https://CRAN.R-project.org/package=sharpshootR} |
| 19 |
tanglegram |
| 20 |
} |
| 21 |
\usage{ |
| 22 |
tanglegram(dend1, ...) |
| 23 |
|
| 24 |
\method{tanglegram}{default}(dend1, ...) |
| 25 |
|
| 26 |
\method{tanglegram}{hclust}(dend1, ...) |
| 27 |
|
| 28 |
\method{tanglegram}{phylo}(dend1, ...) |
| 29 |
|
| 30 |
\method{tanglegram}{dendlist}( |
| 31 |
dend1, |
| 32 |
which = c(1L, 2L), |
| 33 |
main_left, |
| 34 |
main_right, |
| 35 |
just_one = TRUE, |
| 36 |
... |
| 37 |
) |
| 38 |
|
| 39 |
\method{tanglegram}{dendrogram}( |
| 40 |
dend1, |
| 41 |
dend2, |
| 42 |
sort = FALSE, |
| 43 |
color_lines, |
| 44 |
lwd = 3.5, |
| 45 |
edge.lwd = NULL, |
| 46 |
columns_width = c(5, 3, 5), |
| 47 |
margin_top = 3, |
| 48 |
margin_bottom = 2.5, |
| 49 |
margin_inner = 3, |
| 50 |
margin_outer = 0.5, |
| 51 |
left_dendo_mar = c(margin_bottom, margin_outer, margin_top, margin_inner), |
| 52 |
right_dendo_mar = c(margin_bottom, margin_inner, margin_top, margin_outer), |
| 53 |
intersecting = TRUE, |
| 54 |
dLeaf = NULL, |
| 55 |
dLeaf_left = dLeaf, |
| 56 |
dLeaf_right = dLeaf, |
| 57 |
axes = TRUE, |
| 58 |
type = "r", |
| 59 |
lab.cex = NULL, |
| 60 |
remove_nodePar = FALSE, |
| 61 |
main = "", |
| 62 |
main_left = "", |
| 63 |
main_right = "", |
| 64 |
sub = "", |
| 65 |
k_labels = NULL, |
| 66 |
k_branches = NULL, |
| 67 |
rank_branches = FALSE, |
| 68 |
hang = FALSE, |
| 69 |
match_order_by_labels = TRUE, |
| 70 |
cex_main = 2, |
| 71 |
cex_main_left = cex_main, |
| 72 |
cex_main_right = cex_main, |
| 73 |
cex_sub = cex_main, |
| 74 |
highlight_distinct_edges = TRUE, |
| 75 |
common_subtrees_color_lines = TRUE, |
| 76 |
common_subtrees_color_lines_default_single_leaf_color = "grey", |
| 77 |
common_subtrees_color_branches = FALSE, |
| 78 |
highlight_branches_col = FALSE, |
| 79 |
highlight_branches_lwd = TRUE, |
| 80 |
faster = FALSE, |
| 81 |
just_one = TRUE, |
| 82 |
... |
| 83 |
) |
| 84 |
|
| 85 |
dendbackback( |
| 86 |
dend1, |
| 87 |
dend2, |
| 88 |
sort = FALSE, |
| 89 |
color_lines, |
| 90 |
lwd = 3.5, |
| 91 |
edge.lwd = NULL, |
| 92 |
columns_width = c(5, 3, 5), |
| 93 |
margin_top = 3, |
| 94 |
margin_bottom = 2.5, |
| 95 |
margin_inner = 3, |
| 96 |
margin_outer = 0.5, |
| 97 |
left_dendo_mar = c(margin_bottom, margin_outer, margin_top, margin_inner), |
| 98 |
right_dendo_mar = c(margin_bottom, margin_inner, margin_top, margin_outer), |
| 99 |
intersecting = TRUE, |
| 100 |
dLeaf = NULL, |
| 101 |
dLeaf_left = dLeaf, |
| 102 |
dLeaf_right = dLeaf, |
| 103 |
axes = TRUE, |
| 104 |
type = "r", |
| 105 |
lab.cex = NULL, |
| 106 |
remove_nodePar = FALSE, |
| 107 |
main = "", |
| 108 |
main_left = "", |
| 109 |
main_right = "", |
| 110 |
sub = "", |
| 111 |
k_labels = NULL, |
| 112 |
k_branches = NULL, |
| 113 |
rank_branches = FALSE, |
| 114 |
hang = FALSE, |
| 115 |
match_order_by_labels = TRUE, |
| 116 |
cex_main = 2, |
| 117 |
cex_main_left = cex_main, |
| 118 |
cex_main_right = cex_main, |
| 119 |
cex_sub = cex_main, |
| 120 |
highlight_distinct_edges = TRUE, |
| 121 |
common_subtrees_color_lines = TRUE, |
| 122 |
common_subtrees_color_lines_default_single_leaf_color = "grey", |
| 123 |
common_subtrees_color_branches = FALSE, |
| 124 |
highlight_branches_col = FALSE, |
| 125 |
highlight_branches_lwd = TRUE, |
| 126 |
faster = FALSE, |
| 127 |
just_one = TRUE, |
| 128 |
... |
| 129 |
) |
| 130 |
} |
| 131 |
\arguments{ |
| 132 |
\item{dend1}{tree object (dendrogram/dendlist/hclust/phylo), plotted on the left} |
| 133 |
|
| 134 |
\item{...}{not used.} |
| 135 |
|
| 136 |
\item{which}{an integer vector of length 2, indicating |
| 137 |
which of the trees in the dendlist object should be plotted} |
| 138 |
|
| 139 |
\item{main_left}{Character. Title of the left dendrogram.} |
| 140 |
|
| 141 |
\item{main_right}{Character. Title of the right dendrogram.} |
| 142 |
|
| 143 |
\item{just_one}{logical (TRUE). If FALSE, it means at least two tanglegrams |
| 144 |
will be plotted on the same page and so \link{layout} is not passed. |
| 145 |
See: \url{https://stackoverflow.com/q/39784746/4137985}} |
| 146 |
|
| 147 |
\item{dend2}{tree object (dendrogram/hclust/phylo), plotted on the right} |
| 148 |
|
| 149 |
\item{sort}{logical (FALSE). Should the dendrogram's labels be "sorted"? |
| 150 |
(might give a better tree in some cases).} |
| 151 |
|
| 152 |
\item{color_lines}{a vector of colors for the lines connected the labels. |
| 153 |
If the colors are shorter than the number of labels, they are recycled |
| 154 |
(and a warning is issued). |
| 155 |
The colors in the vector are applied on the lines from the bottom up.} |
| 156 |
|
| 157 |
\item{lwd}{width of the lines connecting the labels. (default is 3.5)} |
| 158 |
|
| 159 |
\item{edge.lwd}{width of the dendrograms lines. Default is NULL. |
| 160 |
If set, then it switches `highlight_branches_lwd` to FALSE. If you want thicker |
| 161 |
lines which reflect the height, please use \link{highlight_branches_lwd} on the |
| 162 |
dendrograms/dendlist.} |
| 163 |
|
| 164 |
\item{columns_width}{a vector with three elements, giving the relative |
| 165 |
sizes of the the three plots (left dendrogram, connecting lines, |
| 166 |
right dendrogram). This is passed to \link{layout} if parameter just_one is TRUE. |
| 167 |
The default is: c(5,3,5)} |
| 168 |
|
| 169 |
\item{margin_top}{the number of lines of margin to be specified on the top |
| 170 |
of the plots.} |
| 171 |
|
| 172 |
\item{margin_bottom}{the number of lines of margin to be specified on the |
| 173 |
bottom of the plots.} |
| 174 |
|
| 175 |
\item{margin_inner}{margin_bottom the number of lines of margin |
| 176 |
to be specified on the inner distence between the dendrograms |
| 177 |
and the connecting lines.} |
| 178 |
|
| 179 |
\item{margin_outer}{margin_bottom the number of lines of margin |
| 180 |
to be specified on the outer distence between the dendrograms |
| 181 |
and the connecting lines.} |
| 182 |
|
| 183 |
\item{left_dendo_mar}{mar parameters of the left dendrgoram.} |
| 184 |
|
| 185 |
\item{right_dendo_mar}{mar parameters of the right dendrgoram.} |
| 186 |
|
| 187 |
\item{intersecting}{logical (TRUE). Should the leaves of the two dendrograms |
| 188 |
be pruned so that the two trees will have the same labels?} |
| 189 |
|
| 190 |
\item{dLeaf}{a number specifying the distance in user coordinates between |
| 191 |
the tip of a leaf and its label. If NULL, as per default, |
| 192 |
3/4 of a letter width or height is used. |
| 193 |
|
| 194 |
Notice that if we are comparing two dendrograms with different |
| 195 |
heights, manually changing dLeaf will affect both trees differently. |
| 196 |
In such a case, it is recommanded to manually change dLeaf_left |
| 197 |
and dLeaf_right. |
| 198 |
This can be especially important when changing the lab.cex of the |
| 199 |
dendrogram's labels. |
| 200 |
Alternatively, one could manually set the xlim parameter for both |
| 201 |
trees, which will force the proportion of distances of the |
| 202 |
labels from the trees to remain the same.} |
| 203 |
|
| 204 |
\item{dLeaf_left}{dLeaf of the left dendrogram, by default it is equal to dLeaf (often negative).} |
| 205 |
|
| 206 |
\item{dLeaf_right}{dLeaf of the right dendrogram, by default it is equal to minus dLeaf (often positive).} |
| 207 |
|
| 208 |
\item{axes}{logical (TRUE). Should plot axes be plotted?} |
| 209 |
|
| 210 |
\item{type}{type of plot ("t"/"r" = triangle or rectangle)} |
| 211 |
|
| 212 |
\item{lab.cex}{numeric scalar, influanicing the cex size of the labels.} |
| 213 |
|
| 214 |
\item{remove_nodePar}{logical (FALSE). Should the nodePar of the leaves be |
| 215 |
removed? (useful when the trees' leaves has too many parameters on them)} |
| 216 |
|
| 217 |
\item{main}{Character. Title above the connecting lines.} |
| 218 |
|
| 219 |
\item{sub}{Character. Title below the connecting lines.} |
| 220 |
|
| 221 |
\item{k_labels}{integer. Number of groups by which to color the leaves.} |
| 222 |
|
| 223 |
\item{k_branches}{integer. Number of groups by which to color the branches.} |
| 224 |
|
| 225 |
\item{rank_branches}{logical (FALSE). Should the branches heights be adjusted? |
| 226 |
(setting this to TRUE - can make it easier for |
| 227 |
comparing topological differences)} |
| 228 |
|
| 229 |
\item{hang}{logical (FALSE). Should we hang the leaves of the trees?} |
| 230 |
|
| 231 |
\item{match_order_by_labels}{logical (TRUE). Should the leaves value order |
| 232 |
be matched between the two trees based on labels? This is a MUST in order |
| 233 |
to have the lines connect the correct labels. Set this to FALSE if you |
| 234 |
want to make the plotting a bit faster, and only after you are sure |
| 235 |
the labels and orders are correctly aligned.} |
| 236 |
|
| 237 |
\item{cex_main}{A numerical value giving the amount by which plotting title |
| 238 |
should be magnified relative to the default.} |
| 239 |
|
| 240 |
\item{cex_main_left}{see cex_main.} |
| 241 |
|
| 242 |
\item{cex_main_right}{see cex_main.} |
| 243 |
|
| 244 |
\item{cex_sub}{see cex_main.} |
| 245 |
|
| 246 |
\item{highlight_distinct_edges}{logical (default is TRUE). If to highlight distinct edges in each tree (by changing their line types to 2). |
| 247 |
(notice that this can be slow on large trees) |
| 248 |
|
| 249 |
This parameter will automatically be turned off if the tree already comes with a "lty" edgePar |
| 250 |
(this is checked using \link{has_edgePar}). A "lty" can be removed by using set("clear_branches"), by |
| 251 |
removing all of the edgePar parameters of the dendrogram.} |
| 252 |
|
| 253 |
\item{common_subtrees_color_lines}{logical (default is TRUE). color the connecting line based on the common subtrees of both dends. |
| 254 |
This only works if |
| 255 |
(notice that this can be slow on large trees)} |
| 256 |
|
| 257 |
\item{common_subtrees_color_lines_default_single_leaf_color}{When representing edges between common subtrees |
| 258 |
(i.e. common_subtrees_color_branches = TRUE), this parameter sets the color of edges for subtrees that are NOT common. |
| 259 |
Default is "grey"} |
| 260 |
|
| 261 |
\item{common_subtrees_color_branches}{logical (default is FALSE). |
| 262 |
Color the branches of both dends based on the common subtrees. |
| 263 |
(notice that this can be slow on large trees) |
| 264 |
This is FALSE by default since it will override the colors of the existing tree.} |
| 265 |
|
| 266 |
\item{highlight_branches_col}{logical (default is FALSE). Should \link{highlight_branches_col} be used on the dendrograms. |
| 267 |
|
| 268 |
This parameter will automatically be turned off if the tree already comes with a "col" edgePar |
| 269 |
(this is checked using \link{has_edgePar}). A "lty" can be removed by using set("clear_branches"), by |
| 270 |
removing all of the edgePar parameters of the dendrogram.} |
| 271 |
|
| 272 |
\item{highlight_branches_lwd}{logical (default is TRUE). Should \link{highlight_branches_lwd} be used on the dendrograms. |
| 273 |
|
| 274 |
This parameter will automatically be turned off if the tree already comes with a "lwd" edgePar |
| 275 |
(this is checked using \link{has_edgePar}). A "lty" can be removed by using set("clear_branches"), by |
| 276 |
removing all of the edgePar parameters of the dendrogram.} |
| 277 |
|
| 278 |
\item{faster}{logical (FALSE). If TRUE, it overrides some other parameters to |
| 279 |
have them turned off so that the plotting will go a tiny bit faster.} |
| 280 |
} |
| 281 |
\value{ |
| 282 |
An invisible \link{dendlist}, with two trees after being |
| 283 |
modified during the creation of the tanglegram. |
| 284 |
} |
| 285 |
\description{ |
| 286 |
Plots a tanglegram plot of a side by side trees. |
| 287 |
} |
| 288 |
\details{ |
| 289 |
Notice that tanglegram does not "resize" well. In case you are resizing your |
| 290 |
window you would need to re-run the function. |
| 291 |
} |
| 292 |
\examples{ |
| 293 |
\dontrun{ |
| 294 |
set.seed(23235) |
| 295 |
ss <- sample(1:150, 10) |
| 296 |
dend1 <- iris[ss, -5] \%>\% |
| 297 |
dist() \%>\% |
| 298 |
hclust("com") \%>\% |
| 299 |
as.dendrogram() |
| 300 |
dend2 <- iris[ss, -5] \%>\% |
| 301 |
dist() \%>\% |
| 302 |
hclust("sin") \%>\% |
| 303 |
as.dendrogram() |
| 304 |
dend12 <- dendlist(dend1, dend2) |
| 305 |
|
| 306 |
dend12 \%>\% tanglegram() |
| 307 |
|
| 308 |
tanglegram(dend1, dend2) |
| 309 |
tanglegram(dend1, dend2, sort = TRUE) |
| 310 |
tanglegram(dend1, dend2, remove_nodePar = TRUE) |
| 311 |
tanglegram(dend1, dend2, k_labels = 6, k_branches = 4) |
| 312 |
|
| 313 |
tanglegram(dend1, dend2, |
| 314 |
lab.cex = 2, edge.lwd = 3, |
| 315 |
margin_inner = 5, type = "t", center = TRUE |
| 316 |
) |
| 317 |
|
| 318 |
|
| 319 |
## works nicely: |
| 320 |
tanglegram(dend1, dend2, |
| 321 |
lab.cex = 2, edge.lwd = 3, |
| 322 |
margin_inner = 3.5, type = "t", center = TRUE, |
| 323 |
dLeaf = -0.1, xlim = c(7, 0), |
| 324 |
k_branches = 3 |
| 325 |
) |
| 326 |
|
| 327 |
|
| 328 |
# using rank_branches can make the comparison even easier |
| 329 |
tanglegram(rank_branches(dend1), rank_branches(dend2), |
| 330 |
lab.cex = 2, edge.lwd = 3, |
| 331 |
margin_inner = 3.5, type = "t", center = TRUE, |
| 332 |
dLeaf = -0.1, xlim = c(5.1, 0), columns_width = c(5, 1, 5), |
| 333 |
k_branches = 3 |
| 334 |
) |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
######## |
| 339 |
## Nice example of some colored trees |
| 340 |
|
| 341 |
# see the coloring of common sub trees: |
| 342 |
set.seed(23235) |
| 343 |
ss <- sample(1:150, 10) |
| 344 |
dend1 <- iris[ss, -5] \%>\% |
| 345 |
dist() \%>\% |
| 346 |
hclust("com") \%>\% |
| 347 |
as.dendrogram() |
| 348 |
dend2 <- iris[ss, -5] \%>\% |
| 349 |
dist() \%>\% |
| 350 |
hclust("sin") \%>\% |
| 351 |
as.dendrogram() |
| 352 |
dend12 <- dendlist(dend1, dend2) |
| 353 |
# dend12 \%>\% untangle \%>\% tanglegram |
| 354 |
dend12 \%>\% tanglegram(common_subtrees_color_branches = TRUE) |
| 355 |
|
| 356 |
|
| 357 |
set.seed(22133513) |
| 358 |
ss <- sample(1:150, 10) |
| 359 |
dend1 <- iris[ss, -5] \%>\% |
| 360 |
dist() \%>\% |
| 361 |
hclust("com") \%>\% |
| 362 |
as.dendrogram() |
| 363 |
dend2 <- iris[ss, -5] \%>\% |
| 364 |
dist() \%>\% |
| 365 |
hclust("sin") \%>\% |
| 366 |
as.dendrogram() |
| 367 |
dend12 <- dendlist(dend1, dend2) |
| 368 |
# dend12 \%>\% untangle \%>\% tanglegram |
| 369 |
dend12 \%>\% tanglegram(common_subtrees_color_branches = TRUE) |
| 370 |
dend12 \%>\% tanglegram() |
| 371 |
} |
| 372 |
} |
| 373 |
\seealso{ |
| 374 |
\link{remove_leaves_nodePar}, \link{plot_horiz.dendrogram}, \link{rank_branches}, |
| 375 |
\link{hang.dendrogram} |
| 376 |
} |
| 377 |
\author{ |
| 378 |
Tal Galili, Johan Renaudie |
| 379 |
} |