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

 

table 1 data frame draw ggplot2 different background colors region r

 

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")

Next, we can draw our data:

ggplot(data, aes(x, y)) +                         # Draw default plot
  geom_point()

 

r graph figure 1 draw ggplot2 different background colors region r

 

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

 

table 2 data frame draw ggplot2 different background colors region r

 

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))

 

r graph figure 2 draw ggplot2 different background colors region r

 

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.

 

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.

 

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

  • How can we manually choose the colors of each region?

    Reply
    • Hey Chris,

      You can accomplish that using the scale_fill_manual function. Have a look at the following example code:

      ggplot() +
        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)) +
        scale_fill_manual(values = c("blue",
                                     "green",
                                     "yellow",
                                     "red"))

      manual colors

      Regards,
      Joachim

      Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top