Learn R Programming

⚠️There's a newer version (1.1.4) of this package.Take me there.

ggpattern

ggpattern provides custom ggplot2 geoms which support filled areas with geometric and image-based patterns.

Reading the articles/vignettes on the package website is probably the best way to get started.

Feature Summary

  • Custom versions of (almost) all the geoms from ggplot2 which have a region which can be filled.
  • A suite of aesthetics for controlling the pattern appearance (e.g. pattern_alpha)
  • The ability to include user-defined patterns

Installation

You can install the development version from GitHub with the instructions below.

Note: there are a lot of dependencies. Please ensure these are installed first before attempting to install ggpattern. By doing so, this will make any issues during installation clearer, leading to more meaningful bug reporting.

install.packages('ggplot2')
install.packages('scales')
install.packages('grid')
install.packages('glue')
install.packages('rlang')
install.packages('tibble')
install.packages('gridGeometry')
install.packages('sf')
install.packages('png')
install.packages('magick')
install.packages('memoise')

# install.packages("remotes")
remotes::install_github("coolbutuseless/ggpattern")

Quickstart

  1. Take an existing plot which contains a geom with a fillable area e.g geom_col().
  2. Use the {ggpattern} version of the geom e.g. ggpattern::geom_col_pattern() instead of ggplot2::geom_col()
  3. Set the aesthetic pattern to your choice of pattern e.g pattern = 'stripe', and set other options using pattern_* aesthetics
df <- data.frame(level = c("a", "b", "c", 'd'), outcome = c(2.3, 1.9, 3.2, 1))

ggplot(df) +
  geom_col_pattern(
    aes(level, outcome, pattern_fill = level), 
    pattern = 'stripe',
    fill    = 'white',
    colour  = 'black'
  ) +
  theme_bw(18) +
  theme(legend.position = 'none') + 
  labs(
    title    = "ggpattern::geom_pattern_col()",
    subtitle = "pattern = 'stripe'"
  ) +
  coord_fixed(ratio = 1/2)

Gallery

Feature Details

Available Geoms

ggpattern includes versions of (nearly) all geoms from ggplot2 which could plausiblly support being filled with a pattern.

See the vignette galleries for examples of all the available geoms filled with geometry-based patterns and image-based/array-based patterns.

ggplot2ggpattern
geom_areageom_area_pattern
geom_bargeom_bar_pattern
geom_bin2dgeom_bin2d_pattern
geom_boxplotgeom_boxplot_pattern
geom_colgeom_col_pattern
geom_crossbargeom_crossbar_pattern
geom_densitygeom_density_pattern
geom_mapgeom_map_pattern
geom_polygongeom_polygon_pattern
geom_rectgeom_rect_pattern
geom_ribbongeom_ribbon_pattern
geom_sfgeom_sf_pattern
geom_tilegeom_tile_pattern
geom_violingeom_violin_pattern

New aesthetics

To control pattern appearance, a raft of new aesthetics have been added. e.g.  pattern_alpha, pattern_filename, pattern_density.

There are also scale functions to control each of these new aesthetics e.g.  scale_pattern_alpha_discrete.

Not all aesthetics apply to all patterns. See the individual pattern vignettes for which aesthetics it uses, or see the first vignette on developing user-defined patterns for a table of aesthetic use by pattern, or see the individual vignettes for each pattern.

