Browse files

Annotation layer that uses rasters

  • Loading branch information...
1 parent 8578fd0 commit 7a4883c6870dc6369607f2db4f3f2bf982c65075 @hadley committed Dec 1, 2011
Showing with 69 additions and 0 deletions.
  1. +1 −0 DESCRIPTION
  2. +1 −0 NAMESPACE
  3. +43 −0 R/annotation-raster.r
  4. +24 −0 man/annotation_raster.Rd
View
1 DESCRIPTION
@@ -185,3 +185,4 @@ Collate:
'stat-aggr-2d.r'
'stat-aggr-hex.r'
'geom-raster.r'
+ 'annotation-raster.r'
View
1 NAMESPACE
@@ -3,6 +3,7 @@ export(aes_all)
export(aes_string)
export(aes)
export(annotate)
+export(annotation_raster)
export(benchplot)
export(continuous_scale)
export(coord_cartesian)
View
43 R/annotation-raster.r
@@ -0,0 +1,43 @@
+#' @include geom-.r
+#' @include geom-raster.r
+NULL
+
+#' Annotation: High-performance rectangular tiling.
+#'
+#' This is a special version of \code{\link{geom_raster}} optimised for static
+#' anotations that are the same in every panel. These anotations will not
+#' affect scales (i.e. the x and y axes will not grow to cover the range
+#' of the raster, and the raster must already have it's own colours).
+#'
+#' Most useful for adding bitmap images.
+#'
+#' @export
+#' @examples
+#' # Generate data
+#' rainbow <- matrix(hcl(seq(0, 360, length = 50 * 50), 80, 70), nrow = 50)
+#' qplot(mpg, wt, data = mtcars) +
+#' annotation_raster(redGradient, 15, 20, 3, 4)
+annotation_raster <- function (raster, xmin, xmax, ymin, ymax) {
+ raster <- as.raster(raster)
+ GeomRasterAnn$new(geom_params = list(raster = raster, xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), stat = "identity", position = "identity")
+}
+
+GeomRasterAnn <- proto(GeomRaster, {
+ objname <- "raster_ann"
+ draw_groups <- function(., data, scales, coordinates, raster, xmin, xmax,
+ ymin, ymax, ...) {
+ if (!inherits(coordinates, "cartesian")) {
+ stop("annotation_raster only works with Cartesian coordinates",
+ call. = FALSE)
+ }
+ corners <- data.frame(x = c(xmin, xmax), y = c(ymin, ymax))
+ data <- coord_transform(coordinates, corners, scales)
+
+ x_rng <- range(data$x, na.rm = TRUE)
+ y_rng <- range(data$y, na.rm = TRUE)
+
+ rasterGrob(raster, x_rng[1], y_rng[1],
+ diff(x_rng), diff(y_rng), default.units = "native",
+ just = c("left","bottom"), interpolate = FALSE)
+ }
+})
View
24 man/annotation_raster.Rd
@@ -0,0 +1,24 @@
+\name{annotation_raster}
+\alias{annotation_raster}
+\title{Annotation: High-performance rectangular tiling.}
+\usage{
+ annotation_raster(raster, xmin, xmax, ymin, ymax)
+}
+\description{
+ This is a special version of \code{\link{geom_raster}}
+ optimised for static anotations that are the same in
+ every panel. These anotations will not affect scales
+ (i.e. the x and y axes will not grow to cover the range
+ of the raster, and the raster must already have it's own
+ colours).
+}
+\details{
+ Most useful for adding bitmap images.
+}
+\examples{
+# Generate data
+rainbow <- matrix(hcl(seq(0, 360, length = 50 * 50), 80, 70), nrow = 50)
+qplot(mpg, wt, data = mtcars) +
+ annotation_raster(redGradient, 15, 20, 3, 4)
+}
+

0 comments on commit 7a4883c

Please sign in to comment.