Drawing Only Boundaries of stat_smooth in ggplot2 using R
Last Updated :
23 Sep, 2024
When creating plots with ggplot2
, you often use stat_smooth()
to add a smooth curve to visualize trends in your data. By default, stat_smooth()
includes both the smoothed line and the shaded confidence interval. However, in certain cases, you may only want to show the boundaries of the confidence interval without filling the area between them. In this article, we will cover how to draw only the boundaries of the confidence interval (without shading) using stat_smooth()
in ggplot2
.
Overview of stat_smooth()
The stat_smooth()
function in ggplot2
is used to add a smoothed conditional mean to a plot. It often includes:
- A line representing the estimated trend.
- A shaded region represents the confidence interval around the smooth line.
The goal here is to display only the boundaries of the confidence interval and remove the shaded region.
Method 1: Default Behavior of stat_smooth()
Let’s first see how stat_smooth()
behaves by default, with both the trend line and the shaded confidence interval. We will use the mtcars
dataset as an example.
R
# Load the required libraries
library(ggplot2)
# Create a basic scatter plot with stat_smooth()
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
stat_smooth(method = "lm", se = TRUE) +
labs(title = "Default stat_smooth() with Confidence Interval")
Output:
Default Behavior of stat_smooth()geom_point()
: Creates a scatter plot.stat_smooth()
: Adds a smoothed line (method = "lm"
specifies linear regression) and the shaded confidence interval (since se = TRUE
).
By default, the plot shows a regression line with a shaded confidence interval.
Method 2: Drawing Only the Boundaries of the Confidence Interval
To show only the boundaries of the confidence interval without filling the region, we can use the following steps:
- Turn off the shading by setting
se = FALSE
in stat_smooth()
. - Manually add the confidence interval boundaries using
geom_ribbon()
or geom_line()
.
Step 1: Remove the Shading with se = FALSE
By setting the se = FALSE
argument in stat_smooth()
, you can remove the shaded region:
R
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
stat_smooth(method = "lm", se = FALSE) +
labs(title = "Smoothed Line without Confidence Interval Shading")
Output:
Drawing Only Boundaries of stat_smooth in ggplot2 using RThis removes the confidence interval entirely, but the goal is to add just the boundaries back.
Step 2: Add Boundaries Using geom_line()
and predict()
To manually add the boundaries of the confidence interval, you can use the predict()
function to calculate the upper and lower bounds and then plot them using geom_line()
.
R
# Fit a linear model
fit <- lm(mpg ~ wt, data = mtcars)
# Create a data frame with fitted values and confidence intervals
pred_data <- data.frame(
wt = mtcars$wt,
mpg = predict(fit, newdata = mtcars, interval = "confidence")
)
# Create the plot
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_line(aes(y = mpg.fit), data = pred_data, color = "blue", size = 1) + # Smoothed line
geom_line(aes(y = mpg.lwr), data = pred_data, linetype = "dashed", color = "red") + # Lower boundary
geom_line(aes(y = mpg.upr), data = pred_data, linetype = "dashed", color = "red") + # Upper boundary
labs(title = "Linear Regression with Confidence Interval Boundaries",
x = "Weight",
y = "Miles per Gallon") +
theme_minimal()
Output:
Drawing Only Boundaries of stat_smooth in ggplot2 using Rpredict()
is used to calculate the fitted values and the confidence intervals.geom_line()
is used to add both the smoothed line and the upper/lower boundaries of the confidence intervals as dashed lines.- The smoothed line is blue, and the boundaries are represented by dashed red lines.
This plot shows the smoothed regression line with the upper and lower confidence interval boundaries, but without shading between the two boundaries.
Method 3: Using geom_smooth()
for Built-In Confidence Interval Boundaries
If you want a simpler approach using ggplot2
, you can use geom_smooth()
with the fullrange = TRUE
argument and adjust the alpha
of the fill to 0, making the shading invisible while still keeping the boundaries.
R
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE, fill = NA, linetype = "dashed", color = "red") +
labs(title = "Confidence Interval Boundaries Without Shading",
x = "Weight",
y = "Miles per Gallon") +
theme_minimal()
Output:
Drawing Only Boundaries of stat_smooth in ggplot2 using Rfill = NA
: Removes the shading of the confidence interval.linetype = "dashed"
: Changes the confidence interval lines to dashed lines.color = "red"
: Changes the color of the confidence interval boundaries.
This plot displays only the dashed boundaries of the confidence interval without shading.
Conclusion
In this guide, we learned how to draw only the boundaries of the confidence interval without shading using stat_smooth()
and geom_line()
in ggplot2
. Whether you prefer to manually calculate the confidence intervals and plot them or use a simpler built-in method, R provides flexible options for customizing your plots.
Similar Reads
Adding stat_smooth in to only 1 facet in ggplot2
In data visualization with ggplot2, faceting allows us to split the data into subsets and create separate plots for each subset. stat_smooth is a function in ggplot2 that adds a smooth trend line to plots based on the underlying data. Combining these features allows for powerful visualizations where
4 min read
How to Plot a Smooth Line using ggplot2 in R ?
In this article, we will learn how to plot a smooth line using ggplot2 in R Programming Language. We will be using the "USArrests" data set as a sample dataset for this article. Murder Assault UrbanPop Rape Alabama 13.2 236 58 21.2 Alaska 10.0 263 48 44.5 Arizona 8.1 294 80 31.0 Arkansas 8.8 190 50
3 min read
Smooth data for a geom_area graph Using ggplot2 in R
The ggplot2 package is a powerful and widely used package for graphic visualization. It can be used to provide a lot of aesthetic mappings to the plotted graphs. This package is widely available in R. The package can be downloaded and installed into the working space using the following command : in
3 min read
How to plot a subset of a dataframe using ggplot2 in R ?
In this article, we will discuss plotting a subset of a data frame using ggplot2 in the R programming language. Dataframe in use: Â AgeScoreEnrollNo117700521880103177915419752051885256199630717903581971409188345 To get a complete picture, let us first draw a complete data frame. Example: [GFGTABS] R
8 min read
How to change plot area margins using ggplot2 in R?
In the R programming language, ggplot2 is a popular library for creating data visualizations. One of the key benefits of using ggplot2 is the ability to customize the appearance of plots in a variety of ways. In this article, we will explore some of the ways you can customize the appearance of your
3 min read
Plot from DataFrame in ggplot2 using R
ggplot2 is a popular data visualization library in the R programming language. It is widely used for creating beautiful, customizable, and informative visualizations. One of the most useful features of ggplot2 is the ability to plot data stored in a data frame. In this article, we will learn how to
4 min read
Rotating and spacing axis labels in ggplot2 in R
In this article, we will discuss how to Rotate and space axis labels in the ggplot2 in the R Programming Language. Spacing the axis labels: We can increase or decrease the space between the axis label and axis using the theme function. The axis.txt.x / axis.text.y parameter of theme() function is us
3 min read
How to change background color in R using ggplot2?
In this article, we will discuss how to change the background color of a ggplot2 plot in R Programming Language. To do so first we will create a basic ggplot2 plot. Step 1: Create sample data for the plot. sample_data <- data.frame(x = 1:10, y = 1:10) Step 2: Load the package ggplot2. library("gg
2 min read
Adding table within the plotting region of a ggplot in R
In this article, we are going to see how to add the data frame table in the region of the plot using ggplot2 library in R programming language. Dataset in use: Here we are plotting a scatterplot, the same can be done for any other plot. To plot a scatter plot in ggplot2, we use the function geom_poi
2 min read
Set Axis Breaks of ggplot2 Plot in R
In this article, we are going to see how to set axis break of ggplot2 plot in R Programming Language. To add axis breaks in ggplot2 plots in R, we use scale_x_break() and scale_y_break() functions. These functions take a vector as a parameter that has breakpoints.  If we need multiple breakpoints w
2 min read