How to plot a polynomial over a scatterplot in R?

Viewed 33

My intention is to plot a polynomial regression on a data

Lets say the x-axis is the third column of df that is df[,3] and the y-axis is fifth column df[,5]

I performed polynomial regression on this data and obtained a vector yreg which I want to plot over this scatter plot.

My question is how can we make it happen? All I have encountered so far are using built in regression models without explicitly defining the polynomial but in my case I have the regression polynomial in hand and I want to add it to the scatter plot of the x,y data

Here is how I plot the scatter plot using ggplot2

plt <- ggplot(g,aes(df[,3] , df[,5])) +
       geom_point(color = "#69b3a2",size=0.5) 

I tried the following:

plt <- ggplot(g,aes(df[,3] , df[,5])) +
       geom_point(color = "#69b3a2",size=0.5) +
       geom_smooth(formula = y~yreg,color="red")

But that did not work.

0 Answers
Related