How To Customize Border in facet plot in ggplot2 in R

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.
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 tidyverselibrary(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 chartggplot(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:
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 tidyverselibrary(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 chartggplot(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 panelstheme( panel.spacing.x = unit(0,"line"),        panel.spacing.y = unit(0,"line"))
Output:
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 tidyverselibrary(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 chartggplot(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 panelstheme( panel.border = element_blank() )
Output:
Picked
R-ggplot
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
R - if statement
Logistic Regression in R Programming
Replace Specific Characters in String in R
How to import an Excel File into R ?
Joining of Dataframes in R Programming