Skip to content

Commit

Permalink
Add interpolate option to geom/annotation_raster.
Browse files Browse the repository at this point in the history
Fixes #412
  • Loading branch information
hadley committed Apr 12, 2012
1 parent b49056b commit b21b18b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* `geom_raster` and `annotation_raster` gain new `interpolate` argument for
controlling whether or not rasters are interpolated

* Added `plot` as an alias for `print` for ggplot objects.

* Visual tests have been moved to /visual_test and modified to work with the
Expand Down
21 changes: 16 additions & 5 deletions R/annotation-raster.r
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ NULL
#' location of raster
#' @param ymin,ymax y location (in data coordinates) giving vertical
#' location of raster
#' @param interpolate If \code{TRUE} interpolate linearly, if \code{FALSE}
#' (the default) don't interpolate.
#' @export
#' @examples
#' # Generate data
Expand All @@ -26,11 +28,20 @@ NULL
#' qplot(mpg, wt, data = mtcars) +
#' annotation_raster(rainbow, -Inf, Inf, -Inf, Inf) +
#' geom_point()
annotation_raster <- function (raster, xmin, xmax, ymin, ymax) {
#'
#' rainbow2 <- matrix(hcl(seq(0, 360, length = 10), 80, 70), nrow = 1)
#' qplot(mpg, wt, data = mtcars) +
#' annotation_raster(rainbow2, -Inf, Inf, -Inf, Inf) +
#' geom_point()
#' rainbow2 <- matrix(hcl(seq(0, 360, length = 10), 80, 70), nrow = 1)
#' qplot(mpg, wt, data = mtcars) +
#' annotation_raster(rainbow2, -Inf, Inf, -Inf, Inf, interpolate = TRUE) +
#' geom_point()
annotation_raster <- function (raster, xmin, xmax, ymin, ymax, interpolate = FALSE) {
raster <- as.raster(raster)
GeomRasterAnn$new(geom_params = list(raster = raster, xmin = xmin,
xmax = xmax, ymin = ymin, ymax = ymax), stat = "identity",
position = "identity", data = NULL, inherit.aes = TRUE)
xmax = xmax, ymin = ymin, ymax = ymax, interpolate = interpolate),
stat = "identity", position = "identity", data = NULL, inherit.aes = TRUE)
}

