diff --git a/DESCRIPTION b/DESCRIPTION index 021856a..3e88dc0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,13 +22,9 @@ Depends: ggplot2 (>= 0.9.0) Imports: grid, - plyr + plyr, + reshape2 Suggests: FactoMineR, pcaMethods, testthat -Collate: - 'autoplot-package.R' - 'autoplot-pca.R' - 'fortify-pca.R' - 'helpers.R' diff --git a/NAMESPACE b/NAMESPACE index 37c8749..ee831e3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ S3method(autoplot,PCA) +S3method(autoplot,list) S3method(autoplot,pcaRes) S3method(autoplot,prcomp) S3method(autoplot,rda) @@ -8,6 +9,7 @@ S3method(eigenvalues,pcaRes) S3method(eigenvalues,prcomp) S3method(eigenvalues,rda) S3method(fortify,PCA) +S3method(fortify,list) S3method(fortify,pca) S3method(fortify,pcaRes) S3method(fortify,prcomp) diff --git a/R/autoplot-list.R b/R/autoplot-list.R new file mode 100644 index 0000000..2ced18a --- /dev/null +++ b/R/autoplot-list.R @@ -0,0 +1,27 @@ +#' Automatic ggplot for a x, y, z list. +#' +#' Similar to \code{\link[graphics]{image}} +#' +#' @export +#' @param model list with components x, y and z +#' @param data not used by this method +#' @param ... not used by this method +#' @importFrom reshape2 melt +#' @examples +#' x <- seq(0, 1, by=0.1) +#' y <- seq(0, 2, by=0.1) +#' z <- outer(x, y, "+") +#' d <- list(x=x, y=y, z=z) +#' image(d) +#' autoplot(d) +#' autoplot(d, mapping=aes(alpha=x)) +autoplot.list <- function(object, mapping=aes(), ...) { + d <- fortify(object) + + # add user mappings + mapping <- c(mapping, aes_string(x="x", y="y", fill="z")) + class(mapping) <- "uneval" + + p <- ggplot() + geom_tile(mapping=mapping, data=d) + return(p) +} diff --git a/R/fortify-list.R b/R/fortify-list.R new file mode 100644 index 0000000..2d7b1c7 --- /dev/null +++ b/R/fortify-list.R @@ -0,0 +1,41 @@ +#' Fortify method for x, y, z lists. +#' +#' This function turns a list with components x, y, z, such as those used +#' by \code{\link[graphics]{image}} and \code{\link[graphics]{image}} into +#' a data frame that can more easily be plotted with ggplot2. +#' +#' @export +#' @param model list with components x, y and z +#' @param data not used by this method +#' @param ... not used by this method +#' @importFrom reshape2 melt +#' @examples +#' x <- seq(0, 1, by=0.1) +#' y <- seq(0, 2, by=0.1) +#' z <- outer(x, y, "+") +#' d <- list(x=x, y=y, z=z) +#' image(d) +#' persp(d) +#' head(fortify(d)) +#' ggplot(d) + geom_tile(aes(x=x, y=y, fill=z)) +fortify.list <- function(model, data, ...) { + if ( ! all(names(model) == c("x", "y", "z")) ) { + stop("ggplot only knows how to deal lists with components x, y, and z, as is suitable for image() or persp()") + } + if ( ! is.matrix(model$z) ) { + stop("element z of the list needs to be a matrix") + } + if ( length(model$x) != nrow(model$z) ) { + stop("element x of the list needs to be the same length at the number of rows of element z") + } + if ( length(model$y) != ncol(model$z) ) { + stop("element y of the list needs to be the same length at the number of columns of element z") + } + # convert to data.frame + d <- melt(model$z) + names(d) <- c("x", "y", "z") + # get coordinates + d$x <- model$x[d$x] + d$y <- model$y[d$y] + return(d) +} diff --git a/man/autoplot.list.Rd b/man/autoplot.list.Rd new file mode 100644 index 0000000..929fc86 --- /dev/null +++ b/man/autoplot.list.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{autoplot.list} +\alias{autoplot.list} +\title{Automatic ggplot for a x, y, z list.} +\usage{ +\method{autoplot}{list}(object, mapping = aes(), ...) +} +\arguments{ +\item{model}{list with components x, y and z} + +\item{data}{not used by this method} + +\item{...}{not used by this method} +} +\description{ +Similar to \code{\link[graphics]{image}} +} +\examples{ +x <- seq(0, 1, by=0.1) +y <- seq(0, 2, by=0.1) +z <- outer(x, y, "+") +d <- list(x=x, y=y, z=z) +image(d) +autoplot(d) +autoplot(d, mapping=aes(alpha=x)) +} + diff --git a/man/fortify.list.Rd b/man/fortify.list.Rd new file mode 100644 index 0000000..e22bbb8 --- /dev/null +++ b/man/fortify.list.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{fortify.list} +\alias{fortify.list} +\title{Fortify method for x, y, z lists.} +\usage{ +\method{fortify}{list}(model, data, ...) +} +\arguments{ +\item{model}{list with components x, y and z} + +\item{data}{not used by this method} + +\item{...}{not used by this method} +} +\description{ +This function turns a list with components x, y, z, such as those used +by \code{\link[graphics]{image}} and \code{\link[graphics]{image}} into +a data frame that can more easily be plotted with ggplot2. +} +\examples{ +x <- seq(0, 1, by=0.1) +y <- seq(0, 2, by=0.1) +z <- outer(x, y, "+") +d <- list(x=x, y=y, z=z) +image(d) +persp(d) +head(fortify(d)) +ggplot(d) + geom_tile(aes(x=x, y=y, fill=z)) +} +