draw_biplot: Easily perform PCA and draw a biplot of the results with a...

View source: R/draw_biplot.R

draw_biplotR Documentation

Easily perform PCA and draw a biplot of the results with a calibrated axis

Description

takes a data frame, performs PCA on the numeric columns, and draws a biplot with a clearly labeled calibrated axis for one of the original numeric columns. The first non-numeric column is used to label PC scores (points).

Usage

draw_biplot(
  data,
  key_axis = "none",
  ticklab = NULL,
  project = TRUE,
  scale = TRUE,
  fix_sign = FALSE,
  points = TRUE,
  point_labels = TRUE,
  arrows = TRUE,
  mult = NULL,
  vector_colors = c("grey40", "red"),
  point_color = "cornflowerblue",
  point_size = 1.5
)

Arguments

data

data.frame or object that can be coerced to one. PCA will be performed on numeric columns. The first non-numeric column will be used to label PC scores (points).

key_axis

character indicating the column name of the axis to calibrate. If none is specified, no axes will be calibrated.

ticklab

vector of values indicating axis breaks and tick labels for calibrated axis. If not specified, five evenly spaced round values will be chosen by pretty(). Ignored if key_axis is not specified.

project

logical indicating whether projection lines should be drawn from PC scores to PC vectors. Defaults to TRUE.

scale

logical passed to the scale. parameter of prcomp(). Defaults to TRUE (in contrast to prcomp()).

fix_sign

logical indicating whether the signs of the loadings and scores should be chosen so that the first element of each loading is non-negative. Defaults to FALSE (in contrast to princomp() from which this parameter name was borrowed. prcomp() does not have such an option.)

points

logical indicating whether to plot principal component scores. Defaults to TRUE.

point_labels

logical indicating whether to plot point labels. Defaults to TRUE.

arrows

logical indicating whether to plot principal component loading vectors. Defaults to TRUE.

mult

(NULL) numeric value used to scale the length of the rotation vectors, in effect creating secondary axes with different scales than the primary axes, although the secondary axes are not shown. If not specified, the factor is set to the ratio of 75th percentile distance between the scores and the origin to the length of the longest vector.

vector_colors

= vector of two colors, the first for the non-calibrated vectors and the second for the calibrated axis (and vector). Defaults to c("grey40", "red").

point_color

= color for points and point labels. Defaults to "cornflowerblue".

point_size

= point size. Defaults to ggplot2 point size (1.5).

Details

This function was developed mainly for teaching and learning purposes. There are many options for drawing biplots. The main contributions of this one are ease of use and option to calibrate one of the axes. Calibration calculations are performed by calibrate::calibrate().

References

(to be added)

Examples


swiss$country <- rownames(swiss)
draw_biplot(swiss, key_axis = "Agriculture")




jtr13/redav documentation built on March 31, 2024, 11:56 a.m.