aestheticdescriptiondefaultpossible values
patternName of the pattern to draw‘stripe’stripe, crosshatch, circle, image, placeholder, magick, gradient, plasma
pattern_typeGeneric control optionNApattern-dependent
pattern_subtypeGeneric control optionNApattern-dependent
pattern_alphaAlpha1value in range [0, 1] (npc units)
pattern_linetypeStroke linetype1linetype
pattern_sizeStroke linewidth1linewidth
pattern_shapePlotting shape1shapes
pattern_colourStroke colour‘grey20’colour
pattern_fillFill colour‘grey80’colour
pattern_fill2Second fill colour‘#4169E1’colour
pattern_angleRotation angle30angle in degrees
pattern_densityApprox. fraction of area the pattern fills0.2value in range [0, 1] (fraction)
pattern_spacingSpacing between repetitions of pattern0.05value in range [0, 1] (npc units)
pattern_xoffsetShift pattern along x axis0value in range [0, 1] (npc units)
pattern_yoffsetShift pattern along y axis0value in range [0, 1] (npc units)
pattern_aspect_ratioAspect ratio adjustmentNAusual range [0.01, 10]
pattern_key_scale_factorScale factor for pattern in legend1
pattern_filenameImage filename/URL’’Filename/URL
pattern_gravityImage placement‘center’ggpattern::magick_gravity_names
pattern_filterImage scaling filter‘lanczos’ggpattern::magick_filter_names
pattern_scaleScale1Multiplier
pattern_orientationOrientation‘vertical’‘vertical’, ‘horizontal’, ‘radial’
pattern_phasePhase0
pattern_frequencyFrequency0.1
pattern_option_1 - 5Generic options for expansion0

User-Defined Patterns

Users can write their own pattern functions and ask ggpattern to use them, without having to include the pattern in the package.

See the vignettes on developing patterns ( 1 2, 3 ) for how to do this, and see the vignettes on experimental patterns to see this in action ( Point filling, Hex pattern, Ambient Noise ).

Vignettes

General examples

Exploration of pattern parameters and appearance

Developing your own pattern

Experimental patterns

These are patterns that aren’t quite ready for prime-time. Feel free to steal the code and extend to suit your needs.

Other examples

Limitations

  • Nearly always need to use coord_fixed() to ensure the aspect ratio is calculated correctly. Use pattern_aspect_ratio to override the internal calculation, of for occasions where you can’t use coord_fixed() because a different coord_*() is used.
  • Legend rendering for patterns is still not great.
    • Use pattern_key_scale_factor to adjust legend appearance.
  • The Rstudio output device can be quite slow for plots with lots of patterns. It is often faster to save directly to PNG or PDF and view that.
  • Self intersecting geometry can be an issue.
  • Non-linear coordinate systems have not been tested.
  • Polygons with holes are not supported

ToDo

  • Possibly add geoms from third-party sources e.g.
    • geom_circle() and geom_voronoi() from ggforce
  • A vignette on how array-based patterns are implemented.
  • A vignette on why aspect ratio is hard to get right.

Copy Link

Version

Install

install.packages('ggpattern')

Monthly Downloads

12,575

Version

0.1.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Mike Cheng

Last Published

August 27th, 2025

Functions in ggpattern (0.1.3)

convert_polygon_df_to_polygon_sf

Convert a polygon_df to an sf POLYGON/MULTIPOLYGON
convert_r_colour_to_magick_colour

Convert an R colour to a magick colour
check_default

Sanity check an argument value
convert_img_to_array

Convert a magick image to an RGBA array.
convert_polygon_df_to_polygon_grob

Convert a polygon_df to grid::polygonGrob object
calculate_bbox_polygon_df

Calculate the bounding box of a polygon_df object
convert_polygon_df_to_alpha_channel

Convert a polygon to an alpha mask
convert_polygon_sf_to_polygon_df

Convert a sf POLYGON/MULTIPOLYGON into a polygon_df
augment_aes

Combine two aes() mappings
bidirection

Utilities for working with bidirecitonal layers
create_magick_pattern_as_array

Read a user specified filename as an image
create_circles_grob

Create a cicleGrob object for a set of points
create_gradient_img

create a gradient image as an array
create_magick_plasma_img

Create plasma using imagemagick
create_magick_pattern_img_scaled

Create a scaled version one of imagemagick's internal patterns
fetch_placeholder_img

Fetch a placeholder image of the correct dimensions
get_aspect_ratio_from_current_viewport

Get the aspec ratio of the current viewport
create_magick_pattern_img

Create one of imagemagick's internal patterns
fill_area_with_img

Fill an area with a magick image
create_magick_plasma_as_array

Read a user specified filename as an image
create_polygon_df

Create a polygon_df object from the given coordinates
create_stripes_sf

Create a sf MULTIPOLYGON object where each polygon is an individual stripe.
ggpattern

