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