The default plot(dend, horiz = TRUE), gives us a dendrogram tree plot with the tips turned right. The current function enables the creation of the same tree, but with the tips turned left. The main challange in doing this is finding the distance of the labels from the leaves tips - which is solved with this function.

plot_horiz.dendrogram(
x,
type = c("rectangle", "triangle"),
center = FALSE,
edge.root = is.leaf(x) || !is.null(attr(x, "edgetext")),
dLeaf = NULL,
horiz = TRUE,
xaxt = "n",
yaxt = "s",
xlim = NULL,
ylim = NULL,
nodePar = NULL,
edgePar = list(),
leaflab = c("perpendicular", "textlike", "none"),
side = TRUE,
text_pos = 2,
...
)

## Source

This function is based on replicating plot.dendrogram. In fact, I'd be happy if in the future, some tweaks could be make to plot.dendrogram, so that it would replace the need for this function.

## Arguments

x

tree object (dendrogram)

type

a character vector with either "rectangle" or "triangle" (passed to plot.dendrogram)

center

logical; if TRUE, nodes are plotted centered with respect to the leaves in the branch. Otherwise (default), plot them in the middle of all direct child nodes.

edge.root

logical; if true, draw an edge to the root node.

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 is used.

horiz

logical indicating if the dendrogram should be drawn horizontally or not. In this function it MUST be TRUE!

xaxt

graphical parameters, or arguments for other methods.

yaxt

graphical parameters, or arguments for other methods.

xlim

(NULL) optional x- and y-limits of the plot, passed to plot.default. The defaults for these show the full dendrogram.

ylim

(NULL) optional x- and y-limits of the plot, passed to plot.default. The defaults for these show the full dendrogram.

nodePar

NULL.

edgePar

list()

leaflab

c("perpendicular", "textlike", "none")

side

logical (TRUE). Should the tips of the drawn tree be facing the left side. This is the important feature of this function.

text_pos

integer from either 1 to 4 (2). Two relevant values are 2 and 4. 2 (default) means that the labels are alligned to the tips of the tree leaves. 4 will have the labels allign to the left, making them look like they were when the tree was on the left side (with leaves tips facing to the right).

...

passed to plot.

## Value

The invisiable dLeaf value.

## Examples

if (FALSE) {
dend <- USArrests[1:10, ] %>%
dist() %>%
hclust() %>%
as.dendrogram()

par(mfrow = c(1, 2), mar = rep(6, 4))
plot_horiz.dendrogram(dend, side = FALSE)
plot_horiz.dendrogram(dend, side = TRUE)
# plot_horiz.dendrogram(dend, side=TRUE, dLeaf= 0)
# plot_horiz.dendrogram(dend, side=TRUE, nodePar = list(pos = 1))
# sadly, lab.pos is not implemented yet,
## so the labels can not be right aligned...

plot_horiz.dendrogram(dend, side = F)
plot_horiz.dendrogram(dend, side = TRUE, dLeaf = 0, xlim = c(100, -10)) # bad
plot_horiz.dendrogram(dend, side = TRUE, text_offset = 0)
plot_horiz.dendrogram(dend, side = TRUE, text_offset = 0, text_pos = 4)
}