使用ggplot2在R中进行函数拟合

4

我有如下示例:

seed(123)
x<-runif(100)
y<-runif(100)

f <- function(x) { return(4 * x * (1 - x)) }

我希望展示一个假设的依赖关系,其中包括函数f和数据点xy,如下图所示。

enter image description here

有人能使用ggplot2帮忙吗?

谢谢。

1个回答

1
使用stat_function()
df <- data.frame(x= runif(100), y = runif(100))
f <- function(x) { return(4 * x * (1 - x)) }

ggplot(aes(x,y), data = df) + geom_point() +
stat_function(fun=f)

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