[英]How to rotate a ggplot2 dendrogram?
假设我有一个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()
如何将其顺时针旋转90度? 我在coord_flip()中找到了一些主题,但是我只是想旋转而不是翻转。 我尝试了geom_segment(aes(x=y, y=x, xend=yend, yend=xend))
,但是它不起作用。 这是我想要的情节:
将x
和xend
用于y值,然后将y
和yend
用于y值。 使用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()
使用原始代码可以实现相同的效果,但是添加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.