I am developing a shiny app, in which I am generating various scatter plots by uploading the datasets. I am also using stat_smooth
on the generated scatter plots. Now, I want to be able to store the generated stat_smooth
profile as a dataset. How can I do that?
Further, I want to compile various stat_smooth profiles in one graph, that's why I want to store those as a dataset.
You can use ggplot_build
. Here is an example:
library(ggplot2)
p <- ggplot(data = mtcars) +
geom_point(aes(hp, mpg)) +
stat_smooth(aes(hp, mpg))
p2 <- ggplot_build(p)
head(p2$data[[2]])
Output
x y ymin ymax se PANEL group colour fill
1 52.00000 31.15895 27.03176 35.28614 2.009302 1 -1 #3366FF grey60
2 55.58228 30.51224 26.91154 34.11295 1.752985 1 -1 #3366FF grey60
3 59.16456 29.87138 26.72390 33.01886 1.532336 1 -1 #3366FF grey60
4 62.74684 29.23738 26.46507 32.00968 1.349680 1 -1 #3366FF grey60
5 66.32911 28.61085 26.13527 31.08643 1.205223 1 -1 #3366FF grey60
6 69.91139 27.99200 25.73825 30.24575 1.097226 1 -1 #3366FF grey60
size linetype weight alpha
1 1 1 1 0.4
2 1 1 1 0.4
3 1 1 1 0.4
4 1 1 1 0.4
5 1 1 1 0.4
6 1 1 1 0.4