diff --git a/DESCRIPTION b/DESCRIPTION index f90b3a7..b2a9f37 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,10 +8,12 @@ Maintainer: Vincent Q. Vu Description: A ggplot2 based biplot. It provides a drop-in replacement for biplot.princomp(). It implements a biplot and scree plot using ggplot2. -Depends: +Imports: ggplot2, plyr, scales, grid License: GPL-2 URL: http://github.com/vqv/ggbiplot Collate: 'ggbiplot.r' 'ggscreeplot.r' + 'data.r' +RoxygenNote: 6.0.1 diff --git a/NAMESPACE b/NAMESPACE index eef88a6..d06318a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,2 +1,9 @@ +# Generated by roxygen2: do not edit by hand + export(ggbiplot) export(ggscreeplot) +import(ggplot2) +import(grid) +import(plyr) +import(scales) +import(stats) diff --git a/R/data.r b/R/data.r new file mode 100644 index 0000000..fc7010c --- /dev/null +++ b/R/data.r @@ -0,0 +1,24 @@ +#' @name wine.class +#' @docType data +#' @title Cultivars of the wines in the wine dataset. +#' @description Cultivars of the wines in the wine dataset, same length as the +#' dataset has rows. +#' @format Factor of length 178 with 3 levels "barolo", "grignolino", and "barbera". +NULL + + +#' @name wine +#' @aliases wine +#' @docType data +#' @title Chemical composition of three cultivars of wine +#' @description Chemical constituents of wines from three different cultivars grown +#' in the same region in Italy. The cultivars, 'barolo', 'barbera', and +#' 'grignolino', are indicated in wine.class. +#' @format The format is: chr "wine" +#' @source http://archive.ics.uci.edu/ml/datasets/Wine +#' @examples data(wine) +#' wine.pca <- prcomp(wine, scale. = TRUE) +#' print(ggscreeplot(wine.pca)) +#' print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, +#' ellipse = TRUE, circle = TRUE)) +NULL \ No newline at end of file diff --git a/R/ggbiplot.r b/R/ggbiplot.r index e0e06e6..fc37e99 100644 --- a/R/ggbiplot.r +++ b/R/ggbiplot.r @@ -33,17 +33,20 @@ #' @param labels.size size of the text used for the labels #' @param alpha alpha transparency value for the points (0 = transparent, 1 = opaque) #' @param circle draw a correlation circle? (only applies when prcomp was called with scale = TRUE and when var.scale = 1) +#' @param circle.prob size of the circle #' @param var.axes draw arrows for the variables? #' @param varname.size size of the text for variable names #' @param varname.adjust adjustment factor the placement of the variable names, >= 1 means farther from the arrow #' @param varname.abbrev whether or not to abbreviate the variable names #' #' @return a ggplot2 plot +#' @import ggplot2 plyr scales grid stats #' @export #' @examples #' data(wine) #' wine.pca <- prcomp(wine, scale. = TRUE) -#' print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, ellipse = TRUE, circle = TRUE)) +#' print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, +#' ellipse = TRUE, circle = TRUE)) #' ggbiplot <- function(pcobj, choices = 1:2, scale = 1, pc.biplot = TRUE, obs.scale = 1 - scale, var.scale = scale, @@ -52,13 +55,7 @@ ggbiplot <- function(pcobj, choices = 1:2, scale = 1, pc.biplot = TRUE, var.axes = TRUE, circle = FALSE, circle.prob = 0.69, varname.size = 3, varname.adjust = 1.5, - varname.abbrev = FALSE, ...) -{ - library(ggplot2) - library(plyr) - library(scales) - library(grid) - + varname.abbrev = FALSE) { stopifnot(length(choices) == 2) # Recover the SVD diff --git a/ggbiplot.Rproj b/ggbiplot.Rproj index dead601..c539f33 100644 --- a/ggbiplot.Rproj +++ b/ggbiplot.Rproj @@ -15,3 +15,4 @@ LaTeX: XeLaTeX BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,namespace diff --git a/man/ggbiplot.Rd b/man/ggbiplot.Rd index 82e2070..73d9389 100644 --- a/man/ggbiplot.Rd +++ b/man/ggbiplot.Rd @@ -1,74 +1,62 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ggbiplot.r \name{ggbiplot} \alias{ggbiplot} \title{Biplot for Principal Components using ggplot2} \usage{ - ggbiplot(pcobj, choices = 1:2, scale = 1, pc.biplot = - TRUE, obs.scale = 1 - scale, var.scale = scale, groups = - NULL, ellipse = FALSE, ellipse.prob = 0.68, labels = - NULL, labels.size = 3, alpha = 1, var.axes = TRUE, circle - = FALSE, circle.prob = 0.69, varname.size = 3, - varname.adjust = 1.5, varname.abbrev = FALSE, ...) +ggbiplot(pcobj, choices = 1:2, scale = 1, pc.biplot = TRUE, + obs.scale = 1 - scale, var.scale = scale, groups = NULL, + ellipse = FALSE, ellipse.prob = 0.68, labels = NULL, labels.size = 3, + alpha = 1, var.axes = TRUE, circle = FALSE, circle.prob = 0.69, + varname.size = 3, varname.adjust = 1.5, varname.abbrev = FALSE) } \arguments{ - \item{pcobj}{an object returned by prcomp() or - princomp()} +\item{pcobj}{an object returned by prcomp() or princomp()} - \item{choices}{which PCs to plot} +\item{choices}{which PCs to plot} - \item{scale}{covariance biplot (scale = 1), form biplot - (scale = 0). When scale = 1, the inner product between - the variables approximates the covariance and the - distance between the points approximates the Mahalanobis - distance.} +\item{scale}{covariance biplot (scale = 1), form biplot (scale = 0). When scale = 1, the inner product between the variables approximates the covariance and the distance between the points approximates the Mahalanobis distance.} - \item{obs.scale}{scale factor to apply to observations} +\item{pc.biplot}{for compatibility with biplot.princomp()} - \item{var.scale}{scale factor to apply to variables} +\item{obs.scale}{scale factor to apply to observations} - \item{pc.biplot}{for compatibility with - biplot.princomp()} +\item{var.scale}{scale factor to apply to variables} - \item{groups}{optional factor variable indicating the - groups that the observations belong to. If provided the - points will be colored according to groups} +\item{groups}{optional factor variable indicating the groups that the observations belong to. If provided the points will be colored according to groups} - \item{ellipse}{draw a normal data ellipse for each - group?} +\item{ellipse}{draw a normal data ellipse for each group?} - \item{ellipse.prob}{size of the ellipse in Normal - probability} +\item{ellipse.prob}{size of the ellipse in Normal probability} - \item{labels}{optional vector of labels for the - observations} +\item{labels}{optional vector of labels for the observations} - \item{labels.size}{size of the text used for the labels} +\item{labels.size}{size of the text used for the labels} - \item{alpha}{alpha transparency value for the points (0 = - TRUEransparent, 1 = opaque)} +\item{alpha}{alpha transparency value for the points (0 = transparent, 1 = opaque)} - \item{circle}{draw a correlation circle? (only applies - when prcomp was called with scale = TRUE and when - var.scale = 1)} +\item{var.axes}{draw arrows for the variables?} - \item{var.axes}{draw arrows for the variables?} +\item{circle}{draw a correlation circle? (only applies when prcomp was called with scale = TRUE and when var.scale = 1)} - \item{varname.size}{size of the text for variable names} +\item{circle.prob}{size of the circle} - \item{varname.adjust}{adjustment factor the placement of - the variable names, >= 1 means farther from the arrow} +\item{varname.size}{size of the text for variable names} - \item{varname.abbrev}{whether or not to abbreviate the - variable names} +\item{varname.adjust}{adjustment factor the placement of the variable names, >= 1 means farther from the arrow} + +\item{varname.abbrev}{whether or not to abbreviate the variable names} } \value{ - a ggplot2 plot +a ggplot2 plot } \description{ - Biplot for Principal Components using ggplot2 +Biplot for Principal Components using ggplot2 } \examples{ -data(wine) -wine.pca <- prcomp(wine, scale. = TRUE) -print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, ellipse = TRUE, circle = TRUE)) -} + data(wine) + wine.pca <- prcomp(wine, scale. = TRUE) + print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, + ellipse = TRUE, circle = TRUE)) +} diff --git a/man/ggscreeplot.Rd b/man/ggscreeplot.Rd index 1e7ce96..452b67d 100644 --- a/man/ggscreeplot.Rd +++ b/man/ggscreeplot.Rd @@ -1,25 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ggscreeplot.r \name{ggscreeplot} \alias{ggscreeplot} \title{Screeplot for Principal Components} \usage{ - ggscreeplot(pcobj, type = c("pev", "cev")) +ggscreeplot(pcobj, type = c("pev", "cev")) } \arguments{ - \item{pcobj}{an object returned by prcomp() or - princomp()} +\item{pcobj}{an object returned by prcomp() or princomp()} - \item{type}{the type of scree plot. 'pev' corresponds - proportion of explained variance, i.e. the eigenvalues - divided by the trace. 'cev' corresponds to the cumulative - proportion of explained variance, i.e. the partial sum of - the first k eigenvalues divided by the trace.} +\item{type}{the type of scree plot. 'pev' corresponds proportion of explained variance, i.e. the eigenvalues divided by the trace. 'cev' corresponds to the cumulative proportion of explained variance, i.e. the partial sum of the first k eigenvalues divided by the trace.} } \description{ - Screeplot for Principal Components +Screeplot for Principal Components } \examples{ -data(wine) -wine.pca <- prcomp(wine, scale. = TRUE) -print(ggscreeplot(wine.pca)) -} + data(wine) + wine.pca <- prcomp(wine, scale. = TRUE) + print(ggscreeplot(wine.pca)) +} diff --git a/man/wine.Rd b/man/wine.Rd index ec50e7f..f6eb071 100644 --- a/man/wine.Rd +++ b/man/wine.Rd @@ -1,25 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.r +\docType{data} \name{wine} \alias{wine} -\docType{data} -\title{ -Chemical composition of three cultivars of wine -} -\description{ -Chemical constituents of wines from three different cultivars grown in the same region in Italy. The cultivars, -'barolo', 'barbera', and 'grignolino', are indicated in wine.class. -} -\usage{data(wine)} -\format{ - The format is: - chr "wine" -} +\title{Chemical composition of three cultivars of wine} +\format{The format is: chr "wine"} \source{ http://archive.ics.uci.edu/ml/datasets/Wine } +\description{ +Chemical constituents of wines from three different cultivars grown +in the same region in Italy. The cultivars, 'barolo', 'barbera', and +'grignolino', are indicated in wine.class. +} \examples{ data(wine) wine.pca <- prcomp(wine, scale. = TRUE) print(ggscreeplot(wine.pca)) -print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, ellipse = TRUE, circle = TRUE)) +print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, +ellipse = TRUE, circle = TRUE)) } -\keyword{datasets} diff --git a/man/wine.class.Rd b/man/wine.class.Rd new file mode 100644 index 0000000..2c33922 --- /dev/null +++ b/man/wine.class.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.r +\docType{data} +\name{wine.class} +\alias{wine.class} +\title{Cultivars of the wines in the wine dataset.} +\format{Factor of length 178 with 3 levels "barolo", "grignolino", and "barbera".} +\description{ +Cultivars of the wines in the wine dataset, same length as the +dataset has rows. +}