How to automatically insert linear regression equation in graph in RSTUDIO?

How to automatically insert linear regression equation in graph in RSTUDIO?


Sometimes, we need to insert a linear regression equation inside a graph, but it’s an annoying to type an equation every time when generating a linear regression graph. Using stat_poly_eq(), we can automatically insert a linear regression equation.

Let’s generate one data frame.

x<- c(1, 2, 3, 4, 5, 6, 7)
y<- c(10, 13, 15, 14, 16, 19, 20)
dataA<- data.frame (x, y)

Then, I’ll generate a regression graph.

install.packages("ggplot2")
library(ggplot2)
ggplot(data=dataA, aes(x=x, y=y)) +
geom_point(size=1.5, stroke=1) +
geom_smooth(method=lm, level=0.95, se=TRUE, linetype=1, size=0.5, formula=y~x) +
scale_x_continuous(breaks=seq(0,10,2), limits=c(0,10)) +
scale_y_continuous(breaks=seq(0,20,5), limits=c(0,20)) +
labs(x="Weight", y="Height") +
theme(axis.title.x=element_text (family="serif", face="plain", size=25, color="black", margin=margin(t=5, r=0, b=0, l=0)),
axis.title.y=element_text (family="serif", face="plain", size=25, color="black", margin=margin(t=0, r=0, b=0, l=0)),
axis.text.x=element_text(family="serif", size=22, margin=margin(t=0,r=0,b=0,l=0)),
axis.text.y=element_text(family="serif", size=22, margin=margin(t=0,r=5,b=0,l=0)),
axis.line= element_line(size=0.5, colour="black"),
legend.position='none',
legend.key= element_rect(color="white", fill="white"),
legend.key.size=unit(0.5,"cm"),
legend.title=element_text(family="serif", face="plain", size=18, color="Black"),
legend.text=element_text(family="serif", face="plain", size=18, color="Black"),
plot.margin=unit(c(0,0,0,0),"cm"))+
windows(width=6, height=5.5)

Now let’s analyze a linear regression.

Regression <- lm (y ~ x, data=dataA)
summary (Regression) 

The linear model equation is y= 9.1429 + 1.5357x and R2 is 0.9245. Now I’ll insert this equation model automatically using stat_poly_eq(). I’ll add the below codes.

library(ggpmisc)

ggplot(data=dataA, aes(x=x, y=y)) +
geom_point(size=1.5, stroke = 1) +
geom_smooth(method=lm, level=0.95, se=TRUE, linetype=1, size=0.5, formula=y~x) +
#Equation
 stat_poly_eq(aes(label= paste(..eq.label.., sep= "~~~")),
              label.x.npc=0.9, label.y.npc=0.7,
              eq.with.lhs= "italic(hat(y))~'='~", eq.x.rhs= "~italic(x)",
              coef.digits=3, formula=y ~ x, parse=TRUE, size=5)+
# R-squared
 stat_poly_eq(aes(label=paste(..rr.label.., sep= "~~~")),
              label.x.npc=0.9, label.y.npc=0.6, rr.digits=3,
              formula=y ~ x, parse=TRUE, size=5)+
scale_x_continuous(breaks=seq(0,10,2), limits=c(0,10)) +
scale_y_continuous(breaks=seq(0,20,5), limits=c(0,20)) +
labs(x="Weight", y="Height") +
theme(axis.title.x=element_text (family="serif", face="plain", size=25, color="black", margin=margin(t=5, r=0, b=0, l=0)),
axis.title.y=element_text (family="serif", face="plain", size=25, color="black", margin=margin(t=0, r=0, b=0, l=0)),
axis.text.x=element_text(family="serif", size=22, margin=margin(t=0,r=0,b=0,l=0)),
axis.text.y=element_text(family="serif", size=22, margin=margin(t=0,r=5,b=0,l=0)),
axis.line= element_line(size=0.5, colour="black"),
legend.position='none',
legend.key= element_rect(color="white", fill="white"),
legend.key.size=unit(0.5,"cm"),
legend.title=element_text(family="serif", face="plain", size=18, color="Black"),
legend.text=element_text(family="serif", face="plain", size=18, color="Black"),
plot.margin=unit(c(0,0,0,0),"cm"))+
windows(width=6, height=5.5)

Then, the equation is inserted automatically inside the graph.

<code summary> Github


Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.