1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/coord-map.R, R/coord-quickmap.R
\name{coord_map}
\alias{coord_map}
\alias{coord_quickmap}
\title{Map projections}
\usage{
coord_map(
projection = "mercator",
...,
parameters = NULL,
orientation = NULL,
xlim = NULL,
ylim = NULL,
clip = "on"
)
coord_quickmap(xlim = NULL, ylim = NULL, expand = TRUE, clip = "on")
}
\arguments{
\item{projection}{projection to use, see
\code{\link[mapproj:mapproject]{mapproj::mapproject()}} for list}
\item{..., parameters}{Other arguments passed on to
\code{\link[mapproj:mapproject]{mapproj::mapproject()}}. Use \code{...} for named parameters to
the projection, and \code{parameters} for unnamed parameters.
\code{...} is ignored if the \code{parameters} argument is present.}
\item{orientation}{projection orientation, which defaults to
\code{c(90, 0, mean(range(x)))}. This is not optimal for many
projections, so you will have to supply your own. See
\code{\link[mapproj:mapproject]{mapproj::mapproject()}} for more information.}
\item{xlim, ylim}{Manually specific x/y limits (in degrees of
longitude/latitude)}
\item{clip}{Should drawing be clipped to the extent of the plot panel? A
setting of \code{"on"} (the default) means yes, and a setting of \code{"off"}
means no. For details, please see \code{\link[=coord_cartesian]{coord_cartesian()}}.}
\item{expand}{If \code{TRUE}, the default, adds a small expansion factor to
the limits to ensure that data and axes don't overlap. If \code{FALSE},
limits are taken exactly from the data or \code{xlim}/\code{ylim}.}
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#superseded}{\figure{lifecycle-superseded.svg}{options: alt='[Superseded]'}}}{\strong{[Superseded]}}
\code{coord_map()} projects a portion of the earth, which is approximately
spherical, onto a flat 2D plane using any projection defined by the
\code{mapproj} package. Map projections do not, in general, preserve straight
lines, so this requires considerable computation. \code{coord_quickmap()} is a
quick approximation that does preserve straight lines. It works best for
smaller areas closer to the equator.
Both \code{coord_map()} and \code{coord_quickmap()}
are superseded by \code{\link[=coord_sf]{coord_sf()}}, and should no longer be used in new
code. All regular (non-sf) geoms can be used with \code{coord_sf()} by
setting the default coordinate system via the \code{default_crs} argument.
See also the examples for \code{\link[=annotation_map]{annotation_map()}} and \code{\link[=geom_map]{geom_map()}}.
}
\details{
Map projections must account for the fact that the actual length
(in km) of one degree of longitude varies between the equator and the pole.
Near the equator, the ratio between the lengths of one degree of latitude and
one degree of longitude is approximately 1. Near the pole, it tends
towards infinity because the length of one degree of longitude tends towards
0. For regions that span only a few degrees and are not too close to the
poles, setting the aspect ratio of the plot to the appropriate lat/lon ratio
approximates the usual mercator projection. This is what
\code{coord_quickmap()} does, and is much faster (particularly for complex
plots like \code{\link[=geom_tile]{geom_tile()}}) at the expense of correctness.
}
\examples{
if (require("maps")) {
nz <- map_data("nz")
# Prepare a map of NZ
nzmap <- ggplot(nz, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "white", colour = "black")
# Plot it in cartesian coordinates
nzmap
}
if (require("maps")) {
# With correct mercator projection
nzmap + coord_map()
}
if (require("maps")) {
# With the aspect ratio approximation
nzmap + coord_quickmap()
}
if (require("maps")) {
# Other projections
nzmap + coord_map("azequalarea", orientation = c(-36.92, 174.6, 0))
}
if (require("maps")) {
states <- map_data("state")
usamap <- ggplot(states, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black")
# Use cartesian coordinates
usamap
}
if (require("maps")) {
# With mercator projection
usamap + coord_map()
}
if (require("maps")) {
# See ?mapproject for coordinate systems and their parameters
usamap + coord_map("gilbert")
}
if (require("maps")) {
# For most projections, you'll need to set the orientation yourself
# as the automatic selection done by mapproject is not available to
# ggplot
usamap + coord_map("orthographic")
}
if (require("maps")) {
usamap + coord_map("conic", lat0 = 30)
}
if (require("maps")) {
usamap + coord_map("bonne", lat0 = 50)
}
\dontrun{
if (require("maps")) {
# World map, using geom_path instead of geom_polygon
world <- map_data("world")
worldmap <- ggplot(world, aes(x = long, y = lat, group = group)) +
geom_path() +
scale_y_continuous(breaks = (-2:2) * 30) +
scale_x_continuous(breaks = (-4:4) * 45)
# Orthographic projection with default orientation (looking down at North pole)
worldmap + coord_map("ortho")
}
if (require("maps")) {
# Looking up up at South Pole
worldmap + coord_map("ortho", orientation = c(-90, 0, 0))
}
if (require("maps")) {
# Centered on New York (currently has issues with closing polygons)
worldmap + coord_map("ortho", orientation = c(41, -74, 0))
}
}
}
\seealso{
The \href{https://ggplot2-book.org/maps#sec-polygonmaps}{polygon maps section} of the online ggplot2 book.
}
|