I would like to make a graph with 3 linear regression curves in different colors. For an example in the mtcars dataset, I want to plot hp
against disp
for each cyl
inder group (e.g. 4, 6, 8) and plot the linear regression curves in one plot, how would I do that?
Also if it's possible I want a label for the adj.r2
, slope
and p-value
for each linear regression in the top left corner.
Thanks!
Below is a graph I have made with the regression in three separate plots, which might help understand what I wish to do.
library(ggplot2)
library(ggpmisc)
formula <- y~x
ggplot(mtcars, aes(disp, hp)) +
geom_point() +
geom_smooth(method = "lm",formula = formula) +
theme_bw()+
facet_wrap(~cyl, scales = "free")+
stat_poly_eq(
aes(label = paste(stat(adj.rr.label), stat(p.value.label), sep = "*\", \"*")),
formula = formula, parse = TRUE,size=3)