GeomRasterAnn <- proto(GeomRaster, {
Expand All @@ -40,7 +51,7 @@ GeomRasterAnn <- proto(GeomRaster, {
}

draw_groups <- function(., data, scales, coordinates, raster, xmin, xmax,
ymin, ymax, ...) {
ymin, ymax, interpolate = FALSE, ...) {
if (!inherits(coordinates, "cartesian")) {
stop("annotation_raster only works with Cartesian coordinates",
call. = FALSE)
Expand All @@ -53,6 +64,6 @@ GeomRasterAnn <- proto(GeomRaster, {

rasterGrob(raster, x_rng[1], y_rng[1],
diff(x_rng), diff(y_rng), default.units = "native",
just = c("left","bottom"), interpolate = FALSE)
just = c("left","bottom"), interpolate = interpolate)
}
})
13 changes: 9 additions & 4 deletions R/geom-raster.r
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ NULL
#' @param hjust,vjust horizontal and vertical justification of the grob. Each
#' justification value should be a number between 0 and 1. Defaults to 0.5
#' for both, centering each pixel over its data location.
#' @param interpolate If \code{TRUE} interpolate linearly, if \code{FALSE}
#' (the default) don't interpolate.
#' @export
#' @examples
#' \donttest{
Expand All @@ -28,6 +30,9 @@ NULL
#' df
#' }
#' qplot(x, y, data = pp(20), fill = z, geom = "raster")
#' # Interpolation worsens the apperance of this plot, but can help when
#' # rendering images.
#' qplot(x, y, data = pp(20), fill = z, geom = "raster", interpolate = TRUE)
#'
#' # For the special cases where it is applicable, geom_raster is much
#' # faster than geom_tile:
Expand All @@ -44,11 +49,11 @@ NULL
#' # zero padding
#' ggplot(df, aes(x, y, fill = z)) + geom_raster(hpad = 0, vpad = 0)
#' }
geom_raster <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity", hjust = 0.5, vjust = 0.5, ...) {
geom_raster <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity", hjust = 0.5, vjust = 0.5, interpolate = FALSE, ...) {
stopifnot(is.numeric(hjust), length(hjust) == 1)
stopifnot(is.numeric(vjust), length(vjust) == 1)

GeomRaster$new(mapping = mapping, data = data, stat = stat, position = position, hjust = hjust, vjust = vjust, ...)
GeomRaster$new(mapping = mapping, data = data, stat = stat, position = position, hjust = hjust, vjust = vjust, interpolate = interpolate, ...)
}

GeomRaster <- proto(Geom, {
Expand All @@ -68,7 +73,7 @@ GeomRaster <- proto(Geom, {
df
}

draw <- function(., data, scales, coordinates, hjust = 0.5, vjust = 0.5, ...) {
draw <- function(., data, scales, coordinates, hjust = 0.5, vjust = 0.5, interpolate = FALSE, ...) {
if (!inherits(coordinates, "cartesian")) {
stop("geom_raster only works with Cartesian coordinates", call. = FALSE)
}
Expand All @@ -83,7 +88,7 @@ GeomRaster <- proto(Geom, {

rasterGrob(raster, x = mean(x_rng), y = mean(y_rng),
width = diff(x_rng), height = diff(y_rng),
default.units = "native", interpolate = FALSE)
default.units = "native", interpolate = interpolate)
}


Expand Down
15 changes: 14 additions & 1 deletion man/annotation_raster.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
\alias{annotation_raster}
\title{Annotation: High-performance rectangular tiling.}
\usage{
annotation_raster(raster, xmin, xmax, ymin, ymax)
annotation_raster(raster, xmin, xmax, ymin, ymax,
interpolate = FALSE)
}
\arguments{
\item{raster}{raster object to display}
Expand All @@ -12,6 +13,9 @@

\item{ymin,ymax}{y location (in data coordinates) giving
vertical location of raster}

\item{interpolate}{If \code{TRUE} interpolate linearly,
if \code{FALSE} (the default) don't interpolate.}
}
\description{
This is a special version of \code{\link{geom_raster}}
Expand All @@ -33,5 +37,14 @@ qplot(mpg, wt, data = mtcars) +
qplot(mpg, wt, data = mtcars) +
annotation_raster(rainbow, -Inf, Inf, -Inf, Inf) +
geom_point()
rainbow2 <- matrix(hcl(seq(0, 360, length = 10), 80, 70), nrow = 1)
qplot(mpg, wt, data = mtcars) +
annotation_raster(rainbow2, -Inf, Inf, -Inf, Inf) +
geom_point()
rainbow2 <- matrix(hcl(seq(0, 360, length = 10), 80, 70), nrow = 1)
qplot(mpg, wt, data = mtcars) +
annotation_raster(rainbow2, -Inf, Inf, -Inf, Inf, interpolate = TRUE) +
geom_point()
}
8 changes: 7 additions & 1 deletion man/geom_raster.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
\usage{
geom_raster(mapping = NULL, data = NULL,
stat = "identity", position = "identity", hjust = 0.5,
vjust = 0.5, ...)
vjust = 0.5, interpolate = FALSE, ...)
}
\arguments{
\item{hjust,vjust}{horizontal and vertical justification
of the grob. Each justification value should be a number
between 0 and 1. Defaults to 0.5 for both, centering
each pixel over its data location.}

\item{interpolate}{If \code{TRUE} interpolate linearly,
if \code{FALSE} (the default) don't interpolate.}
\item{mapping}{The aesthetic mapping, usually constructed
with \code{\link{aes}} or \code{\link{aes_string}}. Only
needs to be set at the layer level if you are overriding
Expand Down Expand Up @@ -55,6 +58,9 @@ pp <- function (n,r=4) {
df
}
qplot(x, y, data = pp(20), fill = z, geom = "raster")
# Interpolation worsens the apperance of this plot, but can help when
# rendering images.
qplot(x, y, data = pp(20), fill = z, geom = "raster", interpolate = TRUE)
# For the special cases where it is applicable, geom_raster is much
# faster than geom_tile:
Expand Down

0 comments on commit b21b18b

Please sign in to comment.