How to Draw Plots with Textures and Patterns in R

How to Draw Plots with Textures and Patterns in R, In this tutorial shows how to use the ggpattern package in R programming to create ggplot2 graphs using textures and patterns.

Some Information on the ggpattern Package

To add geometric and image-based patterns to ggplot2 charts in R, use the ggpattern library.

This tool takes the ggplot2 package’s data visualizations to the next level!

It might be useful to have another technique (apart from colours) to differentiate between the different groups in your data, especially if you wish to present different data groups in the same picture.

Sounds appealing? So, how can we get the ggpattern package installed?

install.packages("ggpattern")                               
library("ggpattern")                          

If we wish to use the ggplot2 package to plot our data, we must first install and load it.

install.packages("ggplot2")                   
library("ggplot2")

Before we get started with the examples in this tutorial, we’d like to thank Mike FC, the package’s creator.

The development process and many other tutorials on the ggpattern package may be found on this homepage.

Example Information

For this R tutorial, the following data will be utilized as the foundation.

How to interpret Margin of Error Results?

set.seed(123)
data <- data.frame(x = rnorm(50, 10),
                   group = letters[1:5])
head(data)                       
          x group
1  9.439524     a
2  9.769823     b
3 11.558708     c
4 10.070508     d
5 10.129288     e
6 11.715065     a

Take a look at the previous RStudio console output. It demonstrates that the data in our case contains two columns.

Our data is divided into five groups by the variable x, which comprises randomly distributed numeric values.

Let’s start drawing these numbers!

Example 1: Drawing a Barplot with a Pattern Using the function geom bar pattern

How to make a ggplot2 bar chart using the ggpattern package’s default pattern.

To do so, simply call the ggpattern package’s geom bar pattern function instead of the ggplot2 package’s geom bar pattern function.

ggplot(data, aes(group, x, fill = group)) + 
  geom_bar_pattern(stat = "identity")

The output of the previous R programming syntax is shown in Figure 1: Each bar in this ggplot2 barplot has the same texture.

Example 2: Manually specify pattern colours Arguments for pattern color and pattern fill

WeI’ll show you how to adjust the colours of our design in Example 2.

The pattern color argument can be used to specify the pattern’s boundaries, and the pattern fill argument can be used to change the pattern’s filling colour.

ggplot(data, aes(group, x, fill = group)) +  
geom_bar_pattern(stat = "identity",
                   pattern_color = "white",
                   pattern_fill = "black")

The preceding syntax created another barchart with patterns, as seen in Figure 2, but this time the colours of the patterns were set manually.

Example 3: Using the aes Function and the pattern Argument, create a barplot with a pattern for each group.

This example, in my opinion, demonstrates the most significant feature of the ggpattern package!

The syntax for specifying a different texture for each of the groups in our data frame is shown below. As seen below, we must use the aes function and the pattern argument to do this.

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

The prior code created a barplot with various patterns in each bar, as illustrated in Figure 3. Doesn’t it look fantastic?!

Example 4: Drawing a Density Plot with a Pattern Using the function geom density pattern

We’ve just used the ggpattern package for barcharts so far.

Using the geom density pattern function, this example shows how to generate numerous ggplot2 densities in the same graphic with distinct patterns for each density.

How to Add p-values onto ggplot »

ggplot(data, aes(x, fill = group)) +     
geom_density_pattern(pattern_color = "white",
                       pattern_fill = "black",
                       aes(pattern = group))

The prior R programming syntax produced a graph with varied densities and textures, as seen in Figure 4.

Example 5: Creating a Patterned Transparent Density Plot Arguments alpha and pattern alpha

It might be good to make the densities transparent in graphics with numerous overlapping densities.

We can do this by combining the alpha argument from the ggplot2 package with the pattern alpha argument from the ggpattern package.

Take a look at the R code below and the results:

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

Example 6: Drawing a Boxplot with a Pattern Using the function geom boxplot pattern

The syntax for using the geom boxplot pattern function to draw several ggplot2 boxplots with patterns in the same graph is as follows.

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

Figure 6 – A visual with many boxplots – is the result of the previous code. A distinct pattern is used for each boxplot.

You may also like...

Leave a Reply

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

four + eleven =