Draw ggplot2 Plot with Different Background Colors by Region in R (Example)
In this article, I’ll demonstrate how to create a ggplot2 graph with different background colors by region in the R programming language.
Table of contents:
Let’s dig in.
Example Data, Software Packages & Basic Graph
The following data will be used as basement for this R programming tutorial.
data <- data.frame(x = 1:10, # Create example data y = 1:10) data # Print example data |
data <- data.frame(x = 1:10, # Create example data y = 1:10) data # Print example data
Table 1 shows the structure of the exemplifying data: It consists of ten rows and two integer columns that are called “x” and “y”.
We also have to install and load the ggplot2 package, to be able to use the corresponding functions:
install.packages("ggplot2") # Install & load ggplot2 library("ggplot2") |
install.packages("ggplot2") # Install & load ggplot2 library("ggplot2")
Next, we can draw our data:
ggplot(data, aes(x, y)) + # Draw default plot geom_point() |
ggplot(data, aes(x, y)) + # Draw default plot geom_point()
After executing the previous R syntax the scatterplot shown in Figure 1 has been created. As you can see, our plot does not have any additional background colors yet.
Let’s change this!
Example: Add Background Colors to ggplot2 Plot Using geom_rect() Function
The following R programming syntax shows how to assign certain background colors to particular areas in a ggplot2 plot.
For this, we first have to create a data frame containing the locations at which we want to switch from one color to another:
data_breaks <- data.frame(start = c(0, 2, 5, 9), # Create data with breaks end = c(2, 5, 9, 10), colors = factor(1:4)) data_breaks # Print data with breaks |
data_breaks <- data.frame(start = c(0, 2, 5, 9), # Create data with breaks end = c(2, 5, 9, 10), colors = factor(1:4)) data_breaks # Print data with breaks
As shown in Table 2, the previous R code has created a data set with four color breaks.
Next, we can apply the geom_rect function to this data set to specify the background colors in our plot.
Have a look at the R syntax below:
ggplot() + # Add background colors to plot geom_rect(data = data_breaks, aes(xmin = start, xmax = end, ymin = - Inf, ymax = Inf, fill = colors), alpha = 0.5) + geom_point(data = data, aes(x, y)) |
ggplot() + # Add background colors to plot geom_rect(data = data_breaks, aes(xmin = start, xmax = end, ymin = - Inf, ymax = Inf, fill = colors), alpha = 0.5) + geom_point(data = data, aes(x, y))
Figure 2 shows the output of the previous syntax: We have changed the background colors of four regions in our graphic.
Video, Further Resources & Summary
I have recently published a video on the Statistics Globe YouTube channel, which shows the R syntax of this article. You can find the video below.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on www.statisticsglobe.com. I have published several other articles about related topics such as ggplot2 and graphics in R.
- Change Background Color of ggplot2 Plot in R
- Add Image to Plot in R
- Draw ggplot2 Plot with Lines and Points
- ggplot2 Plot with Transparent Background
- Draw Dates to X-Axis of Plot
- Draw Histogram with Different Colors
- Graphics Gallery in R
- R Programming Language
This page has shown how to draw a ggplot2 plot with multiple background colors for each portion of the plot in R.
Note that we have colored each plot region differently in the present tutorial. However, we could also use the shown R syntax to perform other tasks such as adding shade between two vertical lines.
In case you have any further questions, please let me know in the comments section.
Statistics Globe Newsletter
2 Comments. Leave new
How can we manually choose the colors of each region?
Hey Chris,
You can accomplish that using the scale_fill_manual function. Have a look at the following example code:
Regards,
Joachim