Increase Space Between ggplot2 Facet Plot Panels in R (Example)

 

In this tutorial you’ll learn how to add additional space between the panels of a ggplot2 facet grid plot in the R programming language.

Table of contents:

It’s time to dive into the exemplifying R syntax:

 

Example Data, Packages & Basic Plot

I’ll use the data below as basement for this tutorial.

data <- data.frame(x = 6:1,                        # Create example data frame
                   y = 1:6,
                   group = letters[1:3])
data                                               # Print example data frame

 

table 1 data frame increase space between ggplot2 facet panels r

 

Have a look at the table that got returned by the previous R programming syntax. It shows that our example data consists of six data points and three variables.

In order to draw our data with the ggplot2 package, we also need to install and load ggplot2:

install.packages("ggplot2")                        # Install & load ggplot2 package
library("ggplot2")

Now, we can plot our data in a facet plot with default spacing as shown below:

ggp <- ggplot(data, aes(x, y, group = group)) +    # Create facet plot
  geom_line() +
  facet_wrap(group ~ .)
ggp                                                # Draw facet plot

 

r graph figure 1 increase space between ggplot2 facet panels r

 

The output of the previous syntax is shown in Figure 1 – A ggplot2 facet grid graph with relatively small spaces between the pot panels.

 

Example: Increase Space Between ggplot2 Facet Plot Panels Using theme() Function & panel.spacing Argument

The following code illustrates how to add more white space between the margins of each panel of our ggplot2 facet plot.

For this, we can use the theme function and the panel.spacing argument as shown below. Note that we can use different units and values within the unit function.

ggp +                                              # Increase spacing between plot panels
  theme(panel.spacing = unit(3, "cm"))

 

r graph figure 2 increase space between ggplot2 facet panels r

 

Figure 2 shows the output of the previous R syntax – A ggplot2 facet plot with increased spacing between the categories.

 

Video, Further Resources & Summary

Do you need more info on the R programming codes of this article? Then I recommend having a look at the following video of my YouTube channel. I’m explaining the contents of this article in the video:

 

 

Furthermore, you may want to read the other articles that I have published on my website:

 

In this tutorial, I have explained how to increase the spacing between facet groups in a ggplot2 graphic in R programming.

Please note that we have used the facet_wrap function to create our facet plot in this tutorial. However, the same R code could be applied to other facet plot types created based on functions such as facet_grid and facet_null.

Don’t hesitate to let me know in the comments, if you have further questions.

 

Subscribe to the Statistics Globe 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

  • This works great when all the plots are on the same row but what if my facet grid has one plot on each row? how do I add vertical space?

    Reply
    • Hey Lu,

      The code of this tutorial also works in this case. Have a look at the following example:

      data <- data.frame(x = 8:1,                        # Create example data frame
                         y = 1:8,
                         group = letters[1:4])
      data                                               # Print example data frame
       
      library("ggplot2")
       
      ggp <- ggplot(data, aes(x, y, group = group)) +    # Create facet plot
        geom_line() +
        facet_wrap(group ~ .)
      ggp                                                # Draw facet plot
       
      ggp +                                              # Increase spacing between plot panels
        theme(panel.spacing = unit(3, "cm"))

       

      vertical space

       

      Regards,
      Joachim

      Reply

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.

Top