suppressPackageStartupMessages({library(ggplot2)})
suppressWarnings({
pc_scores = as.data.frame(data.pc$x)
loadings = as.data.frame(data.pc$rotation)
biplot = ggplot(data = pc_scores, aes(x = PC1, y = PC2)) +
geom_point() + geom_segment(data = loadings,
aes(x = 0, y = 0, xend = PC1, yend = PC2),
arrow = arrow(length = unit(0.2, "inches")),
color = "red") + geom_text(data = loadings,
aes(x = PC1, y = PC2, label = row.names(loadings)),
hjust = -0.2, vjust = -0.2, size = 3, color = "red") +
labs(x = "Principal Component 1 (~73% variance)",
y = "Principal Component 2 (~23% variance)",
title = "PCA Biplot") + theme_minimal()
print(biplot)
})