Package installation

The package is put on private repo on bitbucket. It can be installed using install_bitbucket funciton in devtools package.

library(devtools)
install_bitbucket("chengvt/cheng", auth_user="keisoku", 
            password="kikuchi", dependencies = TRUE)
library(cheng)

Misc

biplot

Some customizations were added to biplot function of stats package. The usages were illustrated below.

require(EEM)
data(applejuice)
applejuice_uf <- unfold(applejuice) # unfold list into matrix
# get country of apple production
country <- sapply(strsplit(names(applejuice), split = "-"), "[", 1)

# select peaks
local_peak <- findLocalMax(applejuice, n = 1)
##    EX  EM   value
## 1 230 320 6585.27
## 2 280 320 6826.21
index <- colnames(applejuice_uf) %in% local_peak
applejuice_uf_selectedPeak <- applejuice_uf[,index, drop = FALSE]

# PCA
result <- prcomp(applejuice_uf_selectedPeak)

# create color palette for x points
library(RColorBrewer)
xcol <- brewer.pal(3, "Dark2")

# biplot 2: color the scores by group
biplot2(result, xlab = prcompname(result ,1), ylab = prcompname(result,2), 
xlabs = country, xcol = xcol)

# biplot3: turn scores into points and color them by group
biplot3(result, xlab = prcompname(result ,1), ylab = prcompname(result,2), 
xlabs = country, xcol = xcol)

# biplot4: same as biplot3 but move legend outside
biplot4(result, xlab = prcompname(result ,1), ylab = prcompname(result,2), 
xlabs = country, xcol = xcol, legendinset = 0.07)

getText

string <- "country_cultivar_fruit_1"
getText(string, 2) # get the second group of string
## [1] "cultivar"
# different separator
string <- "country~cultivar~fruit~1"
getText(string, 2, sep = "~") 
## [1] "cultivar"

plotScore_ellipse

Plotting ellipse for PCA score plots

data(applejuice)
applejuice_uf <- unfold(applejuice)
PCA <- prcomp(applejuice_uf)
plotScore_ellipse(PCA)

# change level
plotScore_ellipse(PCA, level = 0.8) 

# manually set x,y ranges
plotScore_ellipse(PCA, xlim = c(-9000, 9000), ylim = c(-4000, 4000)) 

# fill in circles
plotScore_ellipse(PCA, fill = TRUE, 
fill.alpha = 0.2, xlim = c(-9000, 9000), ylim = c(-4000, 4000))