Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 102 lines (98 sloc) 3.688 kB
953fe47 @hadley Convert all documentation to roxygen
authored
1 #' Fortify method for map objects.
1244716 @hadley Trim whitespace
authored
2 #'
a43f684 @djmurphy420 Typo fixes in several help files.
djmurphy420 authored
3 #' This function turns a map into a data frame that can more easily be
953fe47 @hadley Convert all documentation to roxygen
authored
4 #' plotted with ggplot2.
1244716 @hadley Trim whitespace
authored
5 #'
3c63ff7 @hadley Convert from @S3method to @export
authored
6 #' @export
08461b8 @hadley Fix documentation cross-refs.
authored
7 #' @seealso \code{\link{map_data}} and \code{\link{borders}}
953fe47 @hadley Convert all documentation to roxygen
authored
8 #' @param model map object
9 #' @param data not used by this method
10 #' @param ... not used by this method
11 #' @examples
d53d57a @hadley Require maps for fortify maps example
authored
12 #' if (require("maps")) {
953fe47 @hadley Convert all documentation to roxygen
authored
13 #' ca <- map("county", "ca", plot = FALSE, fill = TRUE)
14 #' head(fortify(ca))
bbfc5be @hrbrmstr finished removing qplot from remaining functions except for the expli…
hrbrmstr authored
15 #' ggplot(ca, aes(long, lat)) +
16 #' geom_polygon(aes(group = group))
953fe47 @hadley Convert all documentation to roxygen
authored
17 #'
18 #' tx <- map("county", "texas", plot = FALSE, fill = TRUE)
19 #' head(fortify(tx))
bbfc5be @hrbrmstr finished removing qplot from remaining functions except for the expli…
hrbrmstr authored
20 #' ggplot(tx, aes(long, lat)) +
21 #' geom_polygon(aes(group = group), colour = "white")
d53d57a @hadley Require maps for fortify maps example
authored
22 #' }
518ebcc @hadley Fortify tidying. Add fortify.map from book code
authored
23 fortify.map <- function(model, data, ...) {
24 df <- as.data.frame(model[c("x", "y")])
0a1a906 @hadley More map stuff
authored
25 names(df) <- c("long", "lat")
559012f @hadley Fix dumb typo
authored
26 df$group <- cumsum(is.na(df$long) & is.na(df$lat)) + 1
0a1a906 @hadley More map stuff
authored
27 df$order <- 1:nrow(df)
1244716 @hadley Trim whitespace
authored
28
0a1a906 @hadley More map stuff
authored
29 names <- do.call("rbind", lapply(strsplit(model$names, "[:,]"), "[", 1:2))
30 df$region <- names[df$group, 1]
31 df$subregion <- names[df$group, 2]
314b143 @hadley Be explicit about functions in base packages. #1197
authored
32 df[stats::complete.cases(df$lat, df$long), ]
518ebcc @hadley Fortify tidying. Add fortify.map from book code
authored
33 }
34
953fe47 @hadley Convert all documentation to roxygen
authored
35 #' Create a data frame of map data.
1244716 @hadley Trim whitespace
authored
36 #'
37 #' @param map name of map provided by the \pkg{maps} package. These
08461b8 @hadley Fix documentation cross-refs.
authored
38 #' include \code{\link[maps]{county}}, \code{\link[maps]{france}},
1244716 @hadley Trim whitespace
authored
39 #' \code{\link[maps]{italy}}, \code{\link[maps]{nz}},
40 #' \code{\link[maps]{state}}, \code{\link[maps]{usa}},
08461b8 @hadley Fix documentation cross-refs.
authored
41 #' \code{\link[maps]{world}}, \code{\link[maps]{world2}}.
953fe47 @hadley Convert all documentation to roxygen
authored
42 #' @param region name of subregions to include. Defaults to \code{.} which
08461b8 @hadley Fix documentation cross-refs.
authored
43 #' includes all subregion. See documentation for \code{\link[maps]{map}}
44 #' for more details.
953fe47 @hadley Convert all documentation to roxygen
authored
45 #' @param exact should the \code{region} be treated as a regular expression
46 #' (\code{FALSE}) or as a fixed string (\code{TRUE}).
f69e85c @hadley Pass ... in map_data (Fixes #223)
authored
47 #' @param ... all other arguments passed on to \code{\link[maps]{map}}
b1f1d92 @hadley More namespace/example fixes
authored
48 #' @export
953fe47 @hadley Convert all documentation to roxygen
authored
49 #' @examples
b1f1d92 @hadley More namespace/example fixes
authored
50 #' if (require("maps")) {
953fe47 @hadley Convert all documentation to roxygen
authored
51 #' states <- map_data("state")
52 #' arrests <- USArrests
53 #' names(arrests) <- tolower(names(arrests))
54 #' arrests$region <- tolower(rownames(USArrests))
1244716 @hadley Trim whitespace
authored
55 #'
953fe47 @hadley Convert all documentation to roxygen
authored
56 #' choro <- merge(states, arrests, sort = FALSE, by = "region")
57 #' choro <- choro[order(choro$order), ]
bbfc5be @hrbrmstr finished removing qplot from remaining functions except for the expli…
hrbrmstr authored
58 #' ggplot(choro, aes(long, lat)) +
59 #' geom_polygon(aes(group = group, fill = assault)) +
60 #' coord_map("albers", at0 = 45.5, lat1 = 29.5)
61 #'
62 #' ggplot(choro, aes(long, lat)) +
63 #' geom_polygon(aes(group = group, fill = assault / murder)) +
64 #' coord_map("albers", at0 = 45.5, lat1 = 29.5)
b1f1d92 @hadley More namespace/example fixes
authored
65 #' }
f69e85c @hadley Pass ... in map_data (Fixes #223)
authored
66 map_data <- function(map, region = ".", exact = FALSE, ...) {
9ebdd0f @hadley Better implementation of try_require. Minimise use.
authored
67 try_require("maps", "map_data")
f69e85c @hadley Pass ... in map_data (Fixes #223)
authored
68 fortify(map(map, region, exact = exact, plot = FALSE, fill = TRUE, ...))
0a1a906 @hadley More map stuff
authored
69 }
f47bffa @hadley Add missing borders function back in
authored
70
71 #' Create a layer of map borders.
1244716 @hadley Trim whitespace
authored
72 #'
f47bffa @hadley Add missing borders function back in
authored
73 #' @param database map data, see \code{\link[maps]{map}} for details
74 #' @param regions map region
75 #' @param fill fill colour
76 #' @param colour border colour
77 #' @param ... other arguments passed onto \code{\link{geom_polygon}}
78 #' @export
79 #' @examples
80 #' if (require("maps")) {
81 #'
82 #' ia <- map_data("county", "iowa")
83 #' mid_range <- function(x) mean(range(x))
4c08b76 @hadley Don't attach packages in examples.
authored
84 #' seats <- plyr::ddply(ia, "subregion", plyr::colwise(mid_range, c("lat", "long")))
f47bffa @hadley Add missing borders function back in
authored
85 #' ggplot(ia, aes(long, lat)) +
86 #' geom_polygon(aes(group = group), fill = NA, colour = "grey60") +
87 #' geom_text(aes(label = subregion), data = seats, size = 2, angle = 45)
88 #'
89 #' data(us.cities)
90 #' capitals <- subset(us.cities, capital == 2)
91 #' ggplot(capitals, aes(long, lat)) +
92 #' borders("state") +
93 #' geom_point(aes(size = pop)) +
e332c13 @hadley Use aes_q in borders to eliminate check warning
authored
94 #' scale_size_area()
f47bffa @hadley Add missing borders function back in
authored
95 #'
96 #' }
97 borders <- function(database = "world", regions = ".", fill = NA, colour = "grey50", ...) {
98 df <- map_data(database, regions)
5966673 @hadley Use aes_ instead of aes_q
authored
99 geom_polygon(aes_(~long, ~lat, group = ~group), data = df,
05b278e @hadley Don't inherit aesthetics in geom_polygon
authored
100 fill = fill, colour = colour, ..., inherit.aes = FALSE)
f47bffa @hadley Add missing borders function back in
authored
101 }
Something went wrong with that request. Please try again.