I am trying to create a dendrogram using the dendextend package. It creates really nice gg dendrograms, but, unfortunately, when you turn it into a "circle", the tags don't have time. I will give 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)
It basically gives me this image:
It's great. However, I want to turn this into a circle, and therefore use:
ggd1 + coord_polar(theta = 'x')
And I get this graph below. This is close to what I want, but I just need to rotate the labels.

. , dendextend data.frames, geom_segment() geom_text() . , data.frame :
back.df1 <- dendextend::as.ggdend(dend)
back.df2 <- dendextend::prepare.ggdend(back.df1)
, , ggplot(labels = FALSE...) , geom_text() , , geom_text(angle = ).
, - ggplot back.df2 1- 2- , . , , , , dendextend, - dendrogram, !
. ggplot.ggdend(). , . , 6 12 , .
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))
}
, :
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(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)
!

( , )