Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

custom grob and doc added

  • Loading branch information...
commit 82314cf1e6b242b4cb685792fba5997853d66b9d 1 parent f77f22b
baptiste baptiste authored
1  DESCRIPTION
View
@@ -197,3 +197,4 @@ Collate:
'autoplot.r'
'zzz.r'
'fortify-multcomp.r'
+ 'annotation-custom.r'
1  NAMESPACE
View
@@ -4,6 +4,7 @@ export(aes_auto)
export(aes_string)
export(aes)
export(annotate)
+export(annotation_custom)
export(annotation_map)
export(annotation_raster)
export(autoplot)
65 R/annotation-custom.r
View
@@ -0,0 +1,65 @@
+#' @include geom-.r
+NULL
+
+#' Annotation: Custom grob.
+#'
+#' This is a special geom intended for use as static annnotations
+#' 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 grob, and the grob will not be modified by any ggplot settings or mappings).
+#'
+#' Most useful for adding tables, inset plots, and other grid-based decorations.
+#'
+#' @param grob grob to display
+#' @param xmin,xmax x location (in data coordinates) giving horizontal
+#' location of raster
+#' @param ymin,ymax y location (in data coordinates) giving vertical
+#' location of raster
+#' @export
+#' @note \code{annotation_custom} expects the grob to fill the entire viewport
+#' defined by xmin, xmax, ymin, ymax. Grobs with a different (absolute) size
+#' will be center-justified in that region.
+#' Inf values can be used to fill the full plot panel (see examples).
+#' @examples
+#' # Dummy plot
+#' base <- qplot(1:10, 1:10, geom = "blank") + theme_bw()
+#' # Adding a table
+#' \dontrun{
+#' require(gridExtra)
+#' base + annotation_custom(grob = tableGrob(head(iris[ ,1:3])),
+#' xmin = 3, xmax = 6, ymin = 2, ymax = 8)
+#' }
+#' # Inset plot
+#' g <- ggplotGrob(qplot(1, 1) + opts(plot.background = theme_rect(col = "black")))
+#' base + annotation_custom(grob = g, xmin = 1, xmax = 10, ymin = 8, ymax = 10)
+#'
+annotation_custom <- function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) {
+ GeomCustomAnn$new(geom_params = list(grob = grob, xmin = xmin,
+ xmax = xmax, ymin = ymin, ymax = ymax), stat = "identity",
+ position = "identity", data = NULL, inherit.aes = TRUE)
+}
+
+GeomCustomAnn <- proto(Geom, {
+ objname <- "custom_ann"
+
+ draw_groups <- function(., data, scales, coordinates, grob, xmin, xmax,
+ ymin, ymax, ...) {
+ if (!inherits(coordinates, "cartesian")) {
+ stop("annotation_custom 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)
+
+ vp <- viewport(x = mean(x_rng), y = mean(y_rng),
+ width = diff(x_rng), height = diff(y_rng),
+ just = c("center","center"))
+ editGrob(grob, vp = vp)
+ }
+
+ default_aes <- function(.)
+ aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
+})
49 man/annotation_custom.Rd
View
@@ -0,0 +1,49 @@
+\name{annotation_custom}
+\alias{annotation_custom}
+\title{Annotation: Custom grob.}
+\usage{
+ annotation_custom(grob, xmin = -Inf, xmax = Inf,
+ ymin = -Inf, ymax = Inf)
+}
+\arguments{
+ \item{grob}{grob to display}
+
+ \item{xmin,xmax}{x location (in data coordinates) giving
+ horizontal location of raster}
+
+ \item{ymin,ymax}{y location (in data coordinates) giving
+ vertical location of raster}
+}
+\description{
+ This is a special geom intended for use as static
+ annnotations 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 grob, and the
+ grob will not be modified by any ggplot settings or
+ mappings).
+}
+\details{
+ Most useful for adding tables, inset plots, and other
+ grid-based decorations.
+}
+\note{
+ \code{annotation_custom} expects the grob to fill the
+ entire viewport defined by xmin, xmax, ymin, ymax. Grobs
+ with a different (absolute) size will be center-justified
+ in that region. Inf values can be used to fill the full
+ plot panel (see examples).
+}
+\examples{
+# Dummy plot
+base <- qplot(1:10, 1:10, geom = "blank") + theme_bw()
+# Adding a table
+\dontrun{
+require(gridExtra)
+base + annotation_custom(grob = tableGrob(head(iris[ ,1:3])),
+ xmin = 3, xmax = 6, ymin = 2, ymax = 8)
+}
+# Inset plot
+g <- ggplotGrob(qplot(1, 1) + opts(plot.background = theme_rect(col = "black")))
+base + annotation_custom(grob = g, xmin = 1, xmax = 10, ymin = 8, ymax = 10)
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.