Display polygons as a map. This is meant as annotation, so it does notaffect position scales. Note that this function predates thegeom_sf()framework and does not work with sf geometry columns as input. However,it can be used in conjunction withgeom_sf() layers and/orcoord_sf() (see examples).
Usage
geom_map( mapping=NULL, data=NULL, stat="identity",...,map, na.rm=FALSE, show.legend=NA, inherit.aes=TRUE)Arguments
- mapping
Set of aesthetic mappings created by
aes(). If specified andinherit.aes = TRUE(the default), it is combined with the default mappingat the top level of the plot. You must supplymappingif there is no plotmapping.- data
The data to be displayed in this layer. There are threeoptions:
If
NULL, the default, the data is inherited from the plotdata as specified in the call toggplot().A
data.frame, or other object, will override the plotdata. All objects will be fortified to produce a data frame. Seefortify()for which variables will be created.A
functionwill be called with a single argument,the plot data. The return value must be adata.frame, andwill be used as the layer data. Afunctioncan be createdfrom aformula(e.g.~ head(.x, 10)).- stat
The statistical transformation to use on the data for this layer.When using a
geom_*()function to construct a layer, thestatargument can be used to override the default coupling between geoms andstats. Thestatargument accepts the following:A
Statggproto subclass, for exampleStatCount.A string naming the stat. To give the stat as a string, strip thefunction name of the
stat_prefix. For example, to usestat_count(),give the stat as"count".For more information and other ways to specify the stat, see thelayer stat documentation.
- ...
Other arguments passed on to
layer()'sparamsargument. Thesearguments broadly fall into one of 4 categories below. Notably, furtherarguments to thepositionargument, or aesthetics that are requiredcannot be passed through.... Unknown arguments that are not partof the 4 categories below are ignored.Static aesthetics that are not mapped to a scale, but are at a fixedvalue and apply to the layer as a whole. For example,
colour = "red"orlinewidth = 3. The geom's documentation has anAestheticssection that lists the available options. The 'required' aestheticscannot be passed on to theparams. Please note that while passingunmapped aesthetics as vectors is technically possible, the order andrequired length is not guaranteed to be parallel to the input data.When constructing a layer usinga
stat_*()function, the...argument can be used to pass onparameters to thegeompart of the layer. An example of this isstat_density(geom = "area", outline.type = "both"). The geom'sdocumentation lists which parameters it can accept.Inversely, when constructing a layer using a
geom_*()function, the...argument can be used to pass on parametersto thestatpart of the layer. An example of this isgeom_area(stat = "density", adjust = 0.5). The stat's documentationlists which parameters it can accept.The
key_glyphargument oflayer()may also be passed on through.... This can be one of the functions described askey glyphs, to change the display of the layer in the legend.
- map
Data frame that contains the map coordinates. This willtypically be created using
fortify()on a spatial object.It must contain columnsxorlong,yorlat, andregionorid.- na.rm
If
FALSE, the default, missing values are removed witha warning. IfTRUE, missing values are silently removed.- show.legend
logical. Should this layer be included in the legends?
NA, the default, includes if any aesthetics are mapped.FALSEnever includes, andTRUEalways includes.It can also be a named logical vector to finely select the aesthetics todisplay. To include legend keys for all levels, evenwhen no data exists, useTRUE. IfNA, all levels are shown in legend,but unobserved levels are omitted.- inherit.aes
If
FALSE, overrides the default aesthetics,rather than combining with them. This is most useful for helper functionsthat define both data and aesthetics and shouldn't inherit behaviour fromthe default plot specification, e.g.annotation_borders().
Aesthetics
geom_map() understands the following aesthetics. Required aesthetics are displayed in bold and defaults are displayed for optional aesthetics:
| • | map_id | |
| • | alpha | →NA |
| • | colour | → viatheme() |
| • | fill | → viatheme() |
| • | group | → inferred |
| • | linetype | → viatheme() |
| • | linewidth | → viatheme() |
| • | subgroup | →NULL |
Learn more about setting these aesthetics invignette("ggplot2-specs").
Examples
# First, a made-up example containing a few polygons, to explain# how `geom_map()` works. It requires two data frames:# One contains the coordinates of each polygon (`positions`), and is# provided via the `map` argument. The other contains 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)
# Now some examples with real mapsif(require(maps)){crimes<-data.frame(state=tolower(rownames(USArrests)),USArrests)# Equivalent to crimes |> tidyr::pivot_longer(Murder:Rape)vars<-lapply(names(crimes)[-1],function(j){data.frame(state=crimes$state, variable=j, value=crimes[[j]])})crimes_long<-do.call("rbind",vars)states_map<-map_data("state")# without geospatial coordinate system, the resulting plot# looks weirdggplot(crimes,aes(map_id=state))+geom_map(aes(fill=Murder), map=states_map)+expand_limits(x=states_map$long, y=states_map$lat)# in combination with `coord_sf()` we get an appropriate resultggplot(crimes,aes(map_id=state))+geom_map(aes(fill=Murder), map=states_map)+# crs = 5070 is a Conus Albers projection for North America,# see: https://epsg.io/5070# default_crs = 4326 tells coord_sf() that the input map data# are in longitude-latitude formatcoord_sf( crs=5070, default_crs=4326, xlim=c(-125,-70), ylim=c(25,52))ggplot(crimes_long,aes(map_id=state))+geom_map(aes(fill=value), map=states_map)+coord_sf( crs=5070, default_crs=4326, xlim=c(-125,-70), ylim=c(25,52))+facet_wrap(~variable)}
