Skip to content

Commit

Permalink
Don't use rescaler in ggpcp (Fixes #279).
Browse files Browse the repository at this point in the history
Also clean up examples code
  • Loading branch information
hadley committed Dec 9, 2011
1 parent dc2695c commit 030fadf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 30 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -47,6 +47,8 @@ MINOR CHANGES
specific to a given data structure. Default implementation throws
an error. It is designed to have implementations provided by other
packages.

* `ggpcp` loses the `scale` argument because it relied on reshape(1) code

DEVELOPMENT

Expand Down
23 changes: 7 additions & 16 deletions R/templates.r
Expand Up @@ -9,37 +9,28 @@
#' geom functions. In particular this makes it very easy to create parallel
#' boxplots, as shown in the example.
#'
#' Three different scaling function are available:
#'
#' \itemize{
#' \item "range": scale coordinates to have common range $[0, 1]
#' \item "var": scale coordinates to have mean 0 and variance 1
#' \item "I": don't scale the coordinates at all
#' }
#'
#' @param data data frame
#' @param vars variables to include in parallel coordinates plot
#' @param scale scaling function, one of "range", "var" or "I"
#' @param ... other arguments passed on plot creation
#' @export
#' @examples
#' ggpcp(mtcars) + geom_line()
#' ggpcp(mtcars, scale="var") + geom_line()
#' ggpcp(mtcars, vars=names(mtcars)[3:6], formula= . ~cyl, scale="I") + geom_line()
#' ggpcp(mtcars, scale="I") + geom_boxplot(aes(group=variable))
#' ggpcp(mtcars, vars=names(mtcars[2:6])) + geom_line()
#' ggpcp(mtcars) + geom_boxplot(aes(group=variable))
#'
#' p <- ggpcp(mtcars, vars=names(mtcars[2:6]))
#' p + geom_line()
#' p + geom_line(aes(colour=mpg))
ggpcp <- function(data, vars=names(data), scale="range", ...) {
force(vars)
scaled <- rescaler(data[, vars], type=scale)
ggpcp <- function(data, vars=names(data), ...) {

scaled <- as.data.frame(lapply(data[, vars], rescale01))
data <- cunion(scaled, data)

data$ROWID <- 1:nrow(data)
molten <- melt(data, m=vars)

ggplot(molten, aes_string(x = "variable", y = "value", group = "ROWID"), ...)
ggplot(molten, aes_string(x = "variable", y = "value", group = "ROWID"),
...)
}

#' Create a fluctuation plot.
Expand Down
6 changes: 6 additions & 0 deletions R/utilities.r
Expand Up @@ -168,3 +168,9 @@ should_stop <- function(expr) {
# Waive decision
waiver <- function() structure(NULL, class="waiver")
is.waive <- function(x) inherits(x, "waiver")


rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}
17 changes: 3 additions & 14 deletions man/ggpcp.Rd
Expand Up @@ -2,17 +2,14 @@
\alias{ggpcp}
\title{Make a parallel coordinates plot.}
\usage{
ggpcp(data, vars = names(data), scale = "range", ...)
ggpcp(data, vars = names(data), ...)
}
\arguments{
\item{data}{data frame}

\item{vars}{variables to include in parallel coordinates
plot}

\item{scale}{scaling function, one of "range", "var" or
"I"}

\item{...}{other arguments passed on plot creation}
}
\description{
Expand All @@ -27,20 +24,12 @@
use any of the existing geom functions. In particular
this makes it very easy to create parallel boxplots, as
shown in the example.

Three different scaling function are available:

\itemize{ \item "range": scale coordinates to have common
range $[0, 1] \item "var": scale coordinates to have mean
0 and variance 1 \item "I": don't scale the coordinates
at all }
}
\examples{
ggpcp(mtcars) + geom_line()
ggpcp(mtcars, scale="var") + geom_line()
ggpcp(mtcars, vars=names(mtcars)[3:6], formula= . ~cyl, scale="I") + geom_line()
ggpcp(mtcars, scale="I") + geom_boxplot(aes(group=variable))
ggpcp(mtcars, vars=names(mtcars[2:6])) + geom_line()
ggpcp(mtcars) + geom_boxplot(aes(group=variable))

p <- ggpcp(mtcars, vars=names(mtcars[2:6]))
p + geom_line()
p + geom_line(aes(colour=mpg))
Expand Down

0 comments on commit 030fadf

Please sign in to comment.