RNotes


Three dimensional PCA plots

This package provides an interactive 3D represenation of Principal Component Analysis (PCA) models using the 3D OpenGL package rgl.

See RGL: A R-library for 3D visualization with OpenGL for a background on rgl’s capabilities.

The first example here is from the online help for the pca3d package. I hope to add additional examples later.

library(rgl)
library(pca3d)

pca3d example

metabo data

PCA analysis of metabo data – relative abundances of metabolites from serum samples of three groups of individuals. See related article via PubMed.

data(metabo)
dim(metabo)
## [1] 136 424

The metabo dataset is a data.frame with 136 rows and 424 columns.

Column 1 is the group, which is a factor with these possible values:

table(metabo[,1])
## 
## NEG POS  TB 
##  46  46  44

The remaining columns are named X1, X2, …, X428, with a few columns missing, such as X131.

PCA analysis

Include all rows, but exclude group column by specifying -1 for the column.

pca <- prcomp( metabo[,-1], scale.=TRUE )

2D

Specify the column 1 group for the 2D graphic:

pca2d( pca, group= metabo[,1] )
## 
## Legend:
## ----------------------
##  group:  color,  shape
## ----------------------
##    NEG:    red,     16
##    POS: green3,     17
##     TB:   blue,     18

The 2D plot is static and is not interactive.

3D

pca3d( pca, group= metabo[,1] )
## 
## Legend:
## ----------------------------------------
##        group:        color,        shape
## ----------------------------------------
##          NEG:          red, tetrahaedron
##          POS:       green3,         cube
##           TB:         blue,       sphere
## 
## Creating new device

The “creating new device” message tells us that there is an interactive rgl graphic displayed on our console. Screen captures are shown below of representative 3D graphics.

Default 3D View

On a PC use shift and mouse roller button to zoom in or out. Click and drag at different positions for rotations. See ?par3d for 3D graphical info.

The same figure above can be viewed from any position:

Another 3D View

A bit more fancy: black blackground, white axes, centroids, group labels:

pca3d( pca, group= metabo[,1], 
    fancy= TRUE, bg= "black", 
    show.group.labels=TRUE,
    axes.color= "white", new= TRUE )
## 
## Legend:
## ----------------------------------------
##        group:        color,        shape
## ----------------------------------------
##          NEG:          red, tetrahaedron
##          POS:       green3,         cube
##           TB:         blue,       sphere
## 
## Creating new device
## Warning in rgl.texts(x = structure(c(1.65677370419365, 2.61675926383187, :
## "bitmap" family only supports cex = 1
## Warning in rgl.texts(x = c(1.87713479804489, -2.95545892397379,
## 5.41455164521025, : No text to plot

Fancy 3D View

Another Example

Kaggle plankton classification training data can be represented with a matrix of ~3000 image features by 30,336 images. Some of the 121 classes of plankton can be “seen” in a 3D PCA visualization. See details on this page.

PCA of Plankton classification training data


packageVersion("pca3d")
## [1] '0.3'
packageVersion("rgl")
## [1] '0.95.1201'

efg

2015-02-15 2238