ggplot2 – Stacking Regression Plots from two separate locations in one study on R

For separate regression lines on the same plot/facet you can do this:

ggplot(oceanregression, aes(x=log_phosphorus, y=chl.a_mg.m3, color=location))+
  geom_point(col="black", size=2.5)+
  xlab("log phosphorus nmol/L")+
  ylab("Concentration of Chl in mg/m3")+
  theme_classic()+
  geom_smooth(method=lm, formula=y~x)

stackocean

If you want to have split into facets, you can do this:

ggplot(oceanregression, aes(x=log_phosphorus, y=chl.a_mg.m3, color=location))+
  geom_point(col="black", size=2.5)+
  xlab("log phosphorus nmol/L")+
  ylab("Concentration of Chl in mg/m3")+
  theme_classic()+
  facet_wrap(~location)+
  geom_smooth(method=lm, formula=y~x)

facet_ocean

Input Data:

set.seed(123)
oceanregression = data.table(
  location=c(rep("A", 10), rep("B", 10)),
  log_phosphorus = rep(1:10,2),
  chl.a_mg.m3 = rnorm(20)
)

Read more here: Source link