Black and white barchart with textures, using ggpattern and ggplot2



This post explains how to create a black and white barchart with different background elements and textures using the ggpattern package.
We’ll go through several examples with reproducible code.

Barplot Barplot

Packages


For this post, we need to load ggpattern, ggplot2 and hrbrthemes (for better looking chart).

library(ggpattern)
library(ggplot2)
library(hrbrthemes)


Dataset


In this post, we’ll use a simple homemade dataset:

df = data.frame(
  name=c("north","south","south-east","north-west","south-west"),
  val=sample(seq(7,15), 5)
)

Barchart with ggplot2


With ggplot2 and the geom_bar() function, we can easily create simple barchart:

ggplot(df, aes(x=name, y=val, fill=name)) +
  geom_bar(stat="identity", alpha=.6, width=.4) +
  scale_fill_grey(start=0, end=0.8) +  # start and end define the range of grays
  theme_bw()

Black and white barchart.


In various cases, you can be obliged to have black and white charts. However, this does not imply that only can use uniform colors!

Actually, you can add textures to your chart elements and make them more visually apealling thanks to the ggpattern library.

This package has a geom_col_pattern() function for this specific use case!

The following arguments customize the appearance of the patterns:

  • fill = 'white': The base fill color of the bars (behind the pattern) is white.

  • colour = 'black': The border color of the bars is black.

  • pattern_density = 0.5: The density of the pattern inside the bars.

  • pattern_fill = 'black': The color of the pattern itself.

  • pattern_colour = 'darkgrey': The secondary color used in the pattern.

ggplot(df, aes(x=name, y=val)) +
 geom_col_pattern(
    aes(pattern=name,
        pattern_angle=name,
        pattern_spacing=name
        ), 
    fill            = 'white',
    colour          = 'black', 
    pattern_density = 0.5, 
    pattern_fill    = 'black',
    pattern_colour  = 'darkgrey'
  ) +
  theme_bw()

Conclusion

This post explained how to create a textured black and white barchart using the ggpattern library. For more of this package, see the dedicated section or the barplot section.

Related chart types


Line plot
Area
Stacked area
Streamchart
Time Series



Contact

This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message on Twitter, or send an email pasting yan.holtz.data with gmail.com.