Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Add a curve,with my own equation, to an x,y scatterplot

I want to add a curve with the following equation to an x,y scatterplot: y<-(105+0.043(x^2-54x)). Is it possible within the plot() function? Also, if I use qplot instead (ggplot2) is there a way to draw this curve?

like image 778
user4631839 Avatar asked Mar 10 '15 17:03

user4631839


1 Answers

# data for scatterplot
x = rnorm(10, sd = 10)
y = (105 + 0.043 * (x^2 - 54 * x)) + rnorm(10, sd = 5)

Base plotting

plot(x, y)
curve(105 + 0.043 * (x^2 - 54 * x), add = T)

For ggplot, we need a data.frame

dat = data.frame(x = x, y = y)

ggplot(dat, aes(x , y)) + 
    geom_point() +
    stat_function(fun = function(x) 105 + 0.043 * (x^2 - 54 * x))
like image 168
Gregor Thomas Avatar answered Nov 15 '22 14:11

Gregor Thomas