[R] geom_smooth

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sat Aug 12 07:11:58 CEST 2023


Às 05:17 de 12/08/2023, Thomas Subia via R-help escreveu:
> Colleagues,
> 
> Here is my reproducible code for a graph using geom_smooth
> set.seed(55)
> scatter_data <- tibble(x_var = runif(100, min = 0, max = 25)
>                         ,y_var = log2(x_var) + rnorm(100))
> 
> library(ggplot2)
> library(cowplot)
> 
> ggplot(scatter_data,aes(x=x_var,y=y_var))+
>    geom_point()+
>    geom_smooth(se=TRUE,fill="blue",color="black",linetype="dashed")+
>    theme_cowplot()
> 
> I'd like to add a black boundary around the shaded area. I suspect this can be done with geom_ribbon but I cannot figure this out. Some advice would be welcome.
> 
> Thanks!
> 
> Thomas Subia
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

Here is a solution. You ,ust access the computed variables, which you 
can with ?ggplot_build.
Then pass them in the data argument.



p <- ggplot(scatter_data,aes(x=x_var,y=y_var)) +
   geom_point()+
   geom_smooth(se=TRUE,fill="blue",color="black",linetype="dashed")+
   theme_cowplot()

# this is a data.frame, relevant columns are x,  ymin and ymax
fit <- ggplot_build(p)$data[[2]]

p +
   geom_line(data = fit, aes(x, ymin), linetype = "dashed", linewidth = 1) +
   geom_line(data = fit, aes(x, ymax), linetype = "dashed", linewidth = 1)


Hope this helps,

Rui Barradas



More information about the R-help mailing list