How to Create a Venn Diagram in R (8 Examples)

 

This article illustrates how to draw venn diagrams in the R programming language. Venn diagrams are also referred to as primary diagram, set diagram, or logic diagram.

Table of contents:

Let’s do this:

 

Setting Up VennDiagram Package

In the examples of this R tutorial, we’ll use functions provided by the VennDiagram add-on package for the R programming language. In order to use the functions of VennDiagram, we need to install and load the package first:

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

I recommend to have a look at the help documentation of the VennDiagram package. However, in the following examples you’ll learn how to apply the main functions of the VennDiagram package.

 

Example 1: Single Venn Diagram in R

Example 1 shows how to draw a single venn diagram, i.e. only one circle. Have a look at the following R code:

grid.newpage()                                        # Move to new plotting page
draw.single.venn(area = 10)                           # Create single venn diagram

 

Venn Diagram Example 1

Figure 1: Single Venn Diagram in R.

 

Figure 1 is visualizing the output of the previous R syntax. Note that the previous code contains two steps.

First, we are creating a new plotting page with the grid.newpage function. We should usually do this step before the creation of each venn diagram, because otherwise the venn diagram is just overlaying previously created plots.

Second, we are producing our single venn diagram with the draw.single.venn function. All we are specifying within the function is the size of our area (i.e. 10).

 

Example 2: Pairwise Venn Diagram

The VennDiagram package provides functions for the production of venn diagrams of basically every number of sets (i.e. circles). The draw.pairwise.venn is used to draw pairwise venn diagrams.

If we want to apply the draw.pairwise.venn command, we need to specify the sizes of the areas of both sets as well as the intersection of the two sets:

grid.newpage()                                        # Move to new plotting page
draw.pairwise.venn(area1 = 10,                        # Create pairwise venn diagram
                   area2 = 20,
                   cross.area = 2)

 

Venn Diagram Example 2

Figure 2: Pairwise Venn Diagram.

 

Figure 2 is showing the output of our previous R code. As you can see, the size of the areas are reflected in the visualization of the pairwise venn diagram.

 

Example 3: Venn Diagram with Three Sets

Similar to the R programming code of Example 2, we can use the draw.triple.venn function to create a venn diagram with three sets. Note that this time we need to specify three different area values as well as the pairwise intersections and the intersection area of all sets:

grid.newpage()                                        # Move to new plotting page
draw.triple.venn(area1 = 10,                          # Create venn diagram with three sets
                 area2 = 20,
                 area3 = 15,
                 n12 = 2,
                 n23 = 3,
                 n13 = 7,
                 n123 = 2)

 

Venn Diagram Example 3

Figure 3: Triple Venn Diagram.

 

Note that the VennDiagram package provides further functions for more complex venn diagrams with multiple sets, i.e. draw.quad.venn, draw.quintuple.venn, or the more general function venn.diagram, which is taking a list and creates a TIFF-file in publication-quality.

However, for simplicity we’ll stick to the triple venn diagram in the remaining examples of this R tutorial.

 

Example 4: Change Color of Venn Diagram

In Example 4, I’ll show you how to make a venn diagram with colored lines around the circles and a filling color of the circles. The following R code is the same as in Example 3, but in addition we are specifying the line color to be red and the filling color to be blue (with the HEX-code #1b98e0). Have a look at the output:

grid.newpage()                                        # Move to new plotting page
draw.triple.venn(area1 = 10,                          # Change color of venn diagram
                 area2 = 20,
                 area3 = 15,
                 n12 = 2,
                 n23 = 3,
                 n13 = 7,
                 n123 = 2,
                 col = "red",
                 fill = "#1b98e0")

 

Venn Diagram Example 4

Figure 4: Venn Diagram with Color.

 

Example 5: Specify Different Color for Each Set

We can also specify a different color for each of the sets of our venn diagram. For this task, we need to set the fill argument to be equal to a vector of colors. Each element of this vector is defining the color of one of the circles:

grid.newpage()                                        # Move to new plotting page
draw.triple.venn(area1 = 10,                          # Different color for each set
                 area2 = 20,
                 area3 = 15,
                 n12 = 2,
                 n23 = 3,
                 n13 = 7,
                 n123 = 2,
                 col = "red",
                 fill = c("pink", "green", "orange"))

 

Venn Diagram Example 5

Figure 5: Different Color for Each Circle.

 

Example 6: Disable Transparency of Venn Diagram

You may have noticed that the previous venn diagrams are transparent, i.e. showing the intersections in a mixed and overlapping color. If we want to reduce or even disable this transparency, we can use the alpha argument of the VennDiagram functions. An alpha of 1, as shown below, is disabling the transparency completely:

grid.newpage()                                        # Move to new plotting page
draw.triple.venn(area1 = 10,                          # Disable transparency of venn diagram
                 area2 = 20,
                 area3 = 15,
                 n12 = 2,
                 n23 = 3,
                 n13 = 7,
                 n123 = 2,
                 col = "red",
                 fill = c("pink", "green", "orange"),
                 alpha = 1)

 

Venn Diagram Example 6

Figure 6: No Transparency in Venn Diagram.

 

Example 7: Remove Lines from Venn Diagram

The VennDiagram functions provide the possibility to remove the lines around the circles by specifying the lty argument to be equal to blank. Let’s do this in practice:

grid.newpage()                                        # Move to new plotting page
draw.triple.venn(area1 = 10,                          # Remove lines from venn diagram
                 area2 = 20,
                 area3 = 15,
                 n12 = 2,
                 n23 = 3,
                 n13 = 7,
                 n123 = 2,
                 fill = c("pink", "green", "orange"),
                 lty = "blank")

 

Venn Diagram Example 7

Figure 7: Venn Diagram without Lines.

 

Example 8: Add Name to Each Set of Venn Diagram

Finally, I want to show you how to assign category names (or labels) to each of our sets. We can do that by assigning a vector of category names to the category option of the VennDiagram functions:

grid.newpage()                                        # Move to new plotting page
draw.triple.venn(area1 = 10,                          # Add name to each set
                 area2 = 20,
                 area3 = 15,
                 n12 = 2,
                 n23 = 3,
                 n13 = 7,
                 n123 = 2,
                 fill = c("pink", "green", "orange"),
                 lty = "blank",
                 category = c("Group 1", "Group 2", "Group 3"))

 

Venn Diagram Example 8

Figure 8: Adding Category Names.

 

Figure 8 is the same as Figure 7, but this time it is showing labels for each circle.

 

Video, Further Resources & Summary

If you need further info on the examples of this page, you might want to watch the following video which I have published on my YouTube channel. In the video, I illustrate the R codes of this tutorial.

 

 

Additionally, you might read the other tutorials of my website.

 

In summary: In this R tutorial you learned how to generate a venn diagram in RStudio. Please tell me about it in the comments section, if you have further questions or comments. Furthermore, don’t forget to subscribe to my email newsletter in order to receive updates on the newest articles.

 



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.

Menu
Top