GitHub

CRAN status Last Commit Downloads

Version 0.6.2 ( master branch )

This package provides a ggplot2 implementation of the biplot, a simultaneous plot of scores for observations and vectors for variables for principal component-like analyses.
The package provides two main functions: ggscreeplot() and ggbiplot().

ggbiplot aims to be a drop-in replacement for the standard R function stats::biplot() with extended functionality for labeling groups, drawing a correlation circle, and adding data ellipsoids. It also supports PCA-like objects calculated by FactoMineR::PCA(), ade4::dudi.pca() and MASS::lda().

The package was originally developed by Vince Vu at https://github.com/vqv/ggbiplot. That development was supported in part by NSF Postdoctoral Fellowship DMS-0903120 from 2009-2012. The current version, now on CRAN, will be the locus of further development.

Installation

You can install the latest CRAN version, or install from GitHub as shown below.

CRAN version install.packages("ggbiplot")
GitHub master remotes::install_github("friendly/ggbiplot")

Examples

Load packages:

library(ggplot2)
library(ggbiplot)
library(dplyr)
library(corrplot)
# set ggplot2 theme
theme_set(theme_minimal(base_size = 14))

Crime data

The crime data gives rates of various serious crimes in each of the 50 U. S. states, originally from the United States Statistical Abstracts (1970).

Let’s take a quick look at the correlations among these, using corrplot::corrplot() and showing each correlation by an ellipse whose eccentricity and shading represents the value of the correlation.

data(crime)
crime |>
  dplyr::select(where(is.numeric)) |>
  cor() |>
  corrplot(method = "ellipse", tl.srt = 0)

The correlations are all positive. Note also that the variables in the dataset are ordered in seriousness or violence, ranging from murder to auto theft.

Carry out a PCA:

crime.pca <-
  crime |>
  dplyr::select(where(is.numeric)) |>
  prcomp(scale. = TRUE)
crime.pca
#> Standard deviations (1, .., p=7):
#> [1] 2.029 1.113 0.852 0.563 0.508 0.471 0.352
#> 
#> Rotation (n x k) = (7 x 7):
#>             PC1     PC2     PC3     PC4     PC5     PC6     PC7
#> murder   -0.300 -0.6292 -0.1782  0.2321  0.5381  0.2591  0.2676
#> rape     -0.432 -0.1694  0.2442 -0.0622  0.1885 -0.7733 -0.2965
#> robbery  -0.397  0.0422 -0.4959  0.5580 -0.5200 -0.1144 -0.0039
#> assault  -0.397 -0.3435  0.0695 -0.6298 -0.5067  0.1724  0.1917
#> burglary -0.440  0.2033  0.2099  0.0576  0.1010  0.5360 -0.6481
#> larceny  -0.357  0.4023  0.5392  0.2349  0.0301  0.0394  0.6017
#> auto     -0.295  0.5024 -0.5684 -0.4192  0.3698 -0.0573  0.1470

The biplot, using default scaling (standardized components), and labeling the states by their state abbreviation:

ggbiplot(crime.pca,
         labels = crime$st ,
         circle = TRUE,
         varname.size = 4,
         varname.color = "red") 

The directions of the principal components are arbitrary; we are free to reflect the variable vectors and component scores to facilitate interpretation. Also, there seem to be differences among regions of the U.S., which can be visualized using data ellipses for the component scores. The groups argument allows the observations to colored by group and to summarized by groups.

crime.pca <- reflect(crime.pca)
ggbiplot(crime.pca,
         groups = crime$region,
         labels = crime$st,
         labels.size = 4,
         var.factor = 1.4,
         ellipse = TRUE, ellipse.level = 0.5, ellipse.alpha = 0.1,
         circle = TRUE,
         varname.size = 4,
         varname.color = "black") +
  labs(fill = "Region", color = "Region") +
  theme(legend.direction = 'horizontal', legend.position = 'top')

The interpretation of the data is now clear.

  • The first dimension, accounting for 58.8% of variance, can be seen to represent overall crime rate, with Nevada (NV) at the high end and North Dakota (ND), South Dakota (SD) and West Virginia (WV) at the low end.

  • The second dimension, accounting for 17.7% of variance represents a contrast between personal crime vs. property crime. On this dimension, Massachusetts (MA), Rhode Island (RI) are opposed to Mississippi (MS), Alabama (AL), Louisiana (LA) and South Carolina (SC).

  • The regions are represented by the differences in the centers of the data ellipses for the scores. Southern states are highest on murder, assault and rape, while the Northeast states are highest on auto theft and larceny.

  • In this standardized view, the angles between variable vectors approximate the correlations among the variables, according to

Read the original on github.com ↗