在x,y散点图中添加一条曲线,使用自己的方程式。

7
我希望在x,y散点图中添加以下方程的曲线:y<-(105+0.043(x^2-54x))。是否可以使用plot()函数实现?另外,如果我使用qplot(ggplot2),有没有办法绘制这条曲线?

2
在基础绘图中使用 curve(),在 ggplot2 中使用 stat_function() - Gregor Thomas
1个回答

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

基础绘图

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

对于 ggplot,我们需要一个数据框(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))

2
您还可以使用 qplot(...) + stat_function(...) - Alex A.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接