Table of Contents
In order to plot a polynomial regression curve in R, one must first import the necessary data into the R environment. This data should include both the independent and dependent variables. Once the data is imported, the next step is to fit a polynomial regression model using the “lm” function. This function allows for the specification of the degree of the polynomial, as well as any interaction terms. After fitting the model, the “predict” function can be used to generate predicted values for the dependent variable. These predicted values can then be plotted against the original data to create the polynomial regression curve. Additional customization, such as adding a legend or adjusting the appearance of the plot, can be done using R’s built-in plotting functions.
Plot a Polynomial Regression Curve in R
Polynomial regression is a regression technique we use when the relationship between a predictor variable and a response variable is nonlinear.
This tutorial explains how to plot a polynomial regression curve in R.
Example: Plot Polynomial Regression Curve in R
The following code shows how to fit a polynomial regression model to a dataset and then plot the polynomial regression curve over the raw data in a scatterplot:
#define data x <- runif(50, 5, 15) y <- 0.1*x^3 - 0.5 * x^2 - x + 5 + rnorm(length(x),0,10) #plot x vs. y plot(x, y, pch=16, cex=1.5) #fit polynomial regression model fit <- lm(y ~ x + I(x^2) + I(x^3)) #use model to get predicted values pred <- predict(fit) ix <- sort(x, index.return=T)$ix #add polynomial curve to plot lines(x[ix], pred[ix], col='red', lwd=2)

We can also add the fitted polynomial regression equation to the plot using the text() function:
#define data x <- runif(50, 5, 15) y <- 0.1*x^3 - 0.5 * x^2 - x + 5 + rnorm(length(x),0,10) #plot x vs. y plot(x, y, pch=16, cex=1.5) #fit polynomial regression model fit <- lm(y ~ x + I(x^2) + I(x^3)) #use model to get predicted values pred <- predict(fit) ix <- sort(x, index.return=T)$ix #add polynomial curve to plot lines(x[ix], pred[ix], col='red', lwd=2) #get model coefficients coeff <- round(fit$coefficients , 2) #add fitted model equation to plot text(9, 200 , paste("Model: ", coeff[1], " + ", coeff[2], "*x", "+", coeff[3], "*x^2", "+", coeff[4], "*x^3"), cex=1.3)

Note that the cex argument controls the font size of the text. The default value is 1, so we chose to use a value of 1.3 to make the text easier to read.
Cite this article
stats writer (2024). How can I plot a polynomial regression curve in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-plot-a-polynomial-regression-curve-in-r/
stats writer. "How can I plot a polynomial regression curve in R?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-plot-a-polynomial-regression-curve-in-r/.
stats writer. "How can I plot a polynomial regression curve in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-plot-a-polynomial-regression-curve-in-r/.
stats writer (2024) 'How can I plot a polynomial regression curve in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-plot-a-polynomial-regression-curve-in-r/.
[1] stats writer, "How can I plot a polynomial regression curve in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I plot a polynomial regression curve in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
