PCA Biplot : A way to hide vectors to see all data points clearly

r

Solution

If you do a `help(prcomp)` or `?prcomp`, the help file tells us all the things contained in the `prcomp()` object returned by the function. We just need to pick which things we want to plot and do it with some function that gives us more control than `biplot()`.

A more general trick for cases when the help file doesn't clarify things is to do a `str()` on the prcomp object (in your case pca.Sample) to see all its parts and find what we want ( `str()` compactly displays the internal structure of an R object. )

Here is an example with some of R's sample data:

# do a pca of arrests in different states
p<-prcomp(USArrests, scale = TRUE) 

`str(p)` gives me something ugly and too long to include, but I can see that p$x has the states as rownames and their locations on the principal components as columns. Armed with this, we can plot it any way we want, such as with `plot()` and `text()` (for labels):

# plot and add labels
plot(p$x[,1],p$x[,2])
text(p$x[,1],p$x[,2],labels=rownames(p$x))

If we are making a scatterplot with many observations, the labels may not be readable. We therefore might want to only label more extreme values, which we can identify with `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)

Problem

I am trying to do PCA with R. My Data has 10,000 columns and 90 rows I used the prcomp function to do PCA. Trying to prepare a biplot with the prcomp results, I ran into the problem that the 10,000 plotted vectors cover my datapoints. Is there any option for the biplot to hide the vectors' representation? OR I can use `plot` to get the PCA results. But I am not sure how to label these points according to my datapoints, which are numbered 1 to 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() ```

Original source