用ggplot2重现格形树状图 [英] Reproducing lattice dendrogram graph with ggplot2
本文介绍了用ggplot2重现格形树状图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
library(latticeExtra)
data(mtcars)这是可能用ggplot2重现这个格子图吗?
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(格子)
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
还要注意树状图提取功能现在称为 dendro_data
而不是 cluster_data
是的。但暂时你将不得不跳过一些箍:
- 安装
ggdendro
包(可从CRAN获得)。该包将从几种类型的集群方法(包括Hclust
和dendrogram
)中提取集群信息,其明确目的是绘制ggplot
。 - 使用网格图形创建视口并对齐三个不同的图。
- Install the
ggdendro
package (available from CRAN). This package will extract the cluster information from several types of cluster methods (includingHclust
anddendrogram
) with the express purpose of plotting inggplot
. - Use grid graphics to create viewports and align three different plots.
ol>
代码:
首先加载库并为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)
提取树状图数据并创建图
ddata_x < - dendro_data(dd.row)
ddata_y< - dendro_data(dd.col)
###设置一个空白主题
theme_none< - theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.title.x = element_text(color = 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()
)
###创建绘图组件###
#热图
p1 < - ggplot(mdf, aes(x = variable,y = car))+
geom_tile(aes(fill = value))+ scale_fill_gradient2()
#树形图1
p2 < - ggp lot(segment(ddata_x))+
geom_segment(aes(x = x,y = y,xend = xend,yend = yend))+
theme_none + theme(axis.title.x = element_blank ))
#树形图2
p3 < - ggplot(segment(ddata_y))+
geom_segment(aes(x = x,y = y,xend = xend,yend = yend))+
coord_flip()+ theme_none
使用网格图形和一些手动对齐将三块地块放在页面上
###绘制图形###
网格。 (p2,vp = viewport(0.52,0.2,x = 0.45,y = 0.4))
print(p1,vp = viewport(0.8,0.8,x = 0.4,y = 0.9))
print(p3,vp = viewport(0.2,0.8,x = 0.9,y = 0.4))
Is this possible to reproduce this lattice plot with 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))))
解决方案
EDIT
From 8 August 2011 the ggdendro
package is available on CRAN
Note also that the dendrogram extraction function is now called dendro_data
instead of cluster_data
Yes, it is. But for the time being you will have to jump through a few hoops:
The code:
First load the libraries and set up the data for 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")
Extract dendrogram data and create the plots
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
Use grid graphics and some manual alignment to position the three plots on the page
### 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))
这篇关于用ggplot2重现格形树状图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文