Skip to content

Commit 691f41d

Browse files
committed
Add methods to automatically plot xyz lists
1 parent 3de1313 commit 691f41d

File tree

6 files changed

+129
-6
lines changed

6 files changed

+129
-6
lines changed

DESCRIPTION

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ Depends:
2222
ggplot2 (>= 0.9.0)
2323
Imports:
2424
grid,
25-
plyr
25+
plyr,
26+
reshape2
2627
Suggests:
2728
FactoMineR,
2829
pcaMethods,
2930
testthat
30-
Collate:
31-
'autoplot-package.R'
32-
'autoplot-pca.R'
33-
'fortify-pca.R'
34-
'helpers.R'

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
S3method(autoplot,PCA)
2+
S3method(autoplot,list)
23
S3method(autoplot,pcaRes)
34
S3method(autoplot,prcomp)
45
S3method(autoplot,rda)
@@ -8,6 +9,7 @@ S3method(eigenvalues,pcaRes)
89
S3method(eigenvalues,prcomp)
910
S3method(eigenvalues,rda)
1011
S3method(fortify,PCA)
12+
S3method(fortify,list)
1113
S3method(fortify,pca)
1214
S3method(fortify,pcaRes)
1315
S3method(fortify,prcomp)

R/autoplot-list.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' Automatic ggplot for a x, y, z list.
2+
#'
3+
#' Similar to \code{\link[graphics]{image}}
4+
#'
5+
#' @export
6+
#' @param model list with components x, y and z
7+
#' @param data not used by this method
8+
#' @param ... not used by this method
9+
#' @importFrom reshape2 melt
10+
#' @examples
11+
#' x <- seq(0, 1, by=0.1)
12+
#' y <- seq(0, 2, by=0.1)
13+
#' z <- outer(x, y, "+")
14+
#' d <- list(x=x, y=y, z=z)
15+
#' image(d)
16+
#' autoplot(d)
17+
#' autoplot(d, mapping=aes(alpha=x))
18+
autoplot.list <- function(object, mapping=aes(), ...) {
19+
d <- fortify(object)
20+
21+
# add user mappings
22+
mapping <- c(mapping, aes_string(x="x", y="y", fill="z"))
23+
class(mapping) <- "uneval"
24+
25+
p <- ggplot() + geom_tile(mapping=mapping, data=d)
26+
return(p)
27+
}

R/fortify-list.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#' Fortify method for x, y, z lists.
2+
#'
3+
#' This function turns a list with components x, y, z, such as those used
4+
#' by \code{\link[graphics]{image}} and \code{\link[graphics]{image}} into
5+
#' a data frame that can more easily be plotted with ggplot2.
6+
#'
7+
#' @export
8+
#' @param model list with components x, y and z
9+
#' @param data not used by this method
10+
#' @param ... not used by this method
11+
#' @importFrom reshape2 melt
12+
#' @examples
13+
#' x <- seq(0, 1, by=0.1)
14+
#' y <- seq(0, 2, by=0.1)
15+
#' z <- outer(x, y, "+")
16+
#' d <- list(x=x, y=y, z=z)
17+
#' image(d)
18+
#' persp(d)
19+
#' head(fortify(d))
20+
#' ggplot(d) + geom_tile(aes(x=x, y=y, fill=z))
21+
fortify.list <- function(model, data, ...) {
22+
if ( ! all(names(model) == c("x", "y", "z")) ) {
23+
stop("ggplot only knows how to deal lists with components x, y, and z, as is suitable for image() or persp()")
24+
}
25+
if ( ! is.matrix(model$z) ) {
26+
stop("element z of the list needs to be a matrix")
27+
}
28+
if ( length(model$x) != nrow(model$z) ) {
29+
stop("element x of the list needs to be the same length at the number of rows of element z")
30+
}
31+
if ( length(model$y) != ncol(model$z) ) {
32+
stop("element y of the list needs to be the same length at the number of columns of element z")
33+
}
34+
# convert to data.frame
35+
d <- melt(model$z)
36+
names(d) <- c("x", "y", "z")
37+
# get coordinates
38+
d$x <- model$x[d$x]
39+
d$y <- model$y[d$y]
40+
return(d)
41+
}

man/autoplot.list.Rd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
% Generated by roxygen2 (4.0.2): do not edit by hand
2+
\name{autoplot.list}
3+
\alias{autoplot.list}
4+
\title{Automatic ggplot for a x, y, z list.}
5+
\usage{
6+
\method{autoplot}{list}(object, mapping = aes(), ...)
7+
}
8+
\arguments{
9+
\item{model}{list with components x, y and z}
10+
11+
\item{data}{not used by this method}
12+
13+
\item{...}{not used by this method}
14+
}
15+
\description{
16+
Similar to \code{\link[graphics]{image}}
17+
}
18+
\examples{
19+
x <- seq(0, 1, by=0.1)
20+
y <- seq(0, 2, by=0.1)
21+
z <- outer(x, y, "+")
22+
d <- list(x=x, y=y, z=z)
23+
image(d)
24+
autoplot(d)
25+
autoplot(d, mapping=aes(alpha=x))
26+
}
27+

man/fortify.list.Rd

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
% Generated by roxygen2 (4.0.2): do not edit by hand
2+
\name{fortify.list}
3+
\alias{fortify.list}
4+
\title{Fortify method for x, y, z lists.}
5+
\usage{
6+
\method{fortify}{list}(model, data, ...)
7+
}
8+
\arguments{
9+
\item{model}{list with components x, y and z}
10+
11+
\item{data}{not used by this method}
12+
13+
\item{...}{not used by this method}
14+
}
15+
\description{
16+
This function turns a list with components x, y, z, such as those used
17+
by \code{\link[graphics]{image}} and \code{\link[graphics]{image}} into
18+
a data frame that can more easily be plotted with ggplot2.
19+
}
20+
\examples{
21+
x <- seq(0, 1, by=0.1)
22+
y <- seq(0, 2, by=0.1)
23+
z <- outer(x, y, "+")
24+
d <- list(x=x, y=y, z=z)
25+
image(d)
26+
persp(d)
27+
head(fortify(d))
28+
ggplot(d) + geom_tile(aes(x=x, y=y, fill=z))
29+
}
30+

0 commit comments

Comments
 (0)