Suppose that I have these data set:
x <- 1:100
y <- 2 + 70* x
z <- 2 + x^2
index <- c(rep(1,100),rep(2,100))
x <- c(x,x)
t<- c(y,z)
data <- data.frame(x,t,index)
data[,2]= data[,2] + rnorm(200,500,400)
ggplot(data, aes(x = x, y = t, colour = factor(index))) + geom_point() + stat_smooth(method = "lm", formula = y ~ x, se = FALSE)
The ggplot
function just fit a linear model that suits for y
. How can we add a quadratic model for z
to the above function in addition to the linear model.
I look for a better way than this post: ggplot2 - plot multiple models on the same plot