Table of Contents
Adding a confidence interval in ggplot2 is a useful way to visualize the uncertainty in data and provide a range of values within which the true value is likely to fall. This can be done by using the geom_errorbar() function, which allows you to specify the confidence interval for each data point. For example, if you have a scatter plot showing the relationship between two variables, you can add a confidence interval around each point to illustrate the range of values that are statistically significant. This can be particularly helpful in comparing groups or identifying patterns in the data. Simply specify the desired confidence level and use the appropriate statistical function to calculate the confidence interval, then add the geom_errorbar() layer to your ggplot. This will create error bars for each data point representing the confidence interval.
Add a Confidence Interval in ggplot2 (With Example)
You can use geom_smooth() to add confidence interval lines to a plot in ggplot2:
library(ggplot2)
some_ggplot +
geom_point() +
geom_smooth(method=lm)The following examples show how to use this syntax in practice with the built-in dataset in R.
Example 1: Add Confidence Interval Lines in ggplot2
The following code shows how to create a scatterplot in ggplot2 and add a line of best fit along with 95% confidence bands:
library(ggplot2)
#create scatterplot with confidence bands
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
geom_point() +
geom_smooth(method=lm)

The blue line represents the fitted linear regression line and the grey bands represent the 95% confidence interval bands.
Example 2: Modify Level of Confidence Interval
By default, geom_smooth() uses 95% confidence bands but you can use the level argument to specify a different confidence level.
For example, we may choose to create 90% confidence bands instead:
library(ggplot2)
#create scatterplot with 90% confidence bands
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
geom_point() +
geom_smooth(method=lm, level=0.90)

The smaller the confidence level you use, the more narrow the confidence interval bands will be around the regression line.
Example 3: Modify Appearance of Confidence Interval Lines
You can also use the color and fill arguments to modify the color of the regression line and the color of the confidence interval bands, respectively:
library(ggplot2)
#create scatterplot with custom confidence interval lines
ggplot(data=mtcars, aes(x=mpg, y=wt)) +
geom_point() +
geom_smooth(method=lm, color='red', fill='lightblue')

The regression line is now red and the confidence interval bands are filled in with light blue.
Note: You can find the complete documentation for the geom_smooth() function .
Additional Resources
The following tutorials explain how to perform other common operations in ggplot2:
Cite this article
stats writer (2024). How can I add a confidence interval in ggplot2 with an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-a-confidence-interval-in-ggplot2-with-an-example/
stats writer. "How can I add a confidence interval in ggplot2 with an example?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-a-confidence-interval-in-ggplot2-with-an-example/.
stats writer. "How can I add a confidence interval in ggplot2 with an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-a-confidence-interval-in-ggplot2-with-an-example/.
stats writer (2024) 'How can I add a confidence interval in ggplot2 with an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-a-confidence-interval-in-ggplot2-with-an-example/.
[1] stats writer, "How can I add a confidence interval in ggplot2 with an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I add a confidence interval in ggplot2 with an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
