-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods to automatically plot xyz lists
- Loading branch information
Showing
6 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|