Introduction to ggpattern Package in R (6 Examples) | ggplot2 Plots with Textures


This tutorial explains how to draw ggplot2 plots with textures and patterns using the ggpattern package in R programming.

Table of contents:

Let’s dig in…


Some Details About the ggpattern Package

The ggpattern package is used to add geometric and image-based patterns to ggplot2 plots in R.

If you have never heard about the ggpattern package, you may ask: Why do I even need this package?

The reason: This package uplifts data visualizations with the ggplot2 package to the next level!

Especially in case you want to draw different data groups in the same graphic, it might be helpful to have another way (in addition to colors) to differentiate between the different groups in your data.

Sounds good? So how can we install the ggpattern package?

Unfortunately, the ggpattern package is not on CRAN yet (at least the point when I was writing this tutorial). For that reason we have to install the ggpattern package via GitHub:

install.packages("remotes")                    # Install remotes package
library("ggpattern")                           # Load ggpattern package

If we want to plot our data with the ggplot2 package, we also have to install and load ggplot2:

install.packages("ggplot2")                    # Install ggplot2 package
library("ggplot2")                             # Load ggplot2 package

Before we jump into the examples of this tutorial, I also want to give credit to Mike FC, the developer of this package. On this homepage, you can find the development process and many additional tutorials on the ggpattern package.


Example Data

The following data will be used as basement for this R tutorial:

set.seed(92634)                                # Create example data
data <- data.frame(x = rnorm(30, 10),
                   group = letters[1:3])
head(data)                                     # Head of example data
#          x group
# 1 9.795025     a
# 2 9.442812     b
# 3 8.954915     c
# 4 9.456803     a
# 5 7.709386     b
# 6 9.589968     c

Have a look at the previous output of the RStudio console. It reveals that our example data has two columns.

The variable x contains randomly distributed numeric values and the variable group defines three different groups in our data.

Let’s draw these data!


Example 1: Drawing Barplot with Pattern Using geom_bar_pattern Function

In Example 1, I’ll show how to create a ggplot2 barchart with the default pattern used by the ggpattern package.

For this, we simply have to replace the geom_bar function of the ggplot2 package by the geom_bar_pattern function of the ggpattern package:

ggplot(data, aes(group, x, fill = group)) +    # Create barplot with pattern
  geom_bar_pattern(stat = "identity")


r graph figure 1 ggpattern r package programming language


Figure 1 illustrates the output of the previous R programming syntax: A ggplot2 barplot with the same texture in each bar.


Example 2: Manually Specify Colors of Pattern Using pattern_color & pattern_fill Arguments

In Example 2, I’ll illustrate how to change the colors of our pattern.

We can use the pattern_color argument to specify the borders and the pattern_fill argument to edit the filling color of our pattern.

ggplot(data, aes(group, x, fill = group)) +    # Modify colors of pattern
  geom_bar_pattern(stat = "identity",
                   pattern_color = "white",
                   pattern_fill = "black")


r graph figure 2 ggpattern r package programming language


As shown in Figure 2, the previous syntax created another barchart with patterns, but this time the colors of the patterns were set manually.


Example 3: Drawing Barplot with Pattern for Each Group Using aes Function & pattern Argument

In my opinion, this example shows the most important point of the ggpattern package!

The syntax below shows how to specify a different texture for each of the groups in our data frame. For this, we have to use the aes function and the pattern argument as shown below:

ggplot(data, aes(group, x, fill = group)) +    # Different pattern for each group
  geom_bar_pattern(stat = "identity",
                   pattern_color = "white",
                   pattern_fill = "black",
                   aes(pattern = group))


r graph figure 3 ggpattern r package programming language


As shown in Figure 3, the previous code drew a barplot with different pattern in each bar. Looks great, doesn’t it?!


Example 4: Drawing Density Plot with Pattern Using geom_density_pattern Function

So far, we have used the ggpattern package only for barcharts.

This example explains how to draw multiple ggplot2 densities in the same graphic with different patterns for each density using the geom_density_pattern function.

ggplot(data, aes(x, fill = group)) +           # Create density plot with pattern
  geom_density_pattern(pattern_color = "white",
                       pattern_fill = "black",
                       aes(pattern = group))


r graph figure 4 ggpattern r package programming language


As shown in Figure 4, the previous R programming syntax created a graph showing multiple densities with different textures.


Example 5: Drawing Transparent Density Plot with Pattern Using alpha & pattern_alpha Arguments

In graphics containing multiple overlapping densities, it might be useful to make the densities transparent.

For this, we can use the alpha argument provided by the ggplot2 package in combination with the pattern_alpha argument provided by the ggpattern package.

Have a look at the following R code and its output:

ggplot(data, aes(x, fill = group)) +           # Transparent density plot with pattern
  geom_density_pattern(pattern_color = "white",
                       pattern_fill = "black",
                       alpha = 0.5,
                       pattern_alpha = 0.5,
                       aes(pattern = group))


r graph figure 5 ggpattern r package programming language


Figure 5 shows the previously created transparent density plot.


Example 6: Drawing Boxplot with Pattern Using geom_boxplot_pattern Function

The following syntax explains how to apply the geom_boxplot_pattern function to draw multiple ggplot2 boxplots with patterns in the same graph:

ggplot(data, aes(x, fill = group)) +           # Create boxplot with pattern
  geom_boxplot_pattern(pattern_color = "white",
                       pattern_fill = "black",
                       aes(pattern = group))


r graph figure 6 ggpattern r package programming language


The output of the previous code is shown in Figure 6 – A graphic containing several boxplots. Each boxplot is shown with a different pattern.


Video, Further Resources & Summary

Do you need more explanations on the R programming codes of this page? Then you might want to watch the following video of my YouTube channel. I explain the R programming codes of this tutorial in the video tutorial:


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


Besides that, you may want to read some of the related articles on my homepage:


Summary: You learned in this tutorial how to apply the functions of the ggpattern package in R. Don’t hesitate to tell me about it in the comments section, in case you have further questions.


Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

2 Comments. Leave new

  • Hi Joachin, thanks for your script and notes. I’m Alexis.
    I was looking forward to install ggpattern and test with ggplot2 but after few hours it was no possible and this is message I got:
    > remotes::install_github(“coolbutuseless/ggpattern”)
    Downloading GitHub repo coolbutuseless/ggpattern@HEAD
    Error in utils::download.file(url, path, method = method, quiet = quiet, :
    download from ‘’ failed

    Any suggestion.
    Thanks a best regards!


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.