如何用FactoMineR创建双标图? [英] How to create a biplot with FactoMineR?
问题描述
plot.PCA(pca1,喜爱将= IND / VAR)。
我知道用princomp()的结果很容易做到,但我真的很喜欢FactoMineR处理NA,在许多方面对我来说似乎更容易。
有没有办法?我看到它用 ggplot2 完成,但只有结果princomp(),我不知道如何更改代码,以便它可以与PCA()一起使用。
我还看到了一个解决方案,用ggplot2分别进行单独和变量绘制(看看下面的),但我该如何结合这些?
也许这个解决方案在第一个链接中有点不明白,但是我真的不明白:/。
我希望我明白了!
>问候
Lukas
您好您可以修改您的第一个链接的代码对于来自 FactoMineR
的PCA对象,如下所示:
PCbiplot2 < - 函数(res.pca,x =Dim.1,y =Dim.2){
if(!require(ggplot2))install.packages(ggplot2)
#res.pca作为PCA对象
data< - data.frame(obsnames = row.names(res.pca $ ind $ coord),res.pca $ ind $ coord)
plot< - ggpl ot(data,aes_string(x = x,y = y))+ geom_text(alpha = .4,size = 3,aes(label = obsnames))
plot < - plot + geom_hline(aes(0) ,size = .2)+ geom_vline(aes(0),size = 0.2)
datapc< - data.frame(varnames = rownames(res.pca $ var $ coord),res.pca $ var $ (数据[,y]) - 最小(数据[,y])/(最大(datapc [,y]) - 最小(datapc [ (max(data [,x]) - min(data [,x])/(max(datapc [,x]) - min(datapc [,x])))$ b(b)
datapc< - transform(datapc,
v1 = .7 * mult *(get(x)),
v2 = .7 * mult * )
)
plot< - plot + coord_equal()+ geom_text(data = datapc,aes(x = v1,y = v2,label = varnames),size = 5,vjust = 1,color =red)
plot < - plot + geom_segment(data = datapc,aes(x = 0,y = 0,xend = v1,yend = v2),arrow = arrow cm)),alpha = 0.75,color =red)
plot
}
library(FactoMineR)
fit2< - PCA(USArrests,图= F)
PCbiplot2(fit2)
告诉我它是否有效!
编辑:添加jlhoward建议的库
The Question is easy. I'd like to biplot the results of PCA(mydata), which I did with FactoMineR. As it seems I can only display ether the variables or the individuals with the built in ploting device:
plot.PCA(pca1, choix="ind/var").
I know it can be easily done with the result of princomp(), but I really like how FactoMineR handles the NA's and it seems easier to me in many ways.
Is there a way? I saw it somehow done with ggplot2 but again only with the result of princomp(), and I have no idea how to change the code so that it works with PCA().
I also saw a solution for doing the individual and variable plot separately with ggplot2 (Look at the bottom), but how do I combine those?
Maybe the solution is somehow in the first link, but then I don't really get it :/.
I hope I made myself clear!
Greetings
Lukas
Hi you can adapt the code of your first link for PCA objects from FactoMineR
like this :
PCbiplot2 <- function(res.pca, x="Dim.1", y="Dim.2") {
if(!require(ggplot2)) install.packages("ggplot2")
# res.pca being a PCA object
data <- data.frame(obsnames=row.names(res.pca$ind$coord), res.pca$ind$coord)
plot <- ggplot(data, aes_string(x=x, y=y)) + geom_text(alpha=.4, size=3, aes(label=obsnames))
plot <- plot + geom_hline(aes(0), size=.2) + geom_vline(aes(0), size=.2)
datapc <- data.frame(varnames=rownames(res.pca$var$coord), res.pca$var$coord)
mult <- min(
(max(data[,y]) - min(data[,y])/(max(datapc[,y])-min(datapc[,y]))),
(max(data[,x]) - min(data[,x])/(max(datapc[,x])-min(datapc[,x])))
)
datapc <- transform(datapc,
v1 = .7 * mult * (get(x)),
v2 = .7 * mult * (get(y))
)
plot <- plot + coord_equal() + geom_text(data=datapc, aes(x=v1, y=v2, label=varnames), size = 5, vjust=1, color="red")
plot <- plot + geom_segment(data=datapc, aes(x=0, y=0, xend=v1, yend=v2), arrow=arrow(length=unit(0.2,"cm")), alpha=0.75, color="red")
plot
}
library(FactoMineR)
fit2 <- PCA(USArrests, graph=F)
PCbiplot2(fit2)
Tell me if it works !
Edit : add libraries like jlhoward suggests
这篇关于如何用FactoMineR创建双标图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!