vmikk / ggord

a take on ordination plots using ggplot2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ggord

Marcus W. Beck, mbafs2012@gmail.com

Travis-CI Build Status

A simple package for creating ordination plots with ggplot2 (aka reinventing the wheel, see this and this). Install the package as follows:

install.packages('devtools')
library(devtools)
install_github('fawda123/ggord')
library(ggord)

The following shows some examples of creating biplots using the methods available with ggord. These methods were developed independently from the ggbiplot and factoextra packages, though the biplots are practically identical. I made liberal use of the ellipses feature from ggbiplot, so credit is given where credit is due. Most methods are for results from principal components analysis, although methods are available for nonmetric multidimensional scaling, multiple correspondence analysis, correspondence analysis, and linear discriminant analysis. Available methods are as follows:

##  [1] ggord.acm      ggord.ca       ggord.cca      ggord.coa     
##  [5] ggord.default  ggord.lda      ggord.mca      ggord.MCA     
##  [9] ggord.metaMDS  ggord.pca      ggord.PCA      ggord.prcomp  
## [13] ggord.princomp ggord.rda     
## see '?methods' for accessing help and source code
# principal components analysis with the iris data set
# prcomp
ord <- prcomp(iris[, 1:4])

p <- ggord(ord, iris$Species)
p

library(ggplot2)
p + scale_colour_manual('Groups', values = c('purple', 'orange', 'blue'))

p + scale_shape_manual('Groups', values = c(1, 2, 3))

p + theme_classic()

p + theme(legend.position = 'top')

# change the vector labels with vec_lab
new_lab <- list(Sepal.Length = 'SL', Sepal.Width = 'SW', Petal.Width = 'PW',
  Petal.Length = 'PL')
p <- ggord(ord, iris$Species, vec_lab = new_lab)
p

# observations as labels from row names
p <- ggord(ord, iris$Species, obslab = TRUE)
p

# principal components analysis with the iris dataset
# princomp
ord <- princomp(iris[, 1:4])

ggord(ord, iris$Species)

# principal components analysis with the iris dataset
# PCA
library(FactoMineR)

ord <- PCA(iris[, 1:4], graph = FALSE)

ggord(ord, iris$Species)

# principal components analysis with the iris dataset
# dudi.pca
library(ade4)

ord <- dudi.pca(iris[, 1:4], scannf = FALSE, nf = 4)

ggord(ord, iris$Species)

# multiple correspondence analysis with the tea dataset
# MCA
data(tea, package = 'FactoMineR')
tea <- tea[, c('Tea', 'sugar', 'price', 'age_Q', 'sex')]

ord <- MCA(tea[, -1], graph = FALSE)

ggord(ord, tea$Tea)

# multiple correspondence analysis with the tea dataset
# mca
library(MASS)

ord <- mca(tea[, -1])

ggord(ord, tea$Tea)

# multiple correspondence analysis with the tea dataset
# acm
ord <- dudi.acm(tea[, -1], scannf = FALSE)

ggord(ord, tea$Tea)

# nonmetric multidimensional scaling with the iris dataset
# metaMDS
library(vegan)
ord <- metaMDS(iris[, 1:4])

ggord(ord, iris$Species)

# linear discriminant analysis
# example from lda in MASS package
ord <- lda(Species ~ ., iris, prior = rep(1, 3)/3)

ggord(ord, iris$Species)

# correspondence analysis
# dudi.coa
ord <- dudi.coa(iris[, 1:4], scannf = FALSE, nf = 4)

ggord(ord, iris$Species)

# correspondence analysis
# ca
library(ca)
ord <- ca(iris[, 1:4])

ggord(ord, iris$Species)

######
# triplots

# redundancy analysis
# rda from vegan
data(varespec)
data(varechem)
ord <- rda(varespec, varechem)

ggord(ord)

# canonical correspondence analysis
# cca from vegan
ord <- cca(varespec, varechem)

ggord(ord)

# species points as text
# suppress site points
ggord(ord, ptslab = TRUE, size = NA, addsize = 5)

About

a take on ordination plots using ggplot2


Languages

Language:R 69.5%Language:JavaScript 26.4%Language:CSS 4.1%