How To Customize Border in facet plot in ggplot2 in R
Last Updated :
05 Nov, 2021
In this article, we will discuss how to customize the border in the facet plot in ggplot2 in R Programming language.
Facet plots, where one subsets the data based on a categorical variable and makes a series of similar plots with the same scale. Facetting helps us to show the relationship between more than two categories of data. When we have multiple variables, with faceting it can be plotted in a single plot into smaller plots.
We can easily plot a facetted plot using the facet_wrap() function of the ggplot2 package. When we use facet_wrap() in ggplot2, by default it gives a title in a grey box.
Syntax: plot + facet_wrap( ~facet-variable)
Where:
- facet-variable: determines the variable around which plots have to be divided.
Creating a basic facet plot
Here, is a basic facet plot made using the diamonds data frame which is provided by R Language natively. We have used the facet_wrap() function with ~cut to divide the plot into facets according to their clarity.
R
# load library tidyverse
library(tidyverse)
# set theme_bw()
theme_set(theme_bw(18))
# Basic facet plot divided according to category
# clarity diamonds data frame is used in plot which
# is provided natively by R Language
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x="price")) +
# geom_bar function is used to create bar plot
geom_bar()+
# facet_wrap() function divides the plot in
# facets according to category of clarity
facet_wrap(~clarity)
Output:

Remove Space between panels in facet plot
To remove the space between panels we use panel.spacing.x / panel.spacing.y argument of theme() function. We can even manually specify the amount of space needed between the panels using this method.
Syntax: plot + theme( panel.spacing.x / panel.spacing.y )
Example:
Here, we have used the diamonds dataset to create a faceted bar plot and removed space between panels by using panel.spacing.x and panel.spacing.y argument of theme() function.
R
# load library tidyverse
library(tidyverse)
# set theme_bw()
theme_set(theme_bw(18))
# Basic facet plot divided according to category clarity
# diamonds data frame is used in plot which
# is provided natively by R Language
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x="price")) +
# geom_bar function is used to create bar plot
geom_bar()+
# facet_wrap() function divides the plot in
# facets according to category of clarity
facet_wrap(~clarity)+
# theme function with panel.spacing argument
# is used to modify space between panels
theme( panel.spacing.x = unit(0,"line"),
panel.spacing.y = unit(0,"line"))
Output:

Remove Panel Border Lines in facet plots
To remove the Panel Border Lines, we use panel.border argument of theme() function. we use element.blank() function as parameter for panel.border argument to remove the border.
Syntax: plot + theme( panel.border = element_blank() )
Example:
Here, we have used the diamonds dataset to create a faceted bar plot and removed panel borders by using panel.border aselement_blank() in theme() function.
R
# load library tidyverse
library(tidyverse)
# set theme_bw()
theme_set(theme_bw(18))
# Basic facet plot divided according to category clarity
# diamonds data frame is used in plot which
# is provided natively by R Language
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x="price")) +
# geom_bar function is used to create bar plot
geom_bar()+
# facet_wrap() function divides the plot in
# facets according to category of clarity
facet_wrap(~clarity)+
# theme function with panel.border is
# used to remove border of facet panels
theme( panel.border = element_blank() )
Output:
Similar Reads
How to change Colors in ggplot2 Line Plot in R ? A line graph is a chart that is used to display information in the form of series of data points. It utilizes points and lines to represent change over time. Line graphs are drawn by plotting different points on their X coordinates and Y coordinates, then by joining them together through a line from
2 min read
How To Change facet_wrap() Box Color in ggplot2 in R? In this article, we will discuss how to change facet_wrap() box color in ggplot2 in R Programming language. Facet plots, where one subsets the data based on a categorical variable and makes a series of similar plots with the same scale. Facetting helps us to show the relationship between more than t
3 min read
How to change plot area margins using ggplot2 in R? In the R programming language, ggplot2 is a popular library for creating data visualizations. One of the key benefits of using ggplot2 is the ability to customize the appearance of plots in a variety of ways. In this article, we will explore some of the ways you can customize the appearance of your
3 min read
How to increase spacing between faceted plots using ggplot2 in R ? In this article, we will see how to increase spacing between faceted plots using ggplot2 in R Programming language. Note: Here, a line plot has been used the same can be done for any other plot. To create an R plot, we will use ggplot() function and to make a line graph add geom_line() function to
3 min read
Control Point Border Thickness in ggplot2 in R In this article, we will be looking at the approach to control point border thickness in the ggplot2 plot in the R programming language. In this method to control the point border thickness in ggplot2, the user first needs to install and import the ggplot2 package in the R console and then call the
2 min read
How to plot means inside boxplot using ggplot2 in R? In this article, we are going to see how to plot means inside boxplot using ggplot in R programming language. A box plot in base R is used to summarise the distribution of a continuous variable. It can also be used to display the mean of each group. Means or medians can also be computed using a box
4 min read
How To Make Density Plots with ggplot2 in R? Density plots are a data visualization method used to estimate the probability density function (PDF) of a continuous variable. They provide smooth, continuous data distribution which makes them more informative than histograms in certain situations. The plot is produced by applying a kernel functio
2 min read
Add Panel Border to ggplot2 Plot in R In this article, we will use theme() function to add panel border to the plot in R Programming Language. Here we will create a scatter plot, but you can apply it to any plot and add a Panel Border to that one. Approach:Specify the data object, and it has to be a dataframe. Here it has two variable n
3 min read
How To Remove facet_wrap Title Box in ggplot2 in R ? In this article, we will discuss how facet_wrap works in R Programming Language. we will discuss all the types and methods. facet_wrap in RIn R Programming Language facet_wrap() is a function from the ggplot2 package that allows you to create multiple plots, or facets, based on a categorical variabl
4 min read
Reorder Facets in ggplot2 Plot in R In this article, we will be looking at an approach to reorder the facets in the ggplot2 plot in R programming language. To reorder the facets accordingly of the given ggplot2 plot, the user needs to reorder the levels of our grouping variable accordingly with the help of the levels function and requ
2 min read