2016-12-09 9 views
1

RのパッケージClustOfVarggdendroを組み合わせて、可変クラスタリングの視覚的要約を得ることです。R:グレープロットをクラスタリングするためのggplotの高さ調整

データに列が少ない場合、結果は非常に優れています(ただし、以下の図で丸で囲んだ部分があります)。例えばmtcarsを使用する:列の数が多い場合は

library(plyr) 
library(ggplot2) 
library(gtable) 
library(grid) 
library(gridExtra) 

library(ClustOfVar) 
library(ggdendro) 

fit = hclustvar(X.quanti = mtcars) 

labels = cutree(fit,k = 5) 

labelx = data.frame(Names=names(labels),group = paste("Group",as.vector(labels)),num=as.vector(labels)) 

p1 = ggdendrogram(as.dendrogram(fit), rotate=TRUE) 

df2<-data.frame(cluster=cutree(fit, k =5), states=factor(fit$labels,levels=fit$labels[fit$order])) 
df3<-ddply(df2,.(cluster),summarise,pos=mean(as.numeric(states))) 

p2 = ggplot(df2,aes(states,y=1,fill=factor(cluster)))+geom_tile()+ 
    scale_y_continuous(expand=c(0,0))+ 
    theme(axis.title=element_blank(), 
     axis.ticks=element_blank(), 
     axis.text=element_blank(), 
     legend.position="none")+coord_flip()+ 
    geom_text(data=df3,aes(x=pos,label=cluster)) 
gp1<-ggplotGrob(p1) 
gp2<-ggplotGrob(p2) 
maxHeight = grid::unit.pmax(gp1$heights[2:5], gp2$heights[2:5]) 
gp1$heights[2:5] <- as.list(maxHeight) 
gp2$heights[2:5] <- as.list(maxHeight) 
grid.arrange(gp2, gp1, ncol=2,widths=c(1/6,5/6)) 

enter image description here

、別の問題が発生します。つまり、カラータイルパーツの高さは、樹状図の高さと一致しません。

library(ClustOfVar) 
library(ggdendro) 
X = data.frame(mtcars,mtcars,mtcars,mtcars,mtcars,mtcars) 

fit = hclustvar(X.quanti = X) 

labels = cutree(fit,k = 5) 

labelx = data.frame(Names=names(labels),group = paste("Group",as.vector(labels)),num=as.vector(labels)) 

p1 = ggdendrogram(as.dendrogram(fit), rotate=TRUE) 

df2<-data.frame(cluster=cutree(fit, k =5), states=factor(fit$labels,levels=fit$labels[fit$order])) 
df3<-ddply(df2,.(cluster),summarise,pos=mean(as.numeric(states))) 

p2 = ggplot(df2,aes(states,y=1,fill=factor(cluster)))+geom_tile()+ 
    scale_y_continuous(expand=c(0,0))+ 
    theme(axis.title=element_blank(), 
     axis.ticks=element_blank(), 
     axis.text=element_blank(), 
     legend.position="none")+coord_flip()+ 
    geom_text(data=df3,aes(x=pos,label=cluster)) 
gp1<-ggplotGrob(p1) 
gp2<-ggplotGrob(p2) 
maxHeight = grid::unit.pmax(gp1$heights[2:5], gp2$heights[2:5]) 
gp1$heights[2:5] <- as.list(maxHeight) 
gp2$heights[2:5] <- as.list(maxHeight) 
grid.arrange(gp2, gp1, ncol=2,widths=c(1/6,5/6)) 

enter image description here

我々は、バージョン3.3.1にアップグレードRを持ってのIF @Sandy Musprattは、実際にこのに優れたソリューションを提供してきました。 R: ggplot slight adjustment for clustering summary

しかし、コーポレートサーバーに展開されているRのバージョンを変更できないため、これらの2つの部分を整列させるための他の回避策があるのだろうかと思います。

+0

ん[以前のバージョン](http://stackoverflow.com/revisions/33358320/1)の作品? – Axeman

+0

はい、ありがとうございます。 – John

答えて

3

私の知る限り、あなたのコードはあまり間違いではありません。問題は、2つのプロットをマージするときに連続スケールを離散スケールに一致させようとしていることです。また、ggdendrogram()はy軸に追加のスペースを追加するように見えます。あなたのための

library(plyr) 
library(ggplot2) 
library(gtable) 
library(grid) 
library(gridExtra) 

library(ClustOfVar) 
library(ggdendro) 

# Data 
X = data.frame(mtcars,mtcars,mtcars,mtcars,mtcars,mtcars) 

# Cluster analysis 
fit = hclustvar(X.quanti = X) 

# Labels data frames 
df2 <- data.frame(cluster = cutree(fit, k =5), 
    states = factor(fit$labels, levels = fit$labels[fit$order])) 
df3 <- ddply(df2, .(cluster), summarise, pos = mean(as.numeric(states))) 

# Dendrogram 
# scale_x_continuous() for p1 should match scale_x_discrete() from p2 
# scale_x_continuous strips off the labels. I grab them from df2 
# scale _y_continuous() puts a little space between the labels and the dendrogram 
p1 <- ggdendrogram(as.dendrogram(fit), rotate = TRUE) + 
    scale_x_continuous(expand = c(0, 0.5), labels = levels(df2$states), breaks = 1:length(df2$states)) + 
    scale_y_continuous(expand = c(0.02, 0)) 

# Tiles and labels 
p2 <- ggplot(df2,aes(states, y = 1, fill = factor(cluster))) + 
    geom_tile() + 
    scale_y_continuous(expand = c(0, 0)) + 
    scale_x_discrete(expand = c(0, 0)) + 
    geom_text(data = df3, aes(x = pos, label = cluster)) + 
    coord_flip() + 
    theme(axis.title = element_blank(), 
     axis.ticks = element_blank(), 
     axis.text = element_blank(), 
     legend.position = "none") 

# Get the ggplot grobs 
gp1 <- ggplotGrob(p1) 
gp2 <- ggplotGrob(p2) 

# Make sure the heights match 
maxHeight <- unit.pmax(gp1$heights, gp2$heights) 
gp1$heights <- as.list(maxHeight) 
gp2$heights <- as.list(maxHeight) 

# Combine the two plots 
grid.arrange(gp2, gp1, ncol = 2,widths = c(1/6, 5/6)) 

enter image description here

関連する問題