• Stars
    star
    329
  • Rank 117,022 (Top 3 %)
  • Language
    R
  • License
    Other
  • Created over 3 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ggplot geoms with pattern fills
output
github_document

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 instructions below.

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

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)

plot of chunk unnamed-chunk-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.

Click to show/hide list of supported geoms
ggplot2 ggpattern
geom_area geom_area_pattern
geom_bar geom_bar_pattern
geom_bin2d geom_bin2d_pattern
geom_boxplot geom_boxplot_pattern
geom_col geom_col_pattern
geom_crossbar geom_crossbar_pattern
geom_density geom_density_pattern
geom_histogram geom_histogram_pattern
geom_map geom_map_pattern
geom_polygon geom_polygon_pattern
geom_rect geom_rect_pattern
geom_ribbon geom_ribbon_pattern
geom_sf geom_sf_pattern
geom_tile geom_tile_pattern
geom_violin geom_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
aesthetic description default possible values
pattern Name of the pattern to draw 'stripe' stripe, crosshatch, circle, image, placeholder, magick, gradient, plasma
pattern_type Generic control option NA pattern-dependent
pattern_subtype Generic control option NA pattern-dependent
pattern_alpha Alpha 1 value in range [0, 1] (npc units)
pattern_linetype Stroke linetype 1 linetype
pattern_size Stroke linewidth 1 linewidth
pattern_shape Plotting shape 1 shapes
pattern_colour Stroke colour 'grey20' colour
pattern_fill Fill colour 'grey80' colour
pattern_fill2 Second fill colour '#4169E1' colour
pattern_angle Rotation angle 30 angle in degrees
pattern_density Approx. fraction of area the pattern fills 0.2 value in range [0, 1] (fraction)
pattern_spacing Spacing between repetitions of pattern 0.05 value in range [0, 1] (npc units)
pattern_xoffset Shift pattern along x axis 0 value in range [0, 1] (npc units)
pattern_yoffset Shift pattern along y axis 0 value in range [0, 1] (npc units)
pattern_aspect_ratio Aspect ratio adjustment NA usual range [0.01, 10]
pattern_key_scale_factor Scale factor for pattern in legend 1
pattern_filename Image filename/URL '' Filename/URL
pattern_gravity Image placement 'center' ggpattern::magick_gravity_names
pattern_filter Image scaling filter 'lanczos' ggpattern::magick_filter_names
pattern_scale Scale 1 Multiplier
pattern_orientation Orientation 'vertical' 'vertical', 'horizontal', 'radial'
pattern_phase Phase 0
pattern_frequency Frequency 0.1
pattern_option_1 - 5 Generic options for expansion 0
pattern_grid Grid type 'square' 'square', 'hex', 'hex_circle'
pattern_res Pattern resolution (pixels per inch) NA Resolution
pattern_rot Shape rotation angle (within pattern) 0 angle 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.

ToDo

  • Possibly add geoms from third-party sources e.g.
    • geom_circle() and geom_voronoi() from ggforce

More Repositories

1

dplyr-cli

Manipulate CSV files on the command line using dplyr
R
261
star
2

ggrgl

3d extension to ggplot
R
173
star
3

ggsvg

Use SVG images as ggplot points
R
125
star
4

tickle

Easily create UIs in base R
R
118
star
5

emphatic

ANSI colouring of R output
R
101
star
6

devout

Write R graphics output devices in plain R
C++
97
star
7

anotherworld

AnotherWorld ported to R
R
69
star
8

yyjsonr

Fast JSON package for R
C
68
star
9

ggreverse

Reverse a ggplot object back into code
R
65
star
10

eventloop

Event Loop in R
R
64
star
11

isocubes

R
60
star
12

wordle

Wordle helper for RStats
R
58
star
13

devoutsvg

Bespoke SVG graphics output device with pattern fill support
R
57
star
14

ggthreed

3d geoms and stats for ggplot
R
47
star
15

rllama

Minimal R wrapper for llama.cpp
C
44
star
16

threed

Three-Dimensional Object Transformations
R
42
star
17

gluestick

Simple, single-function string interpolation in Base R
R
42
star
18

minipdf

Minimal pure-R PDF document creator
R
41
star
19

nara

nativeRaster tools for R
C
41
star
20

svgparser

Render SVG images in R. Load SVG data as data.frames
R
40
star
21

carelesswhisper

Automatic speech recognition in R using whisper.cpp
C
35
star
22

geomlime

ggplot geom_lime()
R
35
star
23

xxhashlite

Very fast hash functions using xxHash
C
34
star
24

tr808r

TR-808 Drum Machine for R
R
34
star
25

facetious

Home to some alternate facetting for ggplot2
R
33
star
26

ggblur

Blurry Point Geom for ggplot2
R
33
star
27

fugly

Extract named substrings using named capture groups in regular expressions.
R
31
star
28

minisvg

Create SVG documents with R
R
30
star
29

minicss

Build CSS selectors, styles and stylesheets within R
R
30
star
30

numberwang

numbers to words and vice versa
R
25
star
31

pacman

Pacman-ish game in R
R
24
star
32

memoisetools

a collection of additional caches and helper functions to work alongside the memoise package
R
23
star
33

