PCA FactoMineR绘图数据 [英] PCA FactoMineR plot data

查看:954
本文介绍了PCA FactoMineR绘图数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行一个R脚本,使用 FactorMineR

我想输出生成的PCA图的坐标,但我无法找到正确的坐标。我发现 results1 $ ind $ coord results1 $ var $ coord ,但都不像默认图。



我发现
http://www.statistik.tuwien.ac.at/public/filz/students/seminar/ws1011/hoffmann_ausarbeitung.pdf

http://factominer.free.fr/classical-methods/principal-components-analysis。 html
,但都没有描述由PCA创建的变量的内容

  library(FactoMineR)$ b $ data1<  -  read.table(file = args [1],sep ='\ t',header = T,row.names = 1)
result1 < - PCA(data1,ncp = 4,graph = TRUE)#图自动生成
plot(result1)


解决方案

我发现 $ ind $ coord [,1] $ ind $ coord [,2] PCA 对象中的前两个pca coords。下面是一个有效的例子,其中包含您可能想要对 PCA 输出执行的其他一些操作...



<$ p使用ggplot2绘制FactoMineR的PCA的输出

#加载库
库(FactoMineR)
库(ggplot2)
库(比例)
库(网格)
库(plyr)
库(gridExtra)

#以一张干净的石板开始
rm(list = ls(all = TRUE))

#加载示例数据
数据(十项全能)

#计算PCA
res.pca< - PCA(十项全能,quanti.sup = 11:12,quali.sup = 13,graph = FALSE)

#提取一些绘图部分
PC1 < - res.pca $ ind $ coord [,1]
PC2 < - res.pca $ ind $ coord [,2]
labs < - rownames(res.pca $ ind $ coord)
PC< ; - data.frame(cbind(PC1,PC2))
rownames(PCs)< - labs

#只显示单个样本...
ggplot(PC ,aes(PC1,PC2,label = rownames(PCs)))+
geom_text()

 #现在得到补充分类变量
cPC1< - res.pca $ quali.sup $ coor [,1]
cPC2< - res.pca $ quali.sup $ coor [,2] $ b $ (cp)(cPC2))
rownames(cPC)< - clabs $ b(< - rownames(res.pca $ quali.sup $ coor)
cPCs< - data.frame $ b colnames(cPCs)< - colnames(PCs)

#将样本和分类变量(例如,
p< - ggplot()+ theme(aspect.ratio = 1)+ theme_bw(base_size = 20)
#没有数据,所以没有什么可以绘制的。 (数据= PC,aes(x = PC1,y = PC2,标签= rownames(PC)),大小= 4)$ b $加上数据
p < - p + geom_text bp <-p + geom_text(data = cPCs,aes(x = cPC1,y = cPC2,label = rownames(cPC)),size = 10)
p#显示两个图层的绘图


 #现在提取变量

vPC1< - res.pca $ var $ co $ [,1]
vPC2< - res.pca $ var $ coord [,2]
vlabs< - rownames(res.pca $ var $ coord)
vPCs< - 数据帧(cbind(vPC1,vPC2))
rownames(vPC)< -vlabs>
colnames(vPC)< - colnames(PC)>

#和绘制它们

pv < - ggplot()+ theme(aspect.ratio = 1)+ theme_bw(base_size = 20)
#没有数据,所以没有什么可以绘制
#在那里放一个晕圈,就像c一样(数据帧)(x = sin(角度),y = cos(角度))
(-pi,pi,长度= 50)
df< pv < - pv + geom_path(aes(x,y),data = df,color =grey70)

#添加箭头和变量标签
pv < - pv + geom_text(data = vPCs,aes(x = vPC1,y = vPC2,label = rownames(vPCs)),size = 4)+ xlab(PC1)+ ylab(PC2)
pv < (x = 0,y = 0,xend = vPC1 * 0.9,yend = vPC2 * 0.9),箭头=箭头(长度=单位(1/2,'picas')),pv + geom_segment color =grey30)
pv#show plot

 #现在把他们并排在一个单一的图像

