r - X axis label is not showing in clustering dendrogram in ggplot -
i have done clustering dendrogram following previous code found online, x-axis of not being shown in graph. have dissimilarity value shown in x-axis, have not been successful.
females<-cervidae[cervidae$sex=="female",] dstf <- daisy(females[,9:14], metric = "euclidean", stand = false) hcaf <- hclust(dstf, method = "ave") k <- 3 clustf <- cutree(hcaf,k=k) # k clusters dendrf <- dendro_data(hcaf, type="rectangle") # convert ggplot clust.dff <- data.frame(label=rownames(females), cluster=factor(clustf), females$genus, females$species) dendrf[["labels"]] <- merge(dendrf[["labels"]],clust.dff, by="label") rectf <- aggregate(x~cluster,label(dendrf),range) rectf <- data.frame(rectf$cluster,rectf$x) ymax <- mean(hcaf$height[length(hcaf$height)-((k-2):(k-1))]) fem=ggplot() + geom_segment(data=segment(dendrf), aes(x=x, y=y, xend=xend, yend=yend)) + geom_text(data=label(dendrf), aes(x, y, label= females.genus, hjust=0, color=females.genus), size=3) + geom_rect(data=rectf, aes(xmin=x1-.3, xmax=x2+.3, ymin=0, ymax=ymax), color="red", fill=na)+ coord_flip() + scale_y_reverse(expand=c(0.2, 0)) + theme_dendro() + scale_color_discrete(name="genus") + theme(legend.position="none") here how dendrogram looks:
your code included theme_dendro(), described in file as:
sets of ggplot options blank, returning blank theme elements panel grid, panel background, axis title, axis text, axis line , axis ticks.
you force x-axis line / text / ticks visible in theme():
ggplot() + geom_segment(data=segment(dendrf), aes(x=x, y=y, xend=xend, yend=yend)) + geom_text(data=label(dendrf), aes(x, y, label= label, hjust=0, color=cluster), size=3) + geom_rect(data=rectf, aes(xmin=x1-.3, xmax=x2+.3, ymin=0, ymax=ymax), color="red", fill=na)+ coord_flip() + scale_y_reverse(expand=c(0.2, 0)) + theme_dendro() + scale_color_discrete(name="cluster") + theme(legend.position="none", axis.text.x = element_text(), # show x-axis labels axis.ticks.x = element_line(), # show x-axis tick marks axis.line.x = element_line()) # show x-axis lines (this demonstration uses built-in dataset, since i'm not sure what's cervidae. code used create reproduced below:)
library(cluster); library(ggdendro); library(ggplot2) hcaf <- hclust(dist(usarrests), "ave") k <- 3 clustf <- cutree(hcaf,k=k) # k clusters dendrf <- dendro_data(hcaf, type="rectangle") # convert ggplot clust.dff <- data.frame(label=rownames(usarrests), cluster=factor(clustf)) dendrf[["labels"]] <- merge(dendrf[["labels"]],clust.dff, by="label") rectf <- aggregate(x~cluster,label(dendrf),range) rectf <- data.frame(rectf$cluster,rectf$x) ymax <- mean(hcaf$height[length(hcaf$height)-((k-2):(k-1))]) 

Comments
Post a Comment