From dbfe0fef24829371561f7e5739141aee24b663e9 Mon Sep 17 00:00:00 2001 From: Alexander Shenkin Date: Thu, 19 Jan 2017 13:57:14 +0000 Subject: [PATCH] Implement variable label renaming. You can now rename the labels of the arrows in the biplot, and can make them expressions as well. --- DESCRIPTION | 6 +++- NAMESPACE | 2 ++ R/ggbiplot.r | 10 ++++-- ggbiplot.Rproj | 1 + man/ggbiplot.Rd | 79 ++++++++++++++++++++-------------------------- man/ggscreeplot.Rd | 22 ++++++------- 6 files changed, 61 insertions(+), 59 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f90b3a7..af94add 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,9 +9,13 @@ Description: A ggplot2 based biplot. It provides a drop-in replacement for biplot.princomp(). It implements a biplot and scree plot using ggplot2. Depends: - ggplot2, plyr, scales, grid + ggplot2, + plyr, + scales, + grid License: GPL-2 URL: http://github.com/vqv/ggbiplot Collate: 'ggbiplot.r' 'ggscreeplot.r' +RoxygenNote: 5.0.1 diff --git a/NAMESPACE b/NAMESPACE index eef88a6..2124136 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,2 +1,4 @@ +# Generated by roxygen2: do not edit by hand + export(ggbiplot) export(ggscreeplot) diff --git a/R/ggbiplot.r b/R/ggbiplot.r index e0e06e6..d4b53d3 100644 --- a/R/ggbiplot.r +++ b/R/ggbiplot.r @@ -37,6 +37,8 @@ #' @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 +#' @param varname.labels vector of labels to use instead of prcomp rownames +#' @param varname.labels.expr should varname.labels be interpreted as expressions to be parsed? #' #' @return a ggplot2 plot #' @export @@ -52,7 +54,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, ...) + varname.abbrev = FALSE, varname.labels = NULL, varname.labels.expr = F, ...) { library(ggplot2) library(plyr) @@ -138,6 +140,10 @@ ggbiplot <- function(pcobj, choices = 1:2, scale = 1, pc.biplot = TRUE, } else { df.v$varname <- rownames(v) } + + if(!missing(varname.labels)) { + df.v$varname = varname.labels + } # Variables for text label placement df.v$angle <- with(df.v, (180/pi) * atan(yvar / xvar)) @@ -206,7 +212,7 @@ ggbiplot <- function(pcobj, choices = 1:2, scale = 1, pc.biplot = TRUE, geom_text(data = df.v, aes(label = varname, x = xvar, y = yvar, angle = angle, hjust = hjust), - color = 'darkred', size = varname.size) + color = 'darkred', size = varname.size, parse = varname.labels.expr) } # Change the name of the legend for groups # if(!is.null(groups)) { diff --git a/ggbiplot.Rproj b/ggbiplot.Rproj index dead601..cc1983d 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,collate,namespace diff --git a/man/ggbiplot.Rd b/man/ggbiplot.Rd index 82e2070..6c7551b 100644 --- a/man/ggbiplot.Rd +++ b/man/ggbiplot.Rd @@ -1,74 +1,65 @@ +% 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, + varname.labels = NULL, varname.labels.expr = F, ...) } \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{varname.size}{size of the text for variable names} - \item{varname.adjust}{adjustment factor the placement of - the variable names, >= 1 means farther from the arrow} +\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} +\item{varname.abbrev}{whether or not to abbreviate the variable names} + +\item{varname.labels}{vector of labels to use instead of prcomp rownames} + +\item{varname.labels.expr}{should varname.labels be interpreted as expressions to be parsed?} } \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..cbcce74 100644 --- a/man/ggscreeplot.Rd +++ b/man/ggscreeplot.Rd @@ -1,25 +1,23 @@ +% 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)) + }