简体   繁体   English

如何旋转ggplot2树状图?

[英]How to rotate a ggplot2 dendrogram?

Suppose I have a ggplot2 dendrogram like this: 假设我有一个ggplot2树状图,如下所示:

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? 如何将其顺时针旋转90度? I found a few topics in coord_flip(), but I just want to rotate rather flip. 我在coord_flip()中找到了一些主题,但是我只是想旋转而不是翻转。 I tried geom_segment(aes(x=y, y=x, xend=yend, yend=xend)) , but it does not work. 我尝试了geom_segment(aes(x=y, y=x, xend=yend, yend=xend)) ,但是它不起作用。 Here is the plot that I want to: 这是我想要的情节:

在此处输入图片说明

Use x and xend for y values and then y and yend for y values. xxend用于y值,然后将yyend用于y值。 With scale_y_reverse() you will get reversed order clusters. 使用scale_y_reverse()您将获得反向订单集群。

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. 使用原始代码可以实现相同的效果,但是添加coord_flip()旋转90度,然后添加scale_x_reverse()以获得相反的顺序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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