如果你做一个help(prcomp)
或?prcomp
如何标注这些点上,帮助文件告诉我们,所有包含在prcomp()
对象的事由函数返回。我们只需要选择我们想要绘制哪些东西,然后使用一些功能来实现,比biplot()
更具有控制力。
的情况下,一个更普遍的伎俩时,帮助文件不明确的事情是应该做的prcomp对象的str()
(你的情况pca.Sample),看看它的所有部件,并找到我们想要的(str()
紧凑显示器。R对象的内部结构)
下面是一些的r样本数据的例子:
# do a pca of arrests in different states
p<-prcomp(USArrests, scale = TRUE)
str(p)
给我的东西丑陋,太长时间,包括,但我可以看到,p $ X具有作为rownames的状态以及它们在主要组件上的位置作为列。这个武装,我们就可以绘制它,我们想要的任何方式,如用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)
感谢您的回复。但在我的情况下,rownames(pca.Sample $ x)的结果为NULL。在那里作为rownames(示例)给出数字从1到90.是否有办法我可以得到相同的行号pca.Sample $ x –
你可以str(pca.Sample)并将结果添加到您的问题?您是否试图将90维减小到10,000个观测值的较小数目,或将10,000维减少到90个观测值的较小数目? – MattBagg
我不明白你的问题。我对R很新,但是这里是你要求的str(pca.Sample)的结果。更准确地说,我有90行和9912列.'str(pca.Sample)列表5 $ sdev :num [1:90] 87.21 38.37 12.26 8.79 6.2 ... $ rotation:num [1:9912,1:90] 0.01135 -0.0108 -0.00551 -0.00868 -0.0099 ... ..- attr(*,“ dim1“)=列表2 ..:$:chr [1:9912]”V1“”V2“”V3“”V4“... .. .. $:chr [1:90]”PC1 “PC2”“PC3”“PC4”... $ center:命名号码[1:9912] 4.56e-17 4.16e-18 -9.49e-17 -6.03e-17 5.09e -17 ... ' –