我想在R中的散点图中添加非线性回归线。我尝试使用predict()
命令,但我无法使其适用于我的数据。
我的朋友在Stata做这件事,其中graph twoway qfit x y
产生了一条曲线,正如我在R中想要的那样。在R中是否有这样的功能?
答案 0 :(得分:0)
使用library(ggplot2)
您想在数据中使用geom_smooth
。我相信method = 'gam'
会给你一条曲线。所以如果你有以下内容:
ggplot(data = df) + geom_scatter(aes(x = xvar, y = yvar))
您只需添加
即可ggplot(data = df) + geom_scatter(aes(x = xvar, y = yvar)) +
geom_smooth(aes(x = xvar, y = yvar), method = 'gam')
有关详细信息,请参阅此处:http://ggplot2.tidyverse.org/reference/geom_smooth.html