Search code examples
rggplot2smoothing

How to remove filled area on SE when plot "geom_smooth" in ggplot2?


I would like to plot a geom_smooth() in ggplot2 without filled se, but only the two edges of the se.

I have tried to use the code geom_smooth(method="loess", se=T, fill=NA), but it doesn't give what I expected.

    ggplot(mtcars, aes(x=wt, y=mpg)) +
      geom_point(size=2)+
      geom_smooth(method = "loess", se=T)

Something like the figure shows


Solution

  • A solution:

    gg <- ggplot(mpg, aes(displ, hwy)) +
      geom_point() +
      geom_smooth(fill = "transparent")
    
    ggg <- ggplot_build(gg)
    
    dat <- ggg$data[[2]]
    
    gg + geom_line(aes(x, ymin), data=dat, linetype="dashed") + 
      geom_line(aes(x, ymax), data=dat, linetype="dashed")
    

    enter image description here