MyNixOS website logo
Description

'ggplot2' Pattern Geoms.

Provides 'ggplot2' geoms filled with various patterns. Includes a patterned version of every 'ggplot2' geom that has a region that can be filled with a pattern. Provides a suite of 'ggplot2' aesthetics and scales for controlling pattern appearances. Supports over a dozen builtin patterns (every pattern implemented by 'gridpattern') as well as allowing custom user-defined patterns.

ggpattern

CRAN Status Badge R build status

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 following instructions:

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

You can install the CRAN release version using:

install.packages("ggpattern")

Do note though that due to CRAN size limitations the CRAN version omits several vignettes that are contained in the development version.

An installation error is highly unlikely to be an issue with {ggpattern} itself. Please don't open up a Github Issues unless you are sure it is an issue with {ggpattern} itself. If you have an error installing the package we strongly suggest that you look at all your error messages more closely to figure out which package dependency is failing to install and why. See https://thomasadventure.blog/posts/install-r-packages/ for more info on installing R packages.

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_col_pattern()",
    subtitle = "pattern = 'stripe'"
  ) +
  coord_fixed(ratio = 1/2)

Gallery

Feature Details

Available Geoms

ggpattern includes versions of (nearly) all geoms from ggplot2 which could plausibly 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.

Click to show/hide list of supported geoms
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_histogramgeom_histogram_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.

Click to show/hide list of new aesthetics
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
pattern_gridGrid type'square''square', 'hex', 'hex_circle'
pattern_resPattern resolution (pixels per inch)NAResolution
pattern_rotShape rotation angle (within pattern)0angle in degrees

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 vignette on developing patterns: vignette("developing-patterns", package = "ggpattern").

Vignettes

Note: due to CRAN package size limits many of these vignettes are only available in the Github version of {ggpattern} and the package website.

General examples

Exploration of pattern parameters and appearance

Developing your own pattern

Other examples

Limitations

  • Use pattern_aspect_ratio to override the internal aspect ratio calculation.
  • 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.
Metadata

Version

1.1.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows