6.14 Reformatting PCA output

We can plot our PCA output using the information in pca$x.

# extract `x` table
x <- pca$x

# preview first 10 columns
head(x[, 1:10])

Every row of x is an individual, every column is a PC (going up to 960 PCs!), and the value in each cell represents the sample’s coordinate on each PC axis.

Run the code below to create a dataframe of the first three PCs to plot:

# create column of sample names
pca_results <- data.frame(sample = rownames(x),
                          PC1 = x[, 1], # PC1 values
                          PC2 = x[, 2], # PC2 values
                          PC3 = x[, 3]) # PC3 values
head(pca_results)
##          sample      PC1       PC2        PC3
## HG00096 HG00096 3.060014 -5.822356 -1.2683268
## HG00097 HG00097 2.839200 -6.278675  0.8609691
## HG00099 HG00099 1.803619 -5.171999  0.4033319
## HG00100 HG00100 3.160473 -4.504760  1.8926507
## HG00101 HG00101 4.035908 -4.545304  0.9407191
## HG00102 HG00102 3.608347 -4.668695  0.7327117