ggpattern: A package of geoms with parameterised pattern fills
gg_dep

Give a deprecation error, warning, or message, depending on version number.
create_key_pattern_grob

Create the patterned area to be used in the legend key
GeomRectPattern

GeomRectPattern
create_pattern_array

Generate an RGBA array and create a raster grob to fill the region
create_pattern_grobs

Generate all the pattern grobs for this geom
create_pattern_circles

Create grob objects for the pattern elements within a boundary
img_read_as_array_wrapper

Read a user specified filename/URL as an image
get_aspect_ratio_from_coord

Get the aspect ratio from the coord
get_aspect_ratio_from_context

Try and get an aspect ratio from the context of the plot
draw_key_polygon_pattern

Key glyphs for legends
fetch_placeholder_array

Fetch a placeholder image of the correct size and return as an array
fill_area_with_img_none

Resize image canvas to the target width/height, and use gravity to place the undistorted image
fill_area_with_img_squish

Resize image, ignoring aspect, such that both the target width and height is achieved.
magick_filter_names

Gravity types for 'image' positioning
is_polygon_df

Test if object is polygon_df or NULL
scale_pattern_aspect_ratio_continuous

Scales for pattern_aspect_ratio
magick_gravity_names

Gravity types for 'image' positioning
scale_pattern_colour_brewer

Sequential, diverging and qualitative colour scales from colorbrewer.org
rotate_polygon_df

Simple 2D rotation of a polygon about the origin
scale_pattern_filter_continuous

Scales for pattern_filter
scale_pattern_frequency_continuous

Scales for pattern_frequency
placeholder_names

All placeholder names
reexports

Objects exported from other packages
scale_pattern_shape

Scales for shapes, aka glyphs
remove_missing

Convenience function to remove missing values from a data.frame
create_gradient_as_array

A shim to go between the main pattern function for an image, and the specific pattern functon for an image.
img_read

Fetch a given path or URL as a magick image
fill_area_with_img_expand

Resize image, preserving aspect, such that when cropped, the target width and height is achieved.
scale_pattern_size_continuous

Scales for area or radius
fill_area_with_img_fit

Resize image, preserving aspect, such that one of target width or height is achieved without distoring or cropping.
fill_area_with_img_tile

Tile image to fill the specified area
magick_pattern_names

Names of patterns available in image magick, plus subsets for shaded intensity and stripes
my_abind

abind clone for adding a matrix to an array
scale_pattern_angle_continuous

Scales for pattern_angle
geom_rect_pattern

ggplot2 geoms with support for pattern fills
scale_pattern_alpha_continuous

Alpha transparency scales
scale_pattern_colour_viridis_d

Viridis colour scales from viridisLite
scale_pattern_gravity_continuous

Scales for pattern_gravity
scale_pattern_density_continuous

Scales for pattern_density
scale_pattern_colour_grey

Sequential grey colour scales
scale_pattern_key_scale_factor_continuous

Scales for pattern_key_scale_factor
scale_pattern_phase_continuous

Scales for pattern_phase
scale_pattern_yoffset_continuous

Scales for pattern_yoffset
scale_pattern_linetype

Scale for line patterns
scale_pattern_scale_continuous

Scales for pattern_scale
scale_pattern_colour_hue

Evenly spaced colours for discrete data
should_stop

Used in examples to illustrate when errors should occur.
img_read_as_array

Fetch a given path or URL as a 3D RGB array of values
scale_pattern_colour_continuous

Continuous and binned colour scales
scale_pattern_identity

Use values without scaling
scale_pattern_spacing_continuous

Scales for pattern_spacing
scale_pattern_continuous

Scales for pattern
scale_pattern_manual

Create your own discrete scale
scale_pattern_filename_continuous

Scales for pattern_filename
scale_pattern_orientation_continuous

Scales for pattern_orientation
scale_pattern_type_continuous

Scales for pattern_type
scale_pattern_colour_gradient

Gradient colour scales
waiver

A waiver object.
scale_pattern_xoffset_continuous

Scales for pattern_xoffset
scale_pattern_subtype_continuous

Scales for pattern_subtype