如何获取ggplot2 :: geom_smooth曲线后面的数据?

时间:2017-10-09 11:34:19

标签: r ggplot2 gam

举个例子:

ggplot(iris,aes(Sepal.Width,Sepal.Length)) + geom_smooth()

如何获得用于绘制此蓝色曲线的xy

此案例将由loess自动计算,我的实际案例由gam自动计算。

我试过了:

  • 使用gam函数
  • 复制它
  • 探索情节对象

并没有任何成功

1 个答案:

答案 0 :(得分:1)

据我所知,请尝试这种方法:

 library(gam)
    library(broom)
    mod <- gam(Sepal.Length ~ Sepal.Width,  data = iris)
    ggplot(iris, aes(Sepal.Width,Sepal.Length)) + geom_point() + 
      geom_line(data = augment(mod, type.predict = "response"), 
                aes(y = .fitted), color = "blue")