Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Polygons from a reference map

Source:R/geom-map.R
geom_map.Rd

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 byaes(). If specified andinherit.aes = TRUE (the default), it is combined with the default mappingat the top level of the plot. You must supplymapping if there is no plotmapping.

data

The data to be displayed in this layer. There are threeoptions:

IfNULL, the default, the data is inherited from the plotdata as specified in the call toggplot().

Adata.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.

Afunction will be called with a single argument,the plot data. The return value must be adata.frame, andwill be used as the layer data. Afunction can be createdfrom aformula (e.g.~ head(.x, 10)).

stat

The statistical transformation to use on the data for this layer.When using ageom_*() function to construct a layer, thestatargument can be used to override the default coupling between geoms andstats. Thestat argument accepts the following:

  • AStat ggproto subclass, for exampleStatCount.

  • A string naming the stat. To give the stat as a string, strip thefunction name of thestat_ 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 tolayer()'sparams argument. Thesearguments broadly fall into one of 4 categories below. Notably, furtherarguments to theposition argument, 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 usingastat_*() function, the... argument can be used to pass onparameters to thegeom part 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 ageom_*() function, the... argument can be used to pass on parametersto thestat part of the layer. An example of this isgeom_area(stat = "density", adjust = 0.5). The stat's documentationlists which parameters it can accept.

  • Thekey_glyph argument 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 usingfortify() on a spatial object.It must contain columnsx orlong,y orlat, andregion orid.

na.rm

IfFALSE, 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.FALSE never includes, andTRUE always 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

IfFALSE, 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
alphaNA
colour→ viatheme()
fill→ viatheme()
group→ inferred
linetype→ viatheme()
linewidth→ viatheme()
subgroupNULL

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)}

[8]ページ先頭

©2009-2025 Movatter.jp