Home > Back-end >  How to add confidence intervals to geom_spline (ggformula package)?
How to add confidence intervals to geom_spline (ggformula package)?

Time:07-28

I have been looking through the documentation but I can not find a way to show confidence intervals,as we can with geom_smooth.

ggplot(data_long4,aes(y=Area,x=time,color=condition)) 
    geom_point() 
    geom_spline(aes(y=Area,x=time,
                    color=condition), 
                nknots = 5,df=3)

CodePudding user response:

ggformula::geom_spline is a line-only geom. It does not allow confidence intervals. If you want a spline fit to your data with a confidence interval, simply use geom_smooth with a b-spline inside an lm fit.

We don't have your data, so here is an example using the built-in iris data set:

ggplot(iris, aes(y = Petal.Length, x = Sepal.Length, color = Species))  
    geom_point()  
    geom_smooth(method = 'lm', formula = y ~ splines::bs(x, df = 3, knots = 5),
                aes(fill = after_scale(color)), alpha = 0.2)  
  scale_color_brewer(palette = 'Set1')  
  theme_minimal(base_size = 16)

enter image description here

CodePudding user response:

Even it is not what you requested a solution could be

ggplot(data_long4,aes(y=Area,x=time,color=condition)) 
geom_point() 
geom_spline(aes(y=Area,x=time,
                color=condition), 
            nknots = 5,df=3)
 geom_smooth(linetype=0)

If you need the actual solution then try add confidence interval to splines from quantile regression

CodePudding user response:

You could use the function gf_smooth from ggformula:

LOESS and linear model smoothers in ggformula.

Here reproducible example:

library(ggformula)
df <- data.frame(x = runif(10, 0, 1),
                 y = runif(10, 0, 1))
gf_smooth(df, y ~ x, se = TRUE)
#> `geom_smooth()` using method = 'loess'

Created on 2022-07-28 by the reprex package (v2.0.1)

  • Related