How To Change facet_wrap() Box Color in ggplot2 in R?
Last Updated :
19 Nov, 2021
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 two categories of data. When you 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 cut.
R
# load library ggridges and tidyverse
library(ggridges)
library(tidyverse)
# Basic facet plot divided according to category cut
# 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", y="cut", fill="cut")) +
# geom_density_ridges() function is used to draw ridgeline plot
geom_density_ridges()+
# facet_wrap() function divides the plot in facets according to category of cut
facet_wrap(~cut)
Output:

Box Color Customization
We can customize various aspects of a ggplot2 using the theme() function. To change the default grey fill color in the facet_wrap() title box, we need to use “strip.background” argument inside the theme() layer with color and fill property of element_rect() function.
Syntax: plot + theme( strip.background = element_rect( colour, fill ))
Where:
- colour: determines the color of outline of box
- fill: determines the color of background fill of box
Example:
In this example, we specified element_rect with yellow fill color and black for box outline color.
R
# load library ggridges and tidyverse
library(ggridges)
library(tidyverse)
# ggplot() function is used to plot the chart
ggplot(diamonds, aes(x="price", y="cut", fill="cut")) +
# geom_density_ridges() function is used
# to draw ridgeline plot
geom_density_ridges()+
# facet_wrap() function divides the plot in
# facets according to category of cut
facet_wrap(~cut)+
# strip.background parameter of theme
# function is used to change color of box
# color and fill property of element_rect()
# function is used for color change
theme(legend.position="none",
strip.background=element_rect(colour="black",
fill="yellow"))
Output:
Similar Reads
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
How to change background color in R using ggplot2? In this article, we will discuss how to change the background color of a ggplot2 plot in R Programming Language. To do so first we will create a basic ggplot2 plot. Step 1: Create sample data for the plot. sample_data <- data.frame(x = 1:10, y = 1:10) Step 2: Load the package ggplot2. library("gg
2 min read
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
Change Color of ggplot2 Boxplot in R In this article, we are going to see how to change the color of boxplots using ggplot2 in R Programming Language. We have considered the built-in data frame "ChickWeight". It contains information about the feed type and growth rate of chickens for six different types of foods like casein, soybean,
3 min read
How To Customize Border in facet plot in ggplot2 in R 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 mo
3 min read