我正试着和R一起做PCA。
我的数据有10,000列和90行,我使用prcomp函数执行PCA。在尝试使用prcomp结果准备biplot时,我遇到了10,000个绘制的向量覆盖我的数据点的问题。biplot是否有任何选项来隐藏向量的表示?
或
我可以用plot得到主成分分析结果。但是我不确定如何根据我的数据点来标记这些点,这些数据点的编号是从1到90。
Sample<-read.table(file.choose(),header=F,sep="\t")
Sample.scaled<-data.frame(apply(Sample_2XY,2,scale))
Sample_scaled.2<-data.frame(t(na.omit(t(Sample_2XY.scaled))))
pca.Sample<-prcomp(Sample_2XY.scaled.2,retx=TRUE)
pdf("Sample_plot.pdf")
plot(pca.Sample$x)
dev.off()发布于 2012-11-14 06:20:51
如果执行help(prcomp)或?prcomp,帮助文件将告诉我们函数返回的prcomp()对象中包含的所有内容。我们只需要选择我们想要绘制的东西,并使用一些比biplot()更具控制力的函数来完成它。
当帮助文件没有说明问题时,一个更通用的技巧是在prcomp对象(在您的例子中是pca.Sample)上执行str(),以查看它的所有部分并找到我们想要的(prcomp简洁地显示R对象的内部结构)。)
下面是一个使用R的一些样本数据的示例:
# do a pca of arrests in different states
p<-prcomp(USArrests, scale = TRUE) str(p)给了我一些难看的东西,而且太长了,无法包含,但是我可以看到p$x以行名表示状态,以列表示它们在主要组件上的位置。有了它,我们可以用任何我们想要的方式来绘制它,比如使用plot()和text() (用于标签):
# plot and add labels
plot(p$x[,1],p$x[,2])
text(p$x[,1],p$x[,2],labels=rownames(p$x))如果我们正在制作一个包含许多观察值的散点图,则标签可能无法读取。因此,我们可能只希望标记更极端的值,我们可以将其与quantile()联系起来
#make a new dataframe with the info from p we want to plot
df <- data.frame(PC1=p$x[,1],PC2=p$x[,2],labels=rownames(p$x))
#make sure labels are not factors, so we can easily reassign them
df$labels <- as.character(df$labels)
# use quantile() to identify which ones are within 25-75 percentile on both
# PC and blank their labels out
df[ df$PC1 > quantile(df$PC1)["25%"] &
df$PC1 < quantile(df$PC1)["75%"] &
df$PC2 > quantile(df$PC2)["25%"] &
df$PC2 < quantile(df$PC2)["75%"],]$labels <- ""
# plot
plot(df$PC1,df$PC2)
text(df$PC1,df$PC2,labels=df$labels)https://stackoverflow.com/questions/13367880
复制相似问题