用ggplot2重现格型树状图

这是可能重现与ggplot2这个格子图吗?

library(latticeExtra) data(mtcars) x <- t(as.matrix(scale(mtcars))) dd.row <- as.dendrogram(hclust(dist(x))) row.ord <- order.dendrogram(dd.row) dd.col <- as.dendrogram(hclust(dist(t(x)))) col.ord <- order.dendrogram(dd.col) library(lattice) levelplot(x[row.ord, col.ord], aspect = "fill", scales = list(x = list(rot = 90)), colorkey = list(space = "left"), legend = list(right = list(fun = dendrogramGrob, args = list(x = dd.col, ord = col.ord, side = "right", size = 10)), top = list(fun = dendrogramGrob, args = list(x = dd.row, side = "top", size = 10)))) 

在这里输入图像描述

编辑

从2011年8月8日ggdendro , CRAN上提供了ggdendro软件包。还要注意,树状图提取function现在称为dendro_data而不是cluster_data


是的。 但是暂时你将不得不跳过几个箍:

  1. 安装ggdendro软件包(可从CRAN获得)。 这个软件包将从Hclust中绘制出明确的目的,从几种types的聚类方法(包括Hclustdendrogram )中提取聚类信息。
  2. 使用网格graphics创build视口,并alignment三个不同的情节。

在这里输入图像描述

代码:

首先加载库并设置ggplot的数据:

 library(ggplot2) library(reshape2) library(ggdendro) data(mtcars) x <- as.matrix(scale(mtcars)) dd.col <- as.dendrogram(hclust(dist(x))) col.ord <- order.dendrogram(dd.col) dd.row <- as.dendrogram(hclust(dist(t(x)))) row.ord <- order.dendrogram(dd.row) xx <- scale(mtcars)[col.ord, row.ord] xx_names <- attr(xx, "dimnames") df <- as.data.frame(xx) colnames(df) <- xx_names[[2]] df$car <- xx_names[[1]] df$car <- with(df, factor(car, levels=car, ordered=TRUE)) mdf <- melt(df, id.vars="car") 

提取树状图数据并创build图

 ddata_x <- dendro_data(dd.row) ddata_y <- dendro_data(dd.col) ### Set up a blank theme theme_none <- theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.title.x = element_text(colour=NA), axis.title.y = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), axis.line = element_blank() #axis.ticks.length = element_blank() ) ### Create plot components ### # Heatmap p1 <- ggplot(mdf, aes(x=variable, y=car)) + geom_tile(aes(fill=value)) + scale_fill_gradient2() # Dendrogram 1 p2 <- ggplot(segment(ddata_x)) + geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) + theme_none + theme(axis.title.x=element_blank()) # Dendrogram 2 p3 <- ggplot(segment(ddata_y)) + geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) + coord_flip() + theme_none 

使用网格graphics和一些手动alignment来定位页面上的三个图

 ### Draw graphic ### grid.newpage() print(p1, vp=viewport(0.8, 0.8, x=0.4, y=0.4)) print(p2, vp=viewport(0.52, 0.2, x=0.45, y=0.9)) print(p3, vp=viewport(0.2, 0.8, x=0.9, y=0.4)) 

正如本说的,一切皆有可能。 一些支持树状图的工作已经完成。 安德烈·德·弗里斯(Andrie de Vries)提出了一种树木物体的强化方法 但是,您所看到的结果graphics并不美观。

这个瓷砖很容易做到。 对于树状图,我会检查plot.dendrogram (使用getAnywhere )来查看如何计算段的坐标。 提取这些坐标并使用geom_segment绘制树状图。 然后使用视口一起绘制瓷砖和树状图。 抱歉,我不能举一个例子,这是很多工作,为时已晚。

我希望这有帮助

干杯

树状图

疑。 我没有看到ggplot2的索引中的任何函数会build议支持树形图,当这个博客在Sarkar的莱迪思书中集成了一系列插图的翻译时,他无法得到一个ggplot树形图的图例:

http://learnr.wordpress.com/2009/08/10/ggplot2-version-of-figures-in-lattice-multivariate-data-visualization-with-r-part-9/

这些链接为ggplot2中的树形图提供了一个解决scheme:

https://gist.github.com/chr1swallace/4672065

https://github.com/chr1swallace/random-functions/blob/master/R/ggplot-heatmap.R

也是这一个:

垂直alignmentggplot2图

Interesting Posts