Open In App

Add Confidence Band to ggplot2 Plot in R

Last Updated : 05 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to add Add Confidence Band to ggplot2 Plot in the R programming Language.

A confidence band is the lines on a scatter plot or fitted line plot that depict the upper and lower confidence bounds for all points on the range of data. This helps us visualize the error range for data whose exact values cannot be guaranteed. To add confidence band to a scatter plot in R we use the geom_ribbon function of the ggplot2 package.

First, we will make a basic scatter plot using the geom_point() function of ggplot2 package.

Syntax:

ggplot( dataframe, aes( x, y ) ) + geom_point( )

Example: Here, is a basic scatter plot made using the ggplot() and the geom_point() function of the ggplot2 package in the R language.

R




# create data vectors
xAxis <- 1:200
yAxis <- rnorm(200) + xAxis / 10
  
# create data frame from above data vectors
sample_data <- data.frame(xAxis, yAxis)
  
# load library ggplot2
library("ggplot2")
  
# create ggplot2 scatter plot
ggplot(sample_data, aes(xAxis, yAxis)) +   
          geom_point()


Output:

To add a confidence band we need two more variables for each data variable of xAxis and yAxis vector we need a corresponding low and high vector that creates the limit of the confidence band. We can use those values in the geom_ribbon() function to create a confidence band around the scatter plot points.

Syntax:

plot + geom_ribbon( aes(ymin, ymax) )

Parameter:

  • ymin:  determines the lower limit of band
  • ymax:  determines the upper limit of band

Example: Here is a scatter plot with a confidence band made using the geom_ribbon() function of the ggplot2 package in the R Language.

R




# create data vectors
xAxis <- 1:200
yAxis <- rnorm(200) + xAxis / 10
lowBand <- yAxis + rnorm(200, - 1.5, 0.1)
highBand <- yAxis + rnorm(200, + 1.5, 0.1)
  
# create data frame from above data vectors
sample_data <- data.frame(xAxis, yAxis, lowBand, highBand)
  
# load library ggplot2
library("ggplot2")
  
# create ggplot2 scatter plot
ggplot(sample_data, aes(xAxis, yAxis)) +   
          geom_point()+
# geom_ribbon function is used to add confidence interval
          geom_ribbon(aes(ymin = lowBand, ymax = highBand), 
                      alpha = 0.2)


Output:

We can use the color, fill, and alpha parameter of the geom_ribbon() function to change the outline color, background color, and transparency of the confidence band respectively. These properties can be used alone or in combination to create the desired look and feel for the plot.

Syntax:

plot + geom_ribbon( aes(ymin, ymax), color, fill, alpha )

Parameter:

  • color:  determines the color of the outline
  • fill: determines the color of the background
  • alpha: determines the transparency of confidence band

Example: Here, we have made a scatter plot with a confidence band, highlighted using a color

R




# create data vectors
xAxis <- 1:200
yAxis <- rnorm(200) + xAxis / 10
lowBand <- yAxis + rnorm(200, - 1.5, 0.1)
highBand <- yAxis + rnorm(200, + 1.5, 0.1)
  
# create data frame from above data vectors
sample_data <- data.frame(xAxis, yAxis, lowBand, highBand)
  
# load library ggplot2
library("ggplot2")
  
# create ggplot2 scatter plot
ggplot(sample_data, aes(xAxis, yAxis)) +   
          geom_point()+
# geom_ribbon function is used to add confidence interval
          geom_ribbon(aes(ymin = lowBand, ymax = highBand), 
                      alpha = 0.2, fill="green", color="green")


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads