0

I wonder how to get statistical data of a ggplot with geom_smooth. I tried with stat_cor but I am not sure if its the right values for the line from geom_smooth and I don't know how to add r-squared. In the best case I would like to have summary function like in a normal regression just for geom-smooth. I don't know if this is possible. Thanks for your help. Code and Graph is below.

GAMFORMULA<-y~s(x,k=3,fx=F)
  #BHD~Höhe

g<- ggplot(data = data_clean) +
  geom_point(mapping = aes(x = BHD, y = Höhe, color = Soziale.Stellung)) +  
  geom_smooth(method = "gam", mapping = aes(x = BHD, y = Höhe), 
              color = "red", formula = GAMFORMULA) +
  xlab("BHD in cm") + 
  ylab("Höhe in m") 

g + stat_cor(method = "pearson",mapping = aes(x = BHD, y = Höhe),
             formula = GAMFORMULA) 


                    
               

Graph

Paul
  • 1
  • 1
  • 1
    each method will have their own ways to retrieve the stats data. stat_smooth uses mgcv::gam for `method = "gam"`, (see also `?geom_smooth`), so you can and should check mgcv's documentation how to retrieve those statistics. https://www.rdocumentation.org/packages/mgcv/versions/1.8-38/topics/gam – tjebo Jan 22 '22 at 17:24
  • The general method would be to generate this data outside of `ggplot()` and then display on the chart by using one of the `annotate()` functions. As mentioned, the way to grab the data will depend on the method used in `geom_smooth()`. – chemdork123 Jan 22 '22 at 18:56

0 Answers0