Learn R Programming

dendextend (version 1.3.0)

tanglegram: Tanglegram plot

Description

Plots a tanglegram plot of a side by side trees.

Usage

tanglegram(dend1, ...)
"tanglegram"(dend1, dend2 , sort = FALSE, color_lines, lwd = 3.5, edge.lwd = NULL, columns_width = c(5,3,5), margin_top = 3, margin_bottom = 2.5, margin_inner = 3, margin_outer = 0.5, left_dendo_mar = c(margin_bottom,margin_outer,margin_top,margin_inner), right_dendo_mar = c(margin_bottom,margin_inner,margin_top,margin_outer), intersecting = TRUE, dLeaf = NULL, dLeaf_left = dLeaf, dLeaf_right = dLeaf, axes = TRUE, type = "r", # can also be "t" lab.cex = NULL, remove_nodePar =FALSE, main = "", main_left = "", main_right = "", sub = "", k_labels = NULL, k_branches = NULL, rank_branches = FALSE, hang = FALSE, match_order_by_labels = TRUE, cex_main = 2, cex_main_left = cex_main, cex_main_right = cex_main, cex_sub = cex_main, highlight_distinct_edges = TRUE, common_subtrees_color_lines = TRUE, common_subtrees_color_branches = FALSE, faster = FALSE, ...)
"tanglegram"(dend1, which = c(1L,2L), main_left, main_right, ...)
"tanglegram"(dend1, ...)
"tanglegram"(dend1, ...)

Arguments

dend1
tree object (dendrogram/dendlist/hclust/phylo), plotted on the left
...
not used.
dend2
tree object (dendrogram/hclust/phylo), plotted on the right
which
an integer vector of length 2, indicating which of the trees in the dendlist object should be plotted
sort
logical (FALSE). Should the dendrogram's labels be "sorted"? (might give a better tree in some cases).
color_lines
a vector of colors for the lines connected the labels. If the colors are shorter than the number of labels, they are recycled (and a warning is issued). The colors in the vector are applied on the lines from the bottom up.
lwd
width of the lines connecting the labels. (default is 3.5)
edge.lwd
width of the dendrograms lines.
columns_width
a vector with three elements, giving the relative sizes of the the three plots (left dendrogram, connecting lines, right dendrogram). This is passed to layout. The default is: c(5,3,5)
margin_top
the number of lines of margin to be specified on the top of the plots.
margin_bottom
the number of lines of margin to be specified on the bottom of the plots.
margin_inner
margin_bottom the number of lines of margin to be specified on the inner distence between the dendrograms and the connecting lines.
margin_outer
margin_bottom the number of lines of margin to be specified on the outer distence between the dendrograms and the connecting lines.
left_dendo_mar
mar parameters of the left dendrgoram.
right_dendo_mar
mar parameters of the right dendrgoram.
intersecting
logical (TRUE). Should the leaves of the two dendrograms be pruned so that the two trees will have the same labels?
dLeaf
a number specifying the distance in user coordinates between the tip of a leaf and its label. If NULL, as per default, 3/4 of a letter width or height is used.

Notice that if we are comparing two dendrograms with different heights, manually changing dLeaf will affect both trees differently. In such a case, it is recommanded to manually change dLeaf_left and dLeaf_right. This can be especially important when changing the lab.cex of the dendrogram's labels. Alternatively, one could manually set the xlim parameter for both trees, which will force the proportion of distances of the labels from the trees to remain the same.

dLeaf_left
dLeaf of the left dendrogram, by default it is equal to dLeaf (often negative).
dLeaf_right
dLeaf of the right dendrogram, by default it is equal to minus dLeaf (often positive).
axes
logical (TRUE). Should plot axes be plotted?
type
type of plot ("t"/"r" = triangle or rectangle)
lab.cex
numeric scalar, influanicing the cex size of the labels.
remove_nodePar
logical (FALSE). Should the nodePar of the leaves be removed? (useful when the trees' leaves has too many parameters on them)
main
Character. Title above the connecting lines.
main_left
Character. Title of the left dendrogram.
main_right
Character. Title of the right dendrogram.
sub
Character. Title below the connecting lines.
k_labels
integer. Number of groups by which to color the leaves.
k_branches
integer. Number of groups by which to color the branches.
rank_branches
logical (FALSE). Should the branches heights be adjusted? (setting this to TRUE - can make it easier for comparing topological differences)
hang
logical (FALSE). Should we hang the leaves of the trees?
match_order_by_labels
logical (TRUE). Should the leaves value order be matched between the two trees based on labels? This is a MUST in order to have the lines connect the correct labels. Set this to FALSE if you want to make the plotting a bit faster, and only after you are sure the labels and orders are correctly aligned.
cex_main
A numerical value giving the amount by which plotting title should be magnified relative to the default.
cex_main_left
see cex_main.
cex_main_right
see cex_main.
cex_sub
see cex_main.
highlight_distinct_edges
logical (default is TRUE). If to highlight distinct edges in each tree (by changing their line types to 2). (notice that this can be slow on large trees)
common_subtrees_color_lines
logical (default is TRUE). color the connecting line based on the common subtrees of both dends. This only works if (notice that this can be slow on large trees)
common_subtrees_color_branches
logical (default is FALSE). Color the branches of both dends based on the common subtrees. (notice that this can be slow on large trees) This is FALSE by default since it will override the colors of the existing tree.
faster
logical (FALSE). If TRUE, it overrides some other parameters to have them turned off so that the plotting will go a tiny bit faster.

Value

An invisible dendlist, with two trees after being modified during the creation of the tanglegram.

Source

The function is based on code from Johan Renaudie (plannapus), after major revisions. See: http://stackoverflow.com/questions/12456768/duelling-dendrograms-in-r-placing-dendrograms-back-to-back-in-r As far as I could tell, this code was originally inspired by Dylan Beaudette function dueling.dendrograms from the sharpshootR package: http://cran.at.r-project.org/web/packages/sharpshootR/ tanglegram

Details

Notice that tanglegram does not "resize" well. In case you are resizing your window you would need to re-run the function.

See Also

remove_leaves_nodePar, plot_horiz.dendrogram, rank_branches, hang.dendrogram

Examples

Run this code
## Not run: 
# set.seed(23235)
# ss <- sample(1:150, 10 )
# dend1 <- iris[ss,-5] %>% dist %>% hclust("com") %>% as.dendrogram
# dend2 <- iris[ss,-5] %>% dist %>% hclust("sin") %>% as.dendrogram
# dend12 <- dendlist(dend1, dend2)
# 
# dend12 %>% tanglegram
# 
# tanglegram(dend1 , dend2)
# tanglegram(dend1 , dend2, sort = TRUE)
# tanglegram(dend1 , dend2, remove_nodePar = TRUE)
# tanglegram(dend1 , dend2, k_labels = 6, k_branches = 4)
# 
# tanglegram(dend1 , dend2, lab.cex = 2, edge.lwd = 3,
# margin_inner= 5, type = "t", center = TRUE)
# 
# 
# ## works nicely:
# tanglegram(dend1 , dend2, lab.cex = 2, edge.lwd = 3,  
# margin_inner= 3.5, type = "t", center = TRUE,
# dLeaf = -0.1, xlim = c(7,0),
# k_branches=3)
# 
# 
# # using rank_branches can make the comparison even easier
# tanglegram(rank_branches(dend1) , rank_branches(dend2), lab.cex = 2, edge.lwd = 3,  
#  margin_inner= 3.5, type = "t", center = TRUE,
#   dLeaf = -0.1, xlim = c(5.1,0), columns_width= c(5,1,5),
#    k_branches=3)
# 
# 
# 
# ########
# ## Nice example of some colored trees
# 
# # see the coloring of common sub trees:
# set.seed(23235)
# ss <- sample(1:150, 10 )
# dend1 <- iris[ss,-5] %>% dist %>% hclust("com") %>% as.dendrogram
# dend2 <- iris[ss,-5] %>% dist %>% hclust("sin") %>% as.dendrogram
# dend12 <- dendlist(dend1, dend2)
# # dend12 %>% untangle %>% tanglegram
# dend12 %>% tanglegram(common_subtrees_color_branches = TRUE)
# 
# 
# set.seed(22133513)
# ss <- sample(1:150, 10 )
# dend1 <- iris[ss,-5] %>% dist %>% hclust("com") %>% as.dendrogram
# dend2 <- iris[ss,-5] %>% dist %>% hclust("sin") %>% as.dendrogram
# dend12 <- dendlist(dend1, dend2)
# # dend12 %>% untangle %>% tanglegram
# dend12 %>% tanglegram(common_subtrees_color_branches = TRUE)
# dend12 %>% tanglegram
# 
# ## End(Not run)

Run the code above in your browser using DataLab