The ggpattern
package in R is
an extension of the ggplot2
package, designed to add pattern aesthetics to your
ggplot2 visualizations.
This post showcases the key
features of ggpattern
and provides a set of
graph examples using the package.
{ggpattern}
The ggpattern
package in R is an extension of the ggplot2
package, designed to add pattern aesthetics to your
ggplot2 visualizations.
It allows you to add patterns like stripes, crosses, custom images and more to your ggplot2 plots, making your visualizations more informative and engaging.
✍️ author → Mike FC
📘 documentation → github
⭐️ more than 500 stars on github
This package can be especially useful if you need to publish some black and white content.
Getting started with ggpattern
is straightforward.
First, ensure you have ggplot2
installed. Then, you can
install ggpattern
directly from CRAN using the
install.packages
function:
The ggpattern
package comes with a variety of
pattern aesthetics that can be easily integrated into
your ggplot2
visualizations.
Let’s take an example with the geom_col_pattern()
aesthetics. It does pretty much the same task as the
geom_col()
aesthetics of ggplot2
that is used
for bar
charts.
But it adds options to use some patterns into rectangles.
The aesthetics comes with a number of options described in the next section. For each, you have to decide:
if you want it to depend on a value of the dataset, put it inside
the aes()
call. Below, the color of the pattern is mapped
to the name
columnn since pattern_fill
is
in the aes()
function.
if you want it to be the same for all shapes, put it outside the
aes()
call. For instance, all bars will be using
crosshatch
below.
It is possible to customize pretty much everything on the pattern you are building.
colour
and fill
control the shape
outline and the shape content colors.
pattern
controls the pattern type. It can be:
‘stripe’ (default), ‘crosshatch’, ‘point’, ‘circle’ or ‘none’
pattern_alpha
controls the pattern
transparency
pattern_angle
controls the angle 🤷♀️
pattern_colour
and pattern_fill
control
the outline and fill colors
Type help(geom_col_pattern)
to get some more details on
all the available options!
You can map the pattern features to the data in use.
For instance, the following example use a very different look for
each group of the dataset. To do so, the arguments described above are
placed inside the aes()
call:
Several other aesthetics are available, allowing to fill any kind of
chart types with pattern. Names are always matching the original
ggplot2
names.
To put it in a nutshell:
geom_boxplot_pattern
can be used for boxplotgeom_density_pattern
for density
chartsgeom_map_pattern
for mapsgeom_violin_pattern
for violin chartsAnd many more are on the list. Here is an overview of other examples
in the gallery using the ggpattern
library:
👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