5
votes

I am trying to create a dendrogram using the package dendextend. It creates really nice gg dendrograms but unfortunately when you turn it into a "circle", the labels do not keep up. I'll provide an example below.

My distance object is here: http://speedy.sh/JRVBS/mydist.RDS

library(dendextend)
library(ggplot2)
#library(devtools) ; install_github('kassambara/factoextra')
library(factoextra)


clus <- hcut(mydist, k = 6, hc_func = 'hclust', 
             hc_method = 'ward.D2', graph = FALSE, isdiss = TRUE)

dend <- as.dendrogram(clus)
labels(dend) <- paste0(paste0(rep(' ', 3), collapse = ''), labels(dend))
dend <- sort(dend, decreasing = FALSE)

ggd1 <- ggplot(dend %>%
                   set('branches_k_color', k = 6) %>%
                   set('branches_lwd', 0.6) %>%
                   set('labels_colors', k = 6) %>%
                   set('labels_cex', 0.6), 
               theme = theme_minimal(),
               horiz = TRUE)
ggd1 <- ggd1 + theme(panel.grid.major = element_blank(),
                     axis.text = element_blank(),
                     axis.title = element_blank())
ggd1 <- ggd1 + ylim(max(get_branches_heights(dend)), -3)

This basically gives me this image: enter image description here Which is great. However, I want to turn this into a circle, and so use:

ggd1 + coord_polar(theta = 'x') 

And I get this graph below. This is close to exactly what I want, but I just need to rotate the labels. enter image description here

Any help is appreciated. I know that under the hood dendextend is basically creating a few data.frames and then calling geom_segment() and geom_text() on them to create the dendrogram and labels. I believe I can expose the associated data.frame as follows:

back.df1 <- dendextend::as.ggdend(dend)
back.df2 <- dendextend::prepare.ggdend(back.df1)

Another tactic would possibly to be to use ggplot(labels = FALSE...) when plotting, and then to add geom_text() manually in some way that preserves the coloring but allows me to use geom_text(angle = ).

I also suspect some combination of various ggplot wizardry would allow me to take back.df2 and create the 1st and second plots again, but also control the angle of the labels. However, I do not know how to do any of this, and have built out a lot already using the dendextend package and would ideally like to avoid having to use any new package for creating dendrogram objects because I really like this outside of the labels!


SOLUTION

I based this off the solution from Richard Telford below. I first created an edited version of the ggplot.ggdend(). This is identical to the one provided in the answer below. I next created a function to automatically create the angle and hjust vectors so that the labels rotation switches from 6 o'clock to 12 o'clock to improve readability.

createAngleHJustCols <- function(labeldf) {        
    nn <- length(labeldf$y)
    halfn <- floor(nn/2)
    firsthalf <- rev(90 + seq(0,360, length.out = nn))
    secondhalf <- rev(-90 + seq(0,360, length.out = nn))
    angle <- numeric(nn)
    angle[1:halfn] <- firsthalf[1:halfn]
    angle[(halfn+1):nn] <- secondhalf[(halfn+1):nn]

    hjust <- numeric(nn)
    hjust[1:halfn] <- 0
    hjust[(halfn+1):nn] <- 1

    return(list(angle = angle, hjust = hjust))
}

I then produced the plot using the following code:

gdend <- dendextend::as.ggdend(dend %>%
                                   set('branches_k_color', k = 6) %>%
                                   set('branches_lwd', 0.6) %>%
                                   set('labels_colors', k = 6) %>%
                                   set('labels_cex', 0.6))

gdend$labels$angle <- ifelse(horiz, 0, 90)
gdend$labels$hjust <- 0
gdend$labels$vjust <- 0.5

# if polar, change the angle and hjust so that the labels rotate
if(polarplot) {
    newvalues <- createAngleHJustCols(gdend$labels)
    gdend$labels$angle <- newvalues[['angle']]
    gdend$labels$hjust <- newvalues[['hjust']]
}

