简体   繁体   中英

How to rotate a ggplot2 dendrogram?

Suppose I have a ggplot2 dendrogram like this:

require(ggplot2)
require(ggdendro)

hc <- hclust(dist(USArrests), "ave")
dhc <- as.dendrogram(hc)

ddata <- dendro_data(dhc, type="rectangle")

ggplot(segment(ddata),labels=rownames(USArrests))+ 
geom_segment(aes(x=x, y=y, xend=xend, yend=yend))+ 
theme_dendro()

How can I rotate it clockwisely in 90 degree? I found a few topics in coord_flip(), but I just want to rotate rather flip. I tried geom_segment(aes(x=y, y=x, xend=yend, yend=xend)) , but it does not work. Here is the plot that I want to:

在此处输入图片说明

Use x and xend for y values and then y and yend for y values. With scale_y_reverse() you will get reversed order clusters.

ggplot(segment(ddata),labels=rownames(USArrests))+ 
  geom_segment(aes(y=x, x=y, yend=xend, xend=yend))+ 
  theme_dendro()+scale_y_reverse()

The same can be achieved using original code but adding coord_flip() to rotate by 90 degree and then add scale_x_reverse() to get reversed order.

ggplot(segment(ddata),labels=rownames(USArrests))+ 
  geom_segment(aes(x=x, y=y, xend=xend, yend=yend))+ 
  theme_dendro()+coord_flip()+scale_x_reverse()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM