1

take this example :

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

How do I get the x and y that were used to plot this blue curve ?

This case will be computed by loess automatically, my real case is computed by gam automatically.

I tried :

  • reproducing it with gam function
  • exploring the plot object

And didn't have success with any

moodymudskipper
  • 42,696
  • 10
  • 102
  • 146

1 Answers1

1

As far as I understood the question, try this approach:

 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")
Mateusz1981
  • 1,753
  • 16
  • 31