Generate an R lowess line graph
We can generate a lowess
line on top of the Scatter diagram. The lowess
line would likely show a better fit as smoothing is used to fit the line to the data.
How to do it...
We can use the same Scatter diagram as the basis and then call upon lines
to add our lowess
line:
# load the iris dataset data <- read.csv("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") #Let us also clean up the data so as to be more readable colnames(data) <- c("sepal_length", "sepal_width", "petal_length", "petal_width", "species") # call plot first plot(data$sepal_length, data$petal_length) # add the lowess line to the graph lines(lowess(data$sepal_length, data$petal_length), col="blue")
Here is the resulting graph:
How it works...
Locally weighted scatterplot smoothing (LOWESS) is a useful mechanism when performing a regression to see a smoothed line drawn through our data. Once accomplished, you are likely to see relationships and possibly forecasts...