繁体   English   中英

R:使用我自己的公式将曲线添加到x,y散点图中

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

我想在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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM