Draw a two-dimensional projection of the scores with the
original variable axes overlaid as arrows (loadings), as in a biplot. Pass
object (the output of stats::prcomp() or an rrcov::Pca* function);
otherwise supply scores and loadings directly. All scores should be
centred about the origin. The arrows are stretched by a common factor so that
the longest arrow just reaches the edge of the point cloud, and only loadings
longer than label_threshold are labelled.
biplot_projection(
object = NULL,
scores = NULL,
loadings = NULL,
label_threshold = 0,
arrow_colour = "#c14b14",
...
)A ggplot object.
Optionally, the output of stats::prcomp() or an rrcov::Pca*
function. If supplied, the scores and loadings are extracted from it and the
scores and loadings arguments are ignored.
A matrix or data frame of scores centred about the origin,
with the first two columns used as the horizontal and vertical coordinates.
Ignored if object is supplied.
A matrix or data frame of loadings, with row names giving the
variable names and the first two columns used as the arrow directions.
Ignored if object is supplied.
Only loadings whose absolute length exceeds this
threshold are labelled. The default of 0 labels every non-zero loading.
Colour of the arrows and labels.
Additional arguments passed to ggplot2::geom_point().
Rob J Hyndman
Hyndman, R J (2026) "That's weird: Anomaly detection using R", Chapter 9, https://OTexts.com/weird/.
oldfaithful[, c("duration", "waiting")] |>
prcomp(scale = TRUE) |>
biplot_projection()
Run the code above in your browser using DataLab