Fitting a Smooth Line to Data in R Programming

In this tutorial, I’ll show how to draw a smooth line to a scatterplot in the R programming language.

Creation of Example Data

data(iris)                      # Load iris data set
x <- 1:nrow(iris)               # Store relevant values
y <- iris$Sepal.Length
plot(x, y)                      # Plot iris without line

r graph figure 1 fitting a smooth line to data r programming

Example: Fit Smooth Line ti Iris Data

curve_values <- loess(y ~ x)    # Application of loess function
plot(x, y)                      # Iris data with fitted line
lines(predict(curve_values),
      col = "red",
      lwd = 3)

r graph figure 2 fitting a smooth line to data r programming

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top