if (FALSE) {
library(dendextend)
# library(ggdendro)
# Create a complex dend:
dend <- iris[1:30, -5] %>%
dist() %>%
hclust() %>%
as.dendrogram() %>%
set("branches_k_color", k = 3) %>%
set("branches_lwd", c(1.5, 1, 1.5)) %>%
set("branches_lty", c(1, 1, 3, 1, 1, 2)) %>%
set("labels_colors") %>%
set("labels_cex", c(.9, 1.2))
# plot the dend in usual "base" plotting engine:
plot(dend)
# Now let's do it in ggplot2 :)
ggd1 <- as.ggdend(dend)
library(ggplot2)
ggplot(ggd1) # reproducing the above plot in ggplot2 :)
# Triangle version:
plot(dend, type = "triangle")
ggd2 <- as.ggdend(dend, type = "triangle")
ggplot(ggd2)
# More modifications:
labels(dend) <- paste0(labels(dend), "00000")
ggd1 <- as.ggdend(dend)
# Use ylim to deal with long labels in ggplot2
ggplot(ggd1) + ylim(-.4, max(get_branches_heights(dend)))
ggplot(ggd1, horiz = TRUE) # horiz plot in ggplot2
# Adding some extra spice to it...
# creating a radial plot:
ggplot(ggd1) + scale_y_reverse(expand = c(0.2, 0)) + coord_polar(theta = "x")
# The text doesn't look so great, so let's remove it:
ggplot(ggd1, labels = FALSE) + scale_y_reverse(expand = c(0.2, 0)) + coord_polar(theta = "x")
# This can now be sent to plot.ly - which adds zoom-in abilities, and more.
# Here is how it might look like: https://plot.ly/~talgalili/6/y-vs-x/
## Quick guide:
# install.packages("devtools")
# library("devtools")
# devtools::install_github("ropensci/plotly")
# library(plotly)
# set_credentials_file(...)
# you'll need to get it from here: https://plot.ly/ggplot2/getting-started/
# ggplot(ggd1)
# py <- plotly()
# py$ggplotly()
# And you'll get something like this: https://plot.ly/~talgalili/6/y-vs-x/
# Another example: https://plot.ly/ggplot2/
}
Run the code above in your browser using DataLab