terse

Terse output for nested lists and data.frames
R
22
star
34

miranda

Fast PRNGs for R
C
20
star
35

ransid

Convert images to ANSI with R
R
20
star
36

minihtml

A package for building HTML documents in R (shiny compatible)
R
19
star
37

lz4lite

Very Fast compression/decompression of in-memory numeric vectors with LZ4
C
19
star
38

chipmunkcore

R wrapper around the Chipmunk2d physics simulation library
C
19
star
39

simplercpp

A demo R package incorporating C code with Rcpp
C++
19
star
40

zstdlite

Fast, configurable in-memory compression of R objects with zstd
C
19
star
41

wingspan

Data about the birds and bonus cards in the boardgame "Wingspan"
R
18
star
42

hershey

Hershey vector font data for RStats
R
18
star
43

foist

Fast Output of Images
C++
17
star
44

simplecall

A demo R package incorporating C code which is called with .Call()
C
17
star
45

displease

Non-linear numeric interpolation
R
16
star
46

CP1919

Cambridge Pulsar at 19 hours and 19 minutes right ascension
R
16
star
47

purler

Fast run-length encoding with NA support and results as a data.frame
C
16
star
48

RStudioConf-2022

16
star
49

gridfont

A version of the 'gridfont' vector font in an R friendly format
R
15
star
50

ggqr

ggplot2 geom for QR codes
R
15
star
51

nonogram

Nonogram solver in rstats
R
15
star
52

rd2list

Convert Rd documentation to a structured, human-readable list
R
14
star
53

flagon

Flags of the World
R
14
star
54

optout

Optimized Graphics Output
R
14
star
55

miniverse

A constellation of packages for document creation in R
R
14
star
56

devoutaudio

An R graphics device which renders to audio
R
13
star
57

strictlyr

Stricter subset of dplyr
R
13
star
58

chipmunkbasic

Higher-level R6 wrapper around Chipmunk2d rigid body physics simulation library
R
13
star
59

phon

Tools and Data for the CMU Pronouncing Dictionary
R
13
star
60

svgpatternusgs

SVG patterns from the U.S. Geological Survey for use within R
R
13
star
61

anon

Anonymous function creation in R and purrr
R
13
star
62

cstructr

Exposing C structrs to R
C
13
star
63

ggecho

ggplot2 stat for echoing data
R
13
star
64

arcadefont

Oldschool arcade vector font
R
13
star
65

cgrep

Highlighted grep of R objects
R
12
star
66

insitu

In-place modification of vectors
C
12
star
67

minidrawio

Create simple draw.io documents from R
R
12
star
68

ingrid

Tools for interactive grid creation and manipulation in the console
R
12
star
69

devoutrgl

R graphics device to render to {rgl}
R
12
star
70

visvalingam

R package for Visvalingam Line Simplification
C
11
star
71

smallfactor

An R factor backed by a raw vector rather than an integer vector.
R
11
star
72

devoutdrawio

R graphics device to output to draw.io XML vector format
R
11
star
73

cairocore

Low-level bindings to the CairoGraphics library for fast 2d drawing operations
R
11
star
74

bdftools

Bitmap font tools for R
R
11
star
75

image2xlsx

Convert an image to an excel spreadsheet
R
10
star
76

analemmatic

Create analemmatic sundials with R
R
10
star
77

callme

Compile and load C code for use with .Call()
R
9
star
78

frak

Fractal Generator
C
9
star
79

cryogenic

Freezing calls, modifying arguments and evaluating later
R
9
star
80

simplefortran

Demo of how Fortran code could be included in an R package
C
9
star
81

snowcrash

Encode arbitrary objects as PNGs, rasters and rasterGrobs
R
9
star
82

triangular

Decompose complex polygons into sets of triangles
R
9
star
83

ggdebug

Package to help debug and inspect ggplot stats
R
9
star
84

svgpatternsimple

Create some simple repeating SVG patterns in R
R
9
star
85

grrr

Modify function default arguments
R
8
star
86

devoutpdf

A hand-crafted PDF graphics output device written in plain R
R
8
star
87

btnvips

libvips cli wrapper for #RStats (btn = better than nothing)
R
8
star
88

simplec

Demo R package with C code which is called using ".C()'
C
8
star
89

poissoned

Poisson Disc Sampling in R
R
8
star
90

pixelweaver

Convert arrays/matrices to packed ARGB32 colour format
C
7
star
91

svgfilter

Create example SVG filters in R
R
7
star
92

cairobasic

A basic R6 wrapper around cairocore
R
7
star
93

lofi

Low-fidelity, low-bit representations of integers, doubles and colours.
R
7
star
94

serializer

Example showing how to access R's serialization functions from C
C
7
star
95

devoutansi

RStats ANSI graphics device
R
7
star
96

flexo

Simple lex/parse tools in R
R
6
star
97

cssparser

Parse CSS into R and apply to HTML
R
6
star
98

deftly

Demonstration of using the C data.frame functions in the {deft} package.
C
6
star
99

asciicast

Some tools for the asciicast ascii animation format
CSS
6
star
100

sars

Statistical Analysis in R Suite
R
6
star