ggresult <- newggplot.ggdend(gdend, horiz = TRUE, offset_labels = -2) 
ggresult <- ggresult + ggtitle(plottitle)
ggresult <- ggresult + theme(plot.margin = margin(c(2,2,2,2),
                             axis.text = element_blank(),
                             plot.title = element_text(margin = margin(10,2,2,2)))
ggresult <- ggresult + ylim(max(get_branches_heights(dend)), -5)
ggresult <- ggresult + coord_polar(theta = 'x', direction = 1)

And that ultimately produced this final plot! enter image description here

(I changed a couple things in the data so some of the order may appear different in the plot)

1
Your RDS file seems to be corrupt. load("mydist.RDS") Error: bad restore file magic number (file may be corrupted) -- no data loaded. May be better to use dput and include in your question (unless object is huge)Richard Telford
It is unfortunately huge. Thanks for your comment/answer, I'm going to try that. Also, I reuploaded 'mydist.RDS', I believe I may have made a mistake before.Brandon

1 Answers

3
votes

This is possible, but you need to edit dendextend:::ggplot.ggdend first to make it accept the angle aesthetic (and also hjust and vjust)

Step 1: edit dendextend:::ggplot.ggdend

newggplot.ggdend <- function (data, segments = TRUE, labels = TRUE, nodes = TRUE, 
          horiz = FALSE, theme = theme_dendro(), offset_labels = 0, ...) {
  data <- prepare.ggdend(data)
  #angle <- ifelse(horiz, 0, 90)
  #hjust <- ifelse(horiz, 0, 1)
  p <- ggplot()
  if (segments) {
    p <- p + geom_segment(data = data$segments, aes_string(x = "x", y = "y", xend = "xend", yend = "yend", colour = "col", linetype = "lty", size = "lwd"), lineend = "square") + 
      guides(linetype = FALSE, col = FALSE) + scale_colour_identity() + 
      scale_size_identity() + scale_linetype_identity()
  }
  if (nodes) {
    p <- p + geom_point(data = data$nodes, aes_string(x = "x", y = "y", colour = "col", shape = "pch", size = "cex")) + 
      guides(shape = FALSE, col = FALSE, size = FALSE) + 
      scale_shape_identity()
  }
  if (labels) {
    data$labels$cex <- 5 * data$labels$cex
    data$labels$y <- data$labels$y + offset_labels
    p <- p + geom_text(data = data$labels, aes_string(x = "x", y = "y", label = "label", colour = "col", size = "cex", angle = "angle", hjust = "hjust", vjust = "vjust"))#edited
  }
  if (horiz) {
    p <- p + coord_flip() + scale_y_reverse(expand = c(0.2, 0))
  }
  if (!is.null(theme)) {
    p <- p + theme
  }
  p
}

assignInNamespace(x = "ggplot.ggdend", ns = "dendextend", value = newggplot.ggdend)

Step 2: Make the data object

gdend <- dendextend::as.ggdend(dend %>%
                        set('branches_k_color', k = 6) %>%
                        set('branches_lwd', 0.6) %>%
                        set('labels_colors', k = 6) %>%
                        set('labels_cex', 0.6),
                      theme = theme_minimal(),
                      horiz = TRUE)
gdend$labels$angle <- seq(90, -270, length = nrow(gdend$labels))
gdend$labels$vjust <- cos(gdend$labels$angle * pi) / (180)
gdend$labels$hjust <- sin(gdend$labels$angle * pi) / (180)

Step 3: plot

ggd1 <- ggplot(gdend)
ggd1 <- ggd1 + theme(panel.grid.major = element_blank(),
                     axis.text = element_blank(),
                     axis.title = element_blank())
ggd1 <- ggd1 + ylim(max(get_branches_heights(dend)), -3)
ggd1
ggd1 + coord_polar(theta = 'x')