The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Patterns ‘stripe’, ‘wave’, ‘crosshatch’, ‘weave’ - Parameters and Examples

suppressPackageStartupMessages({
  library(ggplot2)
  library(ggpattern)
})

Introduction to the ‘stripe’, ‘wave’, ‘crosshatch’, and ‘weave’ patterns

Pattern Parameters

aesthetic description default possible values
pattern_colour Stroke colour ‘grey20’ colour
pattern_fill Fill colour ‘grey80’ 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 pattern_units grid units
pattern_xoffset Shift pattern along x axis 0 value in pattern_units grid units
pattern_yoffset Shift pattern along y axis 0 value in pattern_units grid units
pattern_units Pattern grid unit ‘snpc’ grid::unit() unit i.e. ‘snpc’, ‘cm’, and ‘inches’
pattern_alpha Alpha NA value in range [0, 1] or NA
pattern_linetype Stroke linetype 1 linetype
pattern_size Stroke linewidth 1 linewidth
pattern_type Pattern type NA See wave and weave pattern documentation
pattern_subtype Pattern subtype NA See weave pattern documentation
pattern_frequency Frequency 0.1 Frequency of waves in ‘wave’ pattern

There are also a number of parameters for extra control of legend sizing and aspect ratio adjustments. See the ‘Pattern Parameters - Common’ for more information.

Data

Standard data for all the example plots

df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
df
#>   trt outcome
#> 1   a     2.3
#> 2   b     1.9
#> 3   c     3.2

Vanilla ggplot2

ggplot(df, aes(trt, outcome)) +
  geom_col(aes(fill=trt),colour='black') +
  theme_bw() +
  labs(title = "Plain ggplot2")

A plot using vanilla ggplot2

Use the {ggpattern} geom

ggplot(df, aes(trt, outcome)) +
  geom_col_pattern(aes(fill=trt),colour='black',
                   pattern='stripe') +
  theme_bw() +
  labs(title = "ggpattern") +
  theme(legend.key.size = unit(1.5, 'cm'))

A plot using ggpattern with a patterned geom

Mixing patterns

ggplot(df, aes(trt, outcome)) +
  geom_col_pattern(aes(fill=trt, pattern=trt, pattern_type=trt),colour='black') + 
  theme_bw() +
  labs(title = "Use 'stripe' and 'wave' patterns") + 
  theme(legend.key.size = unit(1.5, 'cm')) +
  scale_pattern_manual(values=c('stripe', 'wave', 'wave')) +
  scale_pattern_type_manual(values=c(NA, 'triangle', 'sine'))

A plot with both a 'stripe' pattern and a 'wave' pattern.

The Density Aesthetic

The aesthetic pattern_density roughly corresponds to the fraction of the filled area which should be covered by the pattern.

In the following plot the density of striping is increased to 50% of the fill area.

ggplot(df, aes(trt, outcome)) +
  geom_col_pattern(
    aes(fill=trt, pattern=trt),
    colour          = 'black', 
    pattern_density = 0.5
  ) +
  theme_bw() +
  labs(title = "Fixed density of 0.5 (50% of the fill area)") + 
  scale_pattern_manual(values=c('stripe', 'crosshatch', 'weave')) +
  theme(legend.key.size = unit(1.5, 'cm'))

A plot with the density of striping increased to 50% of fill area

Weave types

The ‘weave’ pattern supports a rich set of weave types and subtypes including irregular ‘matt’, ‘twill’ (including ‘herringbone’ and ‘zigzag’ variations), and ‘satin’ weaves . See the weave pattern documentation for more information.

ggplot(df, aes(trt, outcome)) +
  geom_col_pattern(
    aes(pattern_fill2=trt, pattern_type=trt),
    pattern = 'weave',
    colour          = 'black', 
    pattern_density = 1.0,
    pattern_fill = 'grey',
    pattern_key_scale_factor = 0.5,
  ) +
  theme_bw() +
  labs(title = "Some 'weave' types") + 
  scale_pattern_type_manual(values=c('plain', 'twill', 'satin')) +
  theme(legend.key.size = unit(1.5, 'cm'))

A plot with 'plain', 'twill', 'satin' weave patterns

The Density Aesthetic as a Mapped Aesthetic

ggplot(df, aes(trt, outcome)) +
  geom_col_pattern(
    aes(fill = trt, pattern_density = trt),
    colour          = 'black', 
    pattern         = 'stripe'
  ) +
  theme_bw() +
  labs(title = "Aesthetic Mapping of 'trt' to Density") + 
  theme(legend.key.size = unit(1.5, 'cm'))

Example plot from using the density aesthetic as a mapped aesthetic.

The Density Aesthetic as a Mapped Aesthetic with Manual Scale

scale_pattern_density_manual() can be used to manually control how the variable is mapped to the density.

ggplot(df, aes(trt, outcome)) +
  geom_col_pattern(
    aes(fill = trt, pattern_density = trt),
    colour          = 'black', 
    pattern         = 'stripe'
  ) +
  theme_bw() +
  labs(title = "Aesthetic Mapping of 'trt' to Density") + 
  theme(legend.key.size = unit(1.5, 'cm')) + 
  scale_pattern_density_manual(values = c(a = 0.1, b=0.3, c=0.5))

Example plot from using the density aesthetic as a mapped aesthetic with manual scale.

The Spacing Aesthetic as a Mapped Aesthetic

ggplot(df, aes(trt, outcome)) +
  geom_col_pattern(
    aes(fill = trt, pattern_spacing = trt),
    colour          = 'black', 
    pattern         = 'stripe'
  ) +
  theme_bw() +
  labs(title = "Aesthetic Mapping of 'trt' to Spacing") + 
  theme(legend.key.size = unit(1.5, 'cm'))

Example plot from using the spacing aesthetic as a mapped aesthetic.

The Fill Aesthetic as a Mapped Aesthetic

ggplot(df, aes(trt, outcome)) +
  geom_col_pattern(
    aes(fill = trt, pattern_fill = trt),
    colour          = 'black', 
    pattern         = 'stripe'
  ) +
  theme_bw() +
  labs(title = "Aesthetic Mapping of 'trt' to Pattern Fill") + 
  scale_pattern_fill_viridis_d() + 
  theme(legend.key.size = unit(1.5, 'cm'))

Example plot from using the fill aesthetic as a mapped aesthetic.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.