fill_hatch() builds a grid::pattern() fill value that renders a
repeating diagonal hatch line. It's meant to be used as the fill
argument to grid::gpar() (and eventually style()'s fill property),
in place of a plain colour.
Usage
fill_hatch(
color = "black",
angle = 45,
spacing = 0.1,
aspect = NULL,
linewidth = 1,
extend = "repeat"
)Arguments
- color
Line colour. Default
"black".- angle
Hatch angle in degrees, measured counterclockwise from the positive x-axis. Default
45.- spacing
Baseline tile size, as a fraction of the target's bounding box. Must be a positive number. Default
0.1.- aspect
Width-to-height ratio of the target polygon's bounding box. Must be a positive number, or
NULL(the default) to resolve it automatically from the real target's own bounding-box aspect ratio atdraw()time – see the fill class. Passing a fixed number instead computes the pattern once, immediately, against that value only.- linewidth
Line width. Must be a positive number. Default
1.- extend
Passed to
grid::pattern(). Default"repeat".
Value
A pattern object as returned by grid::pattern(), suitable for
use as the fill argument to grid::gpar().
Details
grid::pattern() tiles are sized as a fraction of the target polygon's
own bounding box, not a fixed physical square, so a tile that looks
square in that relative sense can be a stretched rectangle in absolute
terms whenever the target's bounding box isn't square itself – which
distorts any angle baked directly into the pattern content. This is
corrected automatically: aspect defaults to NULL, resolved against
the real target's own bounding-box aspect ratio (width / height) at
draw() time (see the fill class); pass a fixed number instead to
compute the pattern once, immediately, against that value only.
Internally, the hatch line is always drawn as a plain diagonal from one
tile corner to the opposite corner (or the mirror image, for a
negative-sloped angle) – never at an arbitrary slope baked into the
segment's own coordinates. A corner-to-corner diagonal is the only slope
that tiles seamlessly under grid::pattern()'s extend = "repeat",
which translates tile copies by whole tile-widths/heights only; any other
local slope leaves a visible mismatch ("dashing") at every tile edge. The
desired angle is instead achieved entirely by choosing the tile's
width/height ratio. Exactly horizontal/vertical angles are handled as
a special case, since a straight (non-diagonal) line tiles seamlessly at
any tile aspect ratio.
Examples
draw(shape_circle(fill = fill_hatch(angle = 30, spacing = 0.15)))
# a steeper angle and finer spacing
draw(shape_circle(fill = fill_hatch(angle = 75, spacing = 0.06)))
# exactly horizontal/vertical are handled as a special case (no
# diagonal-tile trick needed)
draw(shape_circle(fill = fill_hatch(angle = 0, spacing = 0.1)))
# aspect corrects the rendered angle for a non-square bounding box --
# without it, a 45 degree hatch looks skewed on a wide rectangle
draw(shape_rectangle(width = 3, height = 1, fill = fill_hatch(angle = 45)))
draw(shape_rectangle(
width = 3,
height = 1,
fill = fill_hatch(angle = 45, aspect = 3)
))