ggwatercolor draws ggplot2 maps in the style of watercolor paint.
This article shows the map layers in the package and the settings that
change how they look. For ordinary charts, see
vignette("charts").
Washes and fills
The package has two kinds of paint layer.
- A wash is a band of colour along the edge of a shape. The wash is
strongest at the edge and fades to nothing.
geom_sf_wash()paints washes. - A fill covers the inside of a shape with uneven paint.
geom_sf_watercolor()paints fills.
Each layer draws only paint. To draw outlines, add
geom_sf() with fill = NA.
ggplot(hawaii) +
geom_sf_watercolor(seed = 1929) +
geom_sf_wash(seed = 1929) +
geom_sf(fill = NA, colour = "#5E4A36", linewidth = 0.25) +
theme_paper_void()
The fill aesthetic sets the colour of the paint in both
layers. The alpha aesthetic sets the opacity.
Seeds
The paint uses random noise. If you do not set seed,
each layer picks a seed when you create the layer. The plot then looks
the same each time you draw it, but a new call gives a different
picture. Set seed to get the same picture from the same
code.
Sizes are lengths on the page
ggwatercolor computes the paint when the device draws the plot. The
settings that control size are in millimetres on the page, not in the
units of the data. A wash with width = 4 reaches about 4 mm
from the coast in any map projection and in a panel of any size.
Wash settings
width sets how far the wash reaches.
roughness sets how much the reach changes along the edge.
granulation sets the strength of the pigment grain.
library(patchwork)
maui <- hawaii[hawaii$island %in% c("Maui", "Lanai", "Molokai", "Kahoolawe"), ]
wash_map <- function(title, ...) {
ggplot(maui) +
geom_sf_wash(seed = 1, ...) +
geom_sf(fill = NA, colour = "#5E4A36", linewidth = 0.25) +
labs(title = title) +
theme_paper_void(base_size = 9)
}
wash_map("width = 2", width = 2) +
wash_map("width = 6, roughness = 0", width = 6, roughness = 0) +
wash_map("width = 6, granulation = 1", width = 6, granulation = 1) +
plot_annotation(theme = theme_paper_void())
direction sets the side of the edge that gets the wash.
Use "in" to tint the inside of each shape, in the way that
old political maps tint each border.
ggplot(hawaii) +
geom_sf_wash(aes(fill = island), direction = "in", width = 3, seed = 1) +
geom_sf(fill = NA, colour = "#5E4A36", linewidth = 0.25) +
theme_paper_void() +
theme(legend.position = "none")
Coasts and land borders
A wash layer cannot tell a coast from a land border. The layer knows only the shapes, so it paints along every edge of them. On a map of islands, every edge is a coast and the result is right. On a map of part of a larger land mass, the wash also follows the land borders, where there is no water.
This example shows some counties on the coast of North Carolina.
(We’re not masking all the coastal counties in this example.) In the
first map the wash follows the inland borders of the counties. The
second map gives the other counties to the layer as mask.
The layer paints no wash on the mask, so the wash stays on the
coast.
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
on_coast <- nc$NAME %in%
c(
"Bertie",
"Washington",
"Tyrrell",
"Dare",
"Hyde",
"Carteret",
"Pamlico",
"Currituck",
"Camden",
"Craven",
"Pasquotank",
"Perquimans",
"Chowan",
"Beaufort"
)
coast <- nc[on_coast, ]
inland <- nc[!on_coast, ]
coast_map <- function(title, ...) {
ggplot() +
geom_sf_wash(data = coast, seed = 1, ...) +
geom_sf(data = inland, fill = NA, colour = "grey70", linewidth = 0.2) +
geom_sf(data = coast, fill = NA, colour = "#5E4A36", linewidth = 0.3) +
coord_sf(xlim = c(-78.5, -75.4), ylim = c(34.5, 36.3)) +
labs(title = title) +
theme_paper_void(base_size = 9)
}
wrap_plots(
coast_map("No mask"),
coast_map("mask = inland", mask = inland),
ncol = 1
) +
plot_annotation(theme = theme_paper_void())
The mask is an sf object of polygons. It does not change
the limits of the plot, and the layer does not draw it.
Borders from different sources seldom match exactly. A mask from a
different source than the shapes can leave narrow gaps between them.
Wash shows in the gaps. For this reason the layer grows the mask by
mask_tolerance on every side before it uses the mask. The
default is 0.5 millimetres on the page. Set
mask_tolerance = 0 if the borders match exactly, as they do
in this example.
There is a second way to keep the wash on the coast. The layer treats
shapes that share a fill as one shape. Give the layer all
the land in view. The borders between the pieces of land then get no
wash, and every coast in view gets one. Use this when the map must tint
every coast. Use mask when the map must tint the coast of
some shapes only.
Fill settings
Four settings control the look of a fill.
-
poolingsets how much the opacity changes across the fill. -
rimsets how much extra pigment collects at the edge. -
wobblesets how far the edge of the paint moves from the edge of the shape. -
granulationsets the strength of the pigment grain.
big_island <- hawaii[hawaii$island == "Hawaii", ]
fill_map <- function(title, ...) {
ggplot(big_island) +
geom_sf_watercolor(seed = 1, ...) +
labs(title = title) +
theme_paper_void(base_size = 9)
}
fill_map("Flat", pooling = 0, rim = 0, wobble = 0, granulation = 0) +
fill_map("Default") +
fill_map("Strong", pooling = 0.6, rim = 0.5, wobble = 0.8) +
plot_annotation(theme = theme_paper_void())
By default, geom_sf_watercolor() paints each feature
separately. Use by = "fill" to paint all features of one
colour as a single area. See the README for a comparison.
Relief
geom_hachure() shows the shape of the land with
hachures. Hachures are fine ink lines that run down each slope, as on
engraved maps. The lines are heavier where the ground is steeper. Level
ground stays bare.
The layer needs a raster of elevation in metres, as a
SpatRaster from the terra package.
hawaii_elevation() reads an example raster for the islands
in hawaii.
ggplot(hawaii) +
geom_hachure(hawaii_elevation(), seed = 1929) +
geom_sf_wash(seed = 1929) +
geom_sf(fill = NA, colour = "#7A4A2A", linewidth = 0.25) +
coord_sf(xlim = c(-158.4, -155.9), ylim = c(20.45, 21.8)) +
theme_paper_void()
Set monochrome = TRUE to draw the lines in grey. This
example also gives the wash in grey and sets a grey paper.
ggplot(hawaii) +
geom_hachure(hawaii_elevation(), monochrome = TRUE, seed = 1929) +
geom_sf_wash(monochrome = TRUE, seed = 1929) +
geom_sf(fill = NA, colour = "grey20", linewidth = 0.25) +
coord_sf(xlim = c(-158.4, -155.9), ylim = c(20.45, 21.8)) +
theme_paper_void(paper = "#F5F5F5", ink = "grey20")
Three settings control the look of the lines.
-
separationsets the distance between lines, in millimetres on the page. -
min_slopesets the gentlest slope that gets lines, in degrees. -
linewidthsets the widths of the lightest and heaviest lines.
Cells of the raster with missing values get no lines. Set the sea to
NA to keep the lines on the land. ggwatercolor does not
download elevation data. The elevatr and geodata packages can get
elevation data for most places.
Map scale
min_slope and steep_slope are slopes of the
ground. The layer measures them at the resolution of the page. On a map
of a large area, one millimetre on the page covers many kilometres, and
slopes measured across that distance are gentle. With the default
settings, such a map gets few lines.
exaggeration solves this problem. It multiplies the
heights before the layer measures the slopes, as on a relief model. Give
a number, or use "auto". With "auto", the
layer chooses the number from the land in view, so that the steepest
ground gets the heaviest lines. A map of a large area also needs more
smoothing, about 1 millimetre. This keeps the lines
long.
The alaska dataset holds outlines of Alaska and the
nearby parts of Canada and Russia. alaska_elevation() reads
the elevation of the same land. These maps use the Alaska Albers
projection. The first map uses the default settings and is almost bare.
The second map uses "auto" and more smoothing.
relief_map <- function(title, ...) {
ggplot(alaska) +
geom_hachure(alaska_elevation(), seed = 1929, ...) +
geom_sf(fill = NA, colour = "#7A4A2A", linewidth = 0.25) +
coord_sf(crs = 3338, datum = NA) +
labs(title = title) +
theme_paper_void(base_size = 9)
}
wrap_plots(
relief_map("exaggeration = 1"),
relief_map(
"exaggeration = \"auto\", smoothing = 1",
exaggeration = "auto",
smoothing = 1
),
ncol = 1
) +
plot_annotation(theme = theme_paper_void())
The default is 1, so that a slope in degrees has the same meaning on
every map. With "auto", the value changes with the land in
view.
Masks
geom_hachure() also has a mask argument.
The layer draws no lines on the land under the mask. Use it to show the
relief of some land only. This map shows the relief of Alaska. Canada
and Russia are the mask. The wash uses the same mask, so it follows only
the coast of Alaska.
land <- alaska[alaska$name == "Alaska", ]
neighbours <- alaska[alaska$name != "Alaska", ]
ggplot() +
geom_hachure(
alaska_elevation(),
mask = neighbours,
exaggeration = "auto",
smoothing = 1,
seed = 1929
) +
geom_sf_wash(data = land, mask = neighbours, width = 2.5, seed = 1929) +
geom_sf(data = neighbours, fill = NA, colour = "grey70", linewidth = 0.2) +
geom_sf(data = land, fill = NA, colour = "#7A4A2A", linewidth = 0.25) +
coord_sf(crs = 3338, datum = NA) +
theme_paper_void()
clip is the opposite of mask. The layer
draws lines only on the land inside the clip. Use it when the sea in the
elevation data has heights and not missing values. Many sources give the
sea a height of 0. Smoothing spreads the heights of the coast into such
a sea, and exaggeration makes the result steep. The layer
then draws lines in the water along the coast. In the first map below,
the sea has a height of 0. The second map gives Alaska as the clip.
sea_level <- terra::subst(alaska_elevation(), NA, 0)
coast_map <- function(clip, title) {
ggplot() +
geom_hachure(
sea_level,
mask = neighbours,
clip = clip,
exaggeration = "auto",
smoothing = 1,
seed = 1929
) +
geom_sf(data = land, fill = NA, colour = "#7A4A2A", linewidth = 0.25) +
coord_sf(xlim = c(-149, -139), ylim = c(59, 61.5), datum = NA) +
labs(title = title) +
theme_paper_void()
}
coast_map(NULL, "No clip") / coast_map(land, "clip = land")
Illumination
By default the weight of a line shows only the steepness of the
ground, as if the light came from directly above. Set light
to a compass bearing to light the land from one side. The lines become
thinner on slopes that face the light and heavier on slopes that face
away. Mapmakers usually put the light in the north-west, at a bearing of
315 degrees.
ggplot(hawaii) +
geom_hachure(hawaii_elevation(), light = 315, seed = 1929) +
geom_sf_wash(seed = 1929) +
geom_sf(fill = NA, colour = "#7A4A2A", linewidth = 0.25) +
coord_sf(xlim = c(-158.4, -155.9), ylim = c(20.45, 21.8)) +
theme_paper_void()
The bearing is in degrees clockwise from north. These maps of Maui put the light in the north-west, the north-east, the south-east, and the south-west. The lines are in the same places in each map. Only their weights change.
maui <- hawaii[hawaii$island == "Maui", ]
maui_elevation <- terra::crop(
hawaii_elevation(),
terra::vect(maui),
mask = TRUE
)
lit_map <- function(light, light_strength = 0.6, title = NULL) {
ggplot(maui) +
geom_hachure(
maui_elevation,
light = light,
light_strength = light_strength,
seed = 1929
) +
geom_sf(fill = NA, colour = "#7A4A2A", linewidth = 0.25) +
labs(title = title) +
theme_paper_void(base_size = 9)
}
wrap_plots(
lit_map(315, title = "light = 315"),
lit_map(45, title = "light = 45"),
lit_map(225, title = "light = 225"),
lit_map(135, title = "light = 135"),
ncol = 2
) +
plot_annotation(theme = theme_paper_void())
light_strength sets how much the light changes the
weight of the lines, between 0 and 1. The default is 0.6. A low value
keeps most of the even weight of vertical light. A high value leaves
only faint lines on the lit slopes.
wrap_plots(
lit_map(315, 0.3, title = "light_strength = 0.3"),
lit_map(315, 0.6, title = "light_strength = 0.6"),
lit_map(315, 0.9, title = "light_strength = 0.9"),
ncol = 1
) +
plot_annotation(theme = theme_paper_void())
Illumination works with monochrome = TRUE in the same
way.
ggplot(hawaii) +
geom_hachure(
hawaii_elevation(),
light = 315,
monochrome = TRUE,
seed = 1929
) +
geom_sf_wash(monochrome = TRUE, seed = 1929) +
geom_sf(fill = NA, colour = "grey20", linewidth = 0.25) +
coord_sf(xlim = c(-158.4, -155.9), ylim = c(20.45, 21.8)) +
theme_paper_void(paper = "#F5F5F5", ink = "grey20")
Illumination helps most on a map of a large area, where it separates one mountain range from the next.
ggplot() +
geom_hachure(
alaska_elevation(),
mask = neighbours,
exaggeration = "auto",
smoothing = 1,
light = 315,
seed = 1929
) +
geom_sf_wash(data = land, mask = neighbours, width = 2.5, seed = 1929) +
geom_sf(data = neighbours, fill = NA, colour = "grey70", linewidth = 0.2) +
geom_sf(data = land, fill = NA, colour = "#7A4A2A", linewidth = 0.25) +
coord_sf(crs = 3338, datum = NA) +
theme_paper_void()
Monochrome
Set monochrome = TRUE to paint in greys. Each colour
becomes the grey of the same lightness. geom_sf_wash(),
geom_sf_watercolor(), and geom_hachure()
accept this argument. Give the argument to each layer that you want in
grey.
This example paints only a wash along each coast. The land stays bare paper.
ggplot(hawaii) +
geom_sf_wash(monochrome = TRUE, seed = 1929) +
geom_sf(fill = NA, colour = "grey20", linewidth = 0.25) +
theme_paper_void(paper = "#F5F5F5", ink = "grey20")
monochrome changes only the paint. The built-in papers
have a slight tint, so the example sets a grey paper and a
grey ink.
Paper
theme_paper_void() removes all parts of the plot except
the paper, which is useful for maps. theme_paper() keeps
the axes and a light grid.
The paper argument sets the colour of the paper.
"neutral" is the default. "warm" is more
yellow. You can also give any colour.
The paper is flat by default. Set texture to give the
paper a fine tooth.
ggplot(hawaii) +
geom_sf_watercolor(seed = 1929) +
geom_sf_wash(seed = 1929) +
geom_sf(fill = NA, colour = "#5E4A36", linewidth = 0.25) +
theme_paper_void(paper = "warm", texture = 0.05)
If you combine plots with patchwork, also give the theme to
plot_annotation(theme = ). Otherwise the space between the
plots stays white.
A map has a fixed shape, so the paper does not always fill the image.
ggsave() fills the remaining space with the colour of the
paper. In an R Markdown or Quarto document, set the background colour of
the graphics device to the colour of the paper. For the ragg device, use
the chunk option
dev.args = list(background = "#F4F3ED").
Resolution and speed
Each paint layer is a raster image. The dpi argument
sets the resolution of the image. The default is 300. Use a lower value
to draw faster, or a higher value for print.
For a very large panel, ggwatercolor lowers the resolution and gives
a warning. To change the limit, set the
ggwatercolor.max_cells option.