Add Confidence Band to ggplot2 Plot in R - GeeksforGeeks

05 Nov, 2021
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 vectorsxAxis <- 1:200yAxis <- rnorm(200) + xAxis / 10  # create data frame from above data vectorssample_data <- data.frame(xAxis, yAxis)  # load library ggplot2library("ggplot2")  # create ggplot2 scatter plotggplot(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 vectorsxAxis <- 1:200yAxis <- rnorm(200) + xAxis / 10lowBand <- yAxis + rnorm(200, - 1.5, 0.1)highBand <- yAxis + rnorm(200, + 1.5, 0.1)  # create data frame from above data vectorssample_data <- data.frame(xAxis, yAxis, lowBand, highBand)  # load library ggplot2library("ggplot2")  # create ggplot2 scatter plotggplot(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 vectorsxAxis <- 1:200yAxis <- rnorm(200) + xAxis / 10lowBand <- yAxis + rnorm(200, - 1.5, 0.1)highBand <- yAxis + rnorm(200, + 1.5, 0.1)  # create data frame from above data vectorssample_data <- data.frame(xAxis, yAxis, lowBand, highBand)  # load library ggplot2library("ggplot2")  # create ggplot2 scatter plotggplot(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:
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 Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
Time Series Analysis in R
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
R - if statement