2013-12-17 24 views
6

私は(部分的に)ggplotを使用して、パッケージade4s.class(...) throught利用できるクラスタプロットを再現するが、この質問は実際にははるかに一般的であるにしようとしています。ggplotを使って「星」プロットを作成する方法はありますか?

NB:This questionは「スタープロット」を意味するが、実際にはクモのプロットを説明します。

df  <- mtcars[,c(1,3,4,5,6,7)] 
pca <-prcomp(df, scale.=T, retx=T) 
scores <-data.frame(pca$x) 

library(ade4) 
km <- kmeans(df,centers=3) 
plot.df <- cbind(scores$PC1, scores$PC2) 
s.class(plot.df, factor(km$cluster)) 

私が探している本質的な特徴は、 "星"、例えばあります共通点(ここではクラスタ重心)から他のいくつかの点(ここではクラスタ内の点)まで放射する線の集合。

ggplotパッケージを使用していることを実行する方法はありますか? ggplotから直接アクセスできない場合は、誰でもアドインが動作することを知っていますか?例えば、stat_ellipse(...)には、ggplotパッケージ(here、およびhere)の一部ではないいくつかのバリエーションがあります。

+0

[**この**](http://stackoverflow.com/questions/18039313/pca-scaling-with-ggbiplot)、[**この**](のhttp:// stackoverflowの.com/questions/6578355/plotting-pca-biplot-with-ggplot2/9850141#9850141)と[** this **](http://stackoverflow.com/questions/11484133/structure-diagram-where-each-グループに所属しているメンバーがセンターに接続されていて、すべてのCLに接続されている)が役に立つ場合があります。 – Henrik

+0

もう一度おねがいします!ですから、 'geom_segment(...)'が答えです。私はなぜドキュメントでそれを見ていないのかわかりません。 – jlhoward

答えて

3

ここでの難しさは、プロット自体ではなくデータを作成することです。あなたはパッケージのコードを調べて、それがあなたの役に立つものを抽出する必要があります。

enter image description here

dfxy <- plot.df 
df <- data.frame(dfxy) 
x <- df[, 1] 
y <- df[, 2] 

fac <- factor(km$cluster) 
f1 <- function(cl) { 
    n <- length(cl) 
    cl <- as.factor(cl) 
    x <- matrix(0, n, length(levels(cl))) 
    x[(1:n) + n * (unclass(cl) - 1)] <- 1 
    dimnames(x) <- list(names(cl), levels(cl)) 
    data.frame(x) 
} 
wt = rep(1, length(fac)) 
dfdistri <- f1(fac) * wt 
w1 <- unlist(lapply(dfdistri, sum)) 
dfdistri <- t(t(dfdistri)/w1) 

## create a data.frame 
cstar=2 
ll <- lapply(seq_len(ncol(dfdistri)),function(i){ 
    z1 <- dfdistri[,i] 
    z <- z1[z1>0] 
    x <- x[z1>0] 
    y <- y[z1>0] 
    z <- z/sum(z) 
    x1 <- sum(x * z) 
    y1 <- sum(y * z) 
    hx <- cstar * (x - x1) 
    hy <- cstar * (y - y1) 
    dat <- data.frame(x=x1, y=y1, xend=x1 + hx, yend=y1 + hy,center=factor(i)) 
}) 

dat <- do.call(rbind,ll) 
library(ggplot2) 
ggplot(dat,aes(x=x,y=y))+ 
    geom_point(aes(shape=center)) + 
    geom_segment(aes(yend=yend,xend=xend,color=center,group=center)) 
6

この答えは、@ agstudy者に基づいて応答し、@ヘンリックさんのコメントに作られた提案:これは良いスタートでなければなりません。投稿が短く、質問に直接適用できるので投稿してください。

ボトムラインはこれです:スタープロットは容易geom_segment(...)を使用してggplotで作られています。質問からDF、PCA、スコア、およびキロを使用して:

# build ggplot dataframe with points (x,y) and corresponding groups (cluster) 
gg <- data.frame(cluster=factor(km$cluster), x=scores$PC1, y=scores$PC2) 
# calculate group centroid locations 
centroids <- aggregate(cbind(x,y)~cluster,data=gg,mean) 
# merge centroid locations into ggplot dataframe 
gg <- merge(gg,centroids,by="cluster",suffixes=c("",".centroid")) 
# generate star plot... 
ggplot(gg) + 
    geom_point(aes(x=x,y=y,color=cluster), size=3) + 
    geom_point(data=centroids, aes(x=x, y=y, color=cluster), size=4) + 
    geom_segment(aes(x=x.centroid, y=y.centroid, xend=x, yend=y, color=cluster)) 

結果がs.class(...)で得られたものと同一です。

関連する問題