Introduction to the ‘magick’ and ‘polygon_tiling’ patterns

pattern = 'magick' - filling a geom with pattern from imagemagick

imagemagick has the ability to create pixel-based patterns. This pattern uses those pixel patterns to fill the geom.

pattern = 'magick' options

Aesthetic Description Default
pattern_type Image scaling type ‘fit’
pattern_fill Colour ‘grey20’
pattern_scale Extra scaling 1
pattern_filter Filter to use when scaling ‘box’
pattern_alpha Alpha NA
pattern_aspect_ratio Override aspect ratio NA
pattern_key_scale_factor Additional scale factor for legend 1

pattern = 'magick' - pattern_type options

Patterns have been scaled (using pattern_scale) to make them more visible for this vignette.

Note that these are all pixel patterns and will always look blocky.

if (require("magick")) {

df1 <- data.frame(
  x    = rep(1:6, 9),
  y    = rep(1:9, each=6),
  name = gridpattern::names_magick,
  stringsAsFactors = FALSE
)

ggplot(df1) + 
  geom_tile_pattern(
    aes(x, y, pattern_type = I(name)),
    pattern       = 'magick',
    pattern_scale = 1.5,
    pattern_fill  = 'white', 
    width         = 0.9, 
    height        = 0.9
  ) + 
  geom_label(aes(x+0.4, y+0.4, label = name), hjust = 1, vjust = 1) + 
  theme_void() + 
  labs(
    title = "All the possible magick pattern names"
  ) +
  coord_fixed(1)

}
#> Loading required package: magick
#> Linking to ImageMagick 6.9.12.3
#> Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
#> Disabled features: fftw, ghostscript, x11

Example: pattern = 'magick' - Transparent patterns

if (require("magick")) {

df1 <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))

ggplot(df1, aes(trt, outcome)) +
  geom_col_pattern(
    aes(
      pattern_type = trt, 
      pattern_fill = trt
    ),
    pattern       = 'magick',
    pattern_key_scale_factor = 0.7,
    fill          = 'white',
    colour        = 'black'
  ) +
  theme_bw(15) +
  labs(
    title    = "ggpattern::geom_col_pattern()",
    subtitle = "pattern='magick'"
  ) +
  theme(legend.key.size = unit(2, 'cm')) +
  scale_pattern_type_discrete(choices = c('bricks', 'fishscales', 'right45')) +
  coord_fixed(ratio = 1/2)

}

Example: pattern = 'magick' - Transparent patterns (2)

if (require("magick")) {

ggplot(mtcars) +
  geom_density_pattern(
    aes(
      x             = mpg,
      pattern_fill  = as.factor(cyl),
      pattern_type  = as.factor(cyl)
    ),
    pattern      = 'magick',
    pattern_scale = 2
  ) +
  theme_bw(15) +
  theme(legend.key.size = unit(2, 'cm')) +
  labs(
    title    = "ggpattern::geom_density_pattern()",
    subtitle = "pattern='magick'"
  ) +
  scale_pattern_type_discrete(choices = gridpattern::names_magick) +
  coord_fixed(ratio = 80)

}

Example: pattern = 'magick' - Pie chart

if (require("magick")) {

df2 <- data.frame(
  group = factor(c("Cool", "But", "Use", "Less"), levels = c("Cool", "But", "Use", "Less")),
  value = c(10, 20, 30, 40)
)

ggplot(df2, aes(x="", y = value, pattern_angle = group))+
  geom_bar_pattern(
    aes(pattern_type = group, pattern_fill = group),
    pattern                  = 'magick',
    pattern_scale            = 2,
    width                    = 1,
    stat                     = "identity",
    fill                     = 'white',
    colour                   = 'black',
    pattern_aspect_ratio     = 1,
    pattern_density          = 0.3
  ) +
  coord_polar("y", start=0) +
  theme_void(20) +
  theme(legend.key.size = unit(2, 'cm')) +
  scale_pattern_type_discrete(choices = gridpattern::names_magick_stripe) +
  labs(
    title    = "ggpattern::geom_bar_pattern()",
    subtitle = "pattern='magick'"
  ) 

}

pattern = 'polygon_tiling' - filling a geom with a polygon tiling

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_spacing Spacing between repetitions of pattern 0.05 value in range [0, 1] (snpc units)
pattern_xoffset Shift pattern along x axis 0 value in range [0, 1] (snpc units)
pattern_yoffset Shift pattern along y axis 0 value in range [0, 1] (snpc units)
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 gridpattern::names_polygon_tiling. See polygon_tiling pattern documentation for more info.

pattern = 'polygon_tiling' - pattern_type options


df1 <- data.frame(
  x    = rep(1:4, 9),
  y    = rep(1:9, each=4),
  name = gridpattern::names_polygon_tiling[1:36],
  stringsAsFactors = FALSE
)

ggplot(df1) + 
  geom_tile_pattern(
    aes(x, y, pattern_type = I(name)),
    pattern       = 'polygon_tiling',
    pattern_scale = 1.5,
    pattern_fill  = 'white', 
    width         = 0.9, 
    height        = 0.9
  ) + 
  geom_label(aes(x+0.4, y+0.4, label = name), hjust = 1, vjust = 1) + 
  theme_void() + 
  labs(
    title = "All the possible 'polygon_tiling' pattern names"
  ) 

pattern = 'polygon_tiling' - Bar chart

gg <- ggplot(mtcars) +
   geom_density_pattern(
     aes(
       x            = mpg,
       pattern_fill = as.factor(cyl),
       pattern_type = as.factor(cyl)
     ),
     pattern = 'polygon_tiling',
     pattern_key_scale_factor = 1.2
   ) +
   scale_pattern_type_manual(values = c("hexagonal", "rhombille",
                              "pythagorean")) +
   theme_bw(18) +
   theme(legend.key.size = unit(2, 'cm')) +
   labs(
     title    = "ggpattern::geom_density_pattern()",
     subtitle = "pattern = 'polygon_tiling'"
   )
plot(gg)