grid.arrange(p,pv,nrow = 1)

#现在他们可以被保存或导出...


I'm running an R script generating plots of the PCA analysis using FactorMineR.

I'd like to output the coordinates for the generated PCA plots but I'm having trouble finding the right coordinates. I found results1$ind$coord and results1$var$coord but neither look like the default plot.

I found http://www.statistik.tuwien.ac.at/public/filz/students/seminar/ws1011/hoffmann_ausarbeitung.pdf and http://factominer.free.fr/classical-methods/principal-components-analysis.html but neither describe the contents of the variable created by the PCA

library(FactoMineR)
data1 <- read.table(file=args[1], sep='\t', header=T, row.names=1)
result1 <- PCA(data1,ncp = 4, graph=TRUE) # graphs generated automatically
plot(result1)

解决方案

I found that $ind$coord[,1] and $ind$coord[,2] are the first two pca coords in the PCA object. Here's a worked example that includes a few other things you might want to do with the PCA output...

# Plotting the output of FactoMineR's PCA using ggplot2
#
# load libraries
library(FactoMineR)
library(ggplot2)
library(scales)
library(grid)
library(plyr)
library(gridExtra)
#
# start with a clean slate
rm(list=ls(all=TRUE)) 
#
# load example data
data(decathlon)
#
# compute PCA
res.pca <- PCA(decathlon, quanti.sup = 11:12, quali.sup=13, graph = FALSE)
#
# extract some parts for plotting
PC1 <- res.pca$ind$coord[,1]
PC2 <- res.pca$ind$coord[,2]
labs <- rownames(res.pca$ind$coord)
PCs <- data.frame(cbind(PC1,PC2))
rownames(PCs) <- labs
#
# Just showing the individual samples...
ggplot(PCs, aes(PC1,PC2, label=rownames(PCs))) + 
  geom_text() 

# Now get supplementary categorical variables
cPC1 <- res.pca$quali.sup$coor[,1]
cPC2 <- res.pca$quali.sup$coor[,2]
clabs <- rownames(res.pca$quali.sup$coor)
cPCs <- data.frame(cbind(cPC1,cPC2))
rownames(cPCs) <- clabs
colnames(cPCs) <- colnames(PCs)
#
# Put samples and categorical variables (ie. grouping
# of samples) all together
p <- ggplot() + theme(aspect.ratio=1) + theme_bw(base_size = 20) 
# no data so there's nothing to plot...
# add on data 
p <- p + geom_text(data=PCs, aes(x=PC1,y=PC2,label=rownames(PCs)), size=4) 
p <- p + geom_text(data=cPCs, aes(x=cPC1,y=cPC2,label=rownames(cPCs)),size=10)
p # show plot with both layers

# Now extract the variables
#
vPC1 <- res.pca$var$coord[,1]
vPC2 <- res.pca$var$coord[,2]
vlabs <- rownames(res.pca$var$coord)
vPCs <- data.frame(cbind(vPC1,vPC2))
rownames(vPCs) <- vlabs
colnames(vPCs) <- colnames(PCs)
#
# and plot them
#
pv <- ggplot() + theme(aspect.ratio=1) + theme_bw(base_size = 20) 
# no data so there's nothing to plot
# put a faint circle there, as is customary
angle <- seq(-pi, pi, length = 50) 
df <- data.frame(x = sin(angle), y = cos(angle)) 
pv <- pv + geom_path(aes(x, y), data = df, colour="grey70") 
#
# add on arrows and variable labels
pv <- pv + geom_text(data=vPCs, aes(x=vPC1,y=vPC2,label=rownames(vPCs)), size=4) + xlab("PC1") + ylab("PC2")
pv <- pv + geom_segment(data=vPCs, aes(x = 0, y = 0, xend = vPC1*0.9, yend = vPC2*0.9), arrow = arrow(length = unit(1/2, 'picas')), color = "grey30")
pv # show plot 

# Now put them side by side in a single image
#
grid.arrange(p,pv,nrow=1)
# 
# Now they can be saved or exported...

这篇关于PCA FactoMineR绘图数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