Skip to content
Browse files

First stab at geom_map

  • Loading branch information...
1 parent 7a4883c commit 15896d2cd5529c3a9f0f0ecc81ced81924b537d4 @hadley committed Dec 1, 2011
Showing with 132 additions and 6 deletions.
  1. +7 −6 DESCRIPTION
  2. +1 −0 NAMESPACE
  3. +8 −0 NEWS
  4. +79 −0 R/geom-map.r
  5. +3 −0 R/geom-raster.r
  6. +34 −0 man/geom_map.Rd
View
13 DESCRIPTION
@@ -80,6 +80,8 @@ Collate:
'geom-hex.r'
'geom-hline.r'
'geom-linerange.r'
+ 'geom-polygon.r'
+ 'geom-map.r'
'geom-path-.r'
'geom-path-contour.r'
'geom-path-density2d.r'
@@ -88,7 +90,6 @@ Collate:
'geom-point-.r'
'geom-point-jitter.r'
'geom-pointrange.r'
- 'geom-polygon.r'
'geom-quantile.r'
'geom-rect.r'
'geom-ribbon-.r'
@@ -102,6 +103,9 @@ Collate:
'ggplot2.r'
'grob-absolute.r'
'grob-null.r'
+ 'guide-colorbar.r'
+ 'guide-legend.r'
+ 'guides-.r'
'guides-axis.r'
'guides-grid.r'
'labels.r'
@@ -143,6 +147,8 @@ Collate:
'scale-size.r'
'scales-.r'
'stat-.r'
+ 'stat-aggr-2d.r'
+ 'stat-aggr-hex.r'
'stat-bin.r'
'stat-bin2d.r'
'stat-binhex.r'
@@ -179,10 +185,5 @@ Collate:
'utilities.r'
'xxx-digest.r'
'zxx.r'
- 'guides-.r'
- 'guide-legend.r'
- 'guide-colorbar.r'
- 'stat-aggr-2d.r'
- 'stat-aggr-hex.r'
'geom-raster.r'
'annotation-raster.r'
View
1 NAMESPACE
@@ -42,6 +42,7 @@ export(geom_hline)
export(geom_jitter)
export(geom_line)
export(geom_linerange)
+export(geom_map)
export(geom_path)
export(geom_point)
export(geom_pointrange)
View
8 NEWS
@@ -1,6 +1,14 @@
ggplot2 0.9.0
----------------------------------------------------------------
+NEW GEOMS/ANNOTATIONS
+
+* `geom_map`
+
+* `geom_raster`
+
+* `annotation_raster`
+
MINOR CHANGES
* `geom_text` now supports `fontfamily`, `fontface`, and `lineheight`
View
79 R/geom-map.r
@@ -0,0 +1,79 @@
+#' @include geom-polygon.r
+NULL
+
+#' Polygons from a reference map.
+#'
+#' Does not affect position scales.
+#'
+#' @export
+#' @examples
+#' # When using geom_polygon, you will typically need two data frames:
+#' # one contains the coordinates of each polygon (positions), and the
+#' # other the values associated with each polygon (values). An id
+#' # variable links the two together
+#'
+#' ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
+#'
+#' values <- data.frame(
+#' id = ids,
+#' value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5)
+#' )
+#'
+#' positions <- data.frame(
+#' id = rep(ids, each = 4),
+#' x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
+#' 0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
+#' y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
+#' 2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
+#' )
+#'
+#' ggplot(values) + geom_map(aes(map_id = id), map = positions) +
+#' expand_limits(positions)
+#' ggplot(values, aes(fill = value)) +
+#' geom_map(aes(map_id = id), map = positions) +
+#' expand_limits(positions)
+#' ggplot(values, aes(fill = value)) +
+#' geom_map(aes(map_id = id), map = positions) +
+#' expand_limits(positions) + ylim(0, 3)
+#'
+#' # Better example
+#' crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
+#' crimesm <- melt(crimes, id = 1)
+#' if (require(maps)) {
+#' states_map <- map_data("state")
+#' ggplot(crimes, aes(map_id = state)) + geom_map(aes(fill = Murder), map = states_map) + expand_limits(x = states_map$long, y = states_map$lat)
+#' ggplot(crimesm, aes(map_id = state)) + geom_map(aes(fill = value), map = states_map) + expand_limits(x = states_map$long, y = states_map$lat) + facet_wrap( ~ variable)
+#' }
+#'
+geom_map <- function(mapping = NULL, data = NULL, map, stat = "identity", ...) {
+
+ # Get map input into correct form
+ stopifnot(is.data.frame(map))
+ if (!is.null(map$lat)) map$y <- map$lat
+ if (!is.null(map$long)) map$x <- map$long
+ if (!is.null(map$region)) map$id <- map$region
+ stopifnot(all(c("x", "y", "id") %in% names(map)))
+ map <- split(map, map$id)
+
+ GeomMap$new(geom_params = list(map = map), mapping = mapping, data = data, stat = stat, ...)
+}
+
+GeomMap <- proto(GeomPolygon, {
+ objname <- "map"
+
+ draw_groups <- function(., data, scales, coordinates, map, ...) {
+ data <- data[data$map_id %in% names(map), , drop = FALSE]
+
+ polys <- rbind.fill(map[data$map_id])
+ id <- match(polys$group, unique(polys$group))
+ coords <- coord_munch(coordinates, polys, scales)
+
+ polygonGrob(coords$x, coords$y, default.units = "native", id = id,
+ gp = gpar(
+ col = data$colour, fill = alpha(data$fill, data$alpha),
+ lwd = data$size * .pt))
+ }
+
+ required_aes <- c("map_id")
+
+})
View
3 R/geom-raster.r
@@ -1,3 +1,6 @@
+#' @include geom-.r
+NULL
+
#' High-performance rectangular tiling.
#'
#' This is a special case of \code{\link{geom_tile}} where all tiles are
View
34 man/geom_map.Rd
@@ -0,0 +1,34 @@
+\name{geom_map}
+\alias{geom_map}
+\title{Polygons from a reference map.}
+\usage{
+ geom_map(mapping = NULL, data = NULL, map,
+ stat = "identity", ...)
+}
+\description{
+ Does not affect position scales.
+}
+\examples{
+# When using geom_polygon, you will typically need two data frames:
+# one contains the coordinates of each polygon (positions), and the
+# other the values associated with each polygon (values). An id
+# variable links the two together
+
+ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
+
+values <- data.frame(
+ id = ids,
+ value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5)
+)
+
+positions <- data.frame(
+ id = rep(ids, each = 4),
+ x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
+ 0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
+ y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
+ 2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
+)
+
+ggplot(values, aes(x=x, y=y)) + geom_map(aes(map_id = id), map = positions)
+}
+

0 comments on commit 15896d2

Please sign in to comment.
Something went wrong with that request. Please try again.