# Fit Smooth Curve to Plot of Data in R (Example)

In this tutorial you’ll learn how to draw a smooth line to a scatterplot in the R programming language.

Table of contents:

Here’s how to do it:

## Introduction of Example Data

The following data is used as basement for this R programming tutorial:

```set.seed(87564832) # Create example data x <- 1:100 y <- sort(rnorm(100))```

The previous output of the RStudio console shows the structure of our example data. It consists of two numeric vectors each containing 100 values.

Now, we can draw our data as follows:

`plot(x, y) # Plot without line` Figure 1 visualizes the output of the previous R syntax: A scatterplot showing our data. However, this scatterplot does not show a fitted curve yet…

## Example: Creating Scatterplot with Fitted Smooth Line

The following R code explains how to draw a fitted curve to our example plot. Have a look at the following R code:

`curve_values <- loess(y ~ x) # Apply loess function`
```plot(x, y) # Plot with line lines(predict(curve_values), col = "red", lwd = 3)``` As shown in Figure 2, we created a scatterplot with a fitted curve with the previous R code.

## Video & Further Resources

Do you want to learn more about smooth curves in graphics? Then I can recommend to watch the following video of my YouTube channel. In the video, I’m explaining the topics of this article:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

Furthermore, you may have a look at the related R tutorials of my website. Some posts are shown below.

Summary: You learned in this article how to add a smooth curve to a plot in the R programming language. In case you have further questions or comments, let me know in the comments section below. Furthermore, don’t forget to subscribe to my email newsletter to receive updates on new articles.

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Menu
Top