Plot the PCA Principal Components Analysis in R

Calculating and plot the PCA Principal Components Analysis in R in RStudio example with comments:

# Load data
load("id.Rdata")

# Set to data frame
id <- as.data.frame(id)

# set first column as a factor
id$V1 <- factor(id$V1)

# Load first 1200 rows
data <- id[1:1200,]

# Calculate the PCA (Principal Components Analysis)
data.pca <- prcomp(data[, -1], scale = TRUE)

# Calculate the ggplot2 using autoplot
data.plot <- autoplot(data.pca, data = data, colour = 'V1')

# Draw the plot
data_training.plot

Now plotting it in RStudio you’ll get your view. So here I have three grouping with legends labels on the right. In this example the column one in the dataset is the group type (0, 1, 2). Autoplot() is just using the ggplot2.

For more information about the prcomp: https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/prcomp

Source: https://ggplot2.tidyverse.org/reference/autoplot.html