Skip to contents

geom_sf_wash() paints a soft band of colour along the edges of simple features, in the manner of the coastal tints on archival maps. The wash is strongest at the edge of each shape and fades to nothing, with an uneven reach and a slight pigment grain. It draws only the wash, so it is usually paired with ggplot2::geom_sf() for the shapes themselves.

Usage

geom_sf_wash(
  mapping = aes(),
  data = NULL,
  stat = "sf",
  position = "identity",
  ...,
  width = 4,
  direction = c("out", "in", "both"),
  mask = NULL,
  mask_tolerance = 0.5,
  monochrome = FALSE,
  fade = 1.6,
  roughness = 0.6,
  reach_size = 1.75 * width,
  granulation = 0.5,
  grain_size = 0.25,
  dpi = 300,
  seed = NULL,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

Arguments

mapping

Set of aesthetic mappings created by aes(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

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

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (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, the stat argument can be used to override the default coupling between geoms and stats. The stat argument accepts the following:

  • A Stat ggproto subclass, for example StatCount.

  • A string naming the stat. To give the stat as a string, strip the function name of the stat_ prefix. For example, to use stat_count(), give the stat as "count".

  • For more information and other ways to specify the stat, see the layer stat documentation.

position

A position adjustment to use on the data for this layer. This can be used in various ways, including to prevent overplotting and improving the display. The position argument accepts the following:

  • The result of calling a position function, such as position_jitter(). This method allows for passing extra arguments to the position.

  • A string naming the position adjustment. To give the position as a string, strip the function name of the position_ prefix. For example, to use position_jitter(), give the position as "jitter".

  • For more information and other ways to specify the position, see the layer position documentation.

...

Other arguments passed on to layer()'s params argument. These arguments broadly fall into one of 4 categories below. Notably, further arguments to the position argument, or aesthetics that are required can not be passed through .... Unknown arguments that are not part of the 4 categories below are ignored.

  • Static aesthetics that are not mapped to a scale, but are at a fixed value and apply to the layer as a whole. For example, colour = "red" or linewidth = 3. The geom's documentation has an Aesthetics section that lists the available options. The 'required' aesthetics cannot be passed on to the params. Please note that while passing unmapped aesthetics as vectors is technically possible, the order and required length is not guaranteed to be parallel to the input data.

  • When constructing a layer using a stat_*() function, the ... argument can be used to pass on parameters to the geom part of the layer. An example of this is stat_density(geom = "area", outline.type = "both"). The geom's documentation lists which parameters it can accept.

  • Inversely, when constructing a layer using a geom_*() function, the ... argument can be used to pass on parameters to the stat part of the layer. An example of this is geom_area(stat = "density", adjust = 0.5). The stat's documentation lists which parameters it can accept.

  • The key_glyph argument of layer() may also be passed on through .... This can be one of the functions described as key glyphs, to change the display of the layer in the legend.

width

Nominal reach of the wash, in millimetres.

direction

Where to paint the wash: "out" for outside the shapes, "in" for inside them, or "both".

mask

An sf or sfc object of polygons that must get no wash, such as the land next to the shapes. If NULL, the default, the wash is painted along every edge. See the section on coasts and land borders.

mask_tolerance

Distance to grow the mask on every side, in millimetres. This closes narrow gaps between the mask and the shapes.

monochrome

If TRUE, paint in greys instead of colours. Each colour becomes the grey of the same lightness, so mapped fills keep their order from light to dark.

fade

Exponent of the fade. Values above 1 concentrate pigment near the edge of the shape.

roughness

Variation in how far the wash reaches, between 0 (an even band) and 1.

reach_size

Approximate size of the variations in reach, in millimetres.

granulation

Strength of the pigment grain, where 0 is none.

grain_size

Approximate size of the grain, in millimetres.

dpi

Resolution of the wash, in cells per inch.

seed

A single integer that fixes the noise in the wash. If NULL, the layer chooses a seed at random when you create the layer.

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, 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, and TRUE always includes.

You can also set this to one of "polygon", "line", and "point" to override the default legend.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. annotation_borders().

Value

A list holding a ggplot2 layer and a default ggplot2::coord_sf(), to be added to a plot.

Details

ggwatercolor computes the wash when the device draws the plot. For this reason, width, reach_size, and grain_size are lengths on the page in millimetres. The wash keeps the same appearance in any map projection, for any area shown, and in a panel of any size.

The layer treats shapes that share a fill and alpha as one shape, so the borders between them receive no wash. With direction = "out", the wash never covers any shape in the layer.

The result is a raster image. Raise dpi for print-quality output, or lower it to draw faster. For a very large panel, ggwatercolor lowers the resolution and gives a warning. The ggwatercolor.max_cells option sets the limit, which is 25 million cells by default.

Coasts and land borders

The layer cannot tell a coast from a land border, so it paints along every edge of a shape. There are two ways to keep the wash on the coast.

  • To tint every coast, give the layer all the land in view, with one fill. The borders between the pieces of land then receive no wash.

  • To tint the coast of some shapes only, give the other land as mask. The wash is not painted on the mask. With direction = "in" or "both", the inside of a shape also gets no wash along an edge that it shares with the mask.

Borders from different sources seldom match exactly, and wash can show in the narrow gaps between a shape and the mask. For this reason the mask grows by mask_tolerance on every side before it is used. Use mask_tolerance = 0 when the borders match exactly. The mask does not change the limits of the plot.

Aesthetics

geom_sf_wash() understands the following aesthetics:

  • geometry. The layer requires this aesthetic and finds it automatically in sf data.

  • fill, the colour of the wash.

  • alpha, the opacity of the wash at the edge of the shape.

Examples

library(ggplot2)

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
coast <- sf::st_union(nc)

ggplot() +
  geom_sf_wash(data = coast, seed = 1) +
  geom_sf(data = coast, fill = "#F8F5EA") +
  theme_void()


# Paint in greys
ggplot() +
  geom_sf_wash(data = coast, monochrome = TRUE, seed = 1) +
  geom_sf(data = coast, fill = NA) +
  theme_void()


# Keep the wash on the coast of some counties. The other counties are
# the mask.
sounds <- nc$NAME %in% c("Dare", "Hyde", "Carteret", "Pamlico", "Beaufort")

ggplot() +
  geom_sf_wash(data = nc[sounds, ], mask = nc[!sounds, ], seed = 1) +
  geom_sf(data = nc, fill = NA, colour = "grey70") +
  geom_sf(data = nc[sounds, ], fill = NA) +
  theme_void()


# Tint the inside of each shape instead
ggplot(nc[1:12, ]) +
  geom_sf_wash(aes(fill = NAME), direction = "in", width = 2, seed = 1) +
  geom_sf(fill = NA) +
  theme_void() +
  theme(legend.position = "none")