Extract stat_smooth Regression Line Fit from ggplot2 Plot in R (Example) | ggplot_build() Function

preview_player
Показать описание
R code of this video:

x <- rnorm(100)
y <- x + rnorm(100)

library("ggplot2")

ggp <- ggplot(data, aes(x, y)) + # Draw plot with stat_smooth line
geom_point() +
stat_smooth()
ggp

ggp_data <- ggplot_build(ggp)$data[[2]] # Extract information about plot
head(ggp_data) # Head of information data

ggp_fit <- ggplot(ggp_data, aes(x, y)) + # Redraw stat_smooth line
geom_line()
ggp_fit

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

Hi Joachim, thanks for the video. How do I get the equation of the smoothed line? I need it to calculate changes in cinvexity...
Thanks for your feedback
Guido

guidob_
Автор

Joachim,
Neat ! I was unaware of the ggplot_build function. Do you know if there is a corresponding function in the tidyverse? The broom package perhas?

haraldurkarlsson
Автор

Great video! Is it possible to obtain the regression statistics and significance? Thanks in advance! ❤️🇲🇽❤️

WhySoBroke
welcome to shbcf.ru