Skip to content

Commit

Permalink
Add methods to automatically plot xyz lists
Browse files Browse the repository at this point in the history
  • Loading branch information
jiho committed Sep 11, 2014
1 parent 3de1313 commit 691f41d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 6 deletions.
8 changes: 2 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
S3method(autoplot,PCA)
S3method(autoplot,list)
S3method(autoplot,pcaRes)
S3method(autoplot,prcomp)
S3method(autoplot,rda)
Expand All @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions R/autoplot-list.R
Original file line number Diff line number Diff line change
@@ -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)
}
41 changes: 41 additions & 0 deletions R/fortify-list.R
Original file line number Diff line number Diff line change
@@ -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)
}
27 changes: 27 additions & 0 deletions man/autoplot.list.Rd
Original file line number Diff line number Diff line change
@@ -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))
}

30 changes: 30 additions & 0 deletions man/fortify.list.Rd
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 691f41d

Please sign in to comment.