I was wondering if there is a “direct” way to associate the slope of the regression line in the ggplot facet panel with the background color of this panel (ie visually separate the positive slopes from the negative slopes in a large grid).
I understand how to add a regression line to GGplots - as was well explained in Adding a regression line to facet_grid with qplot in R
I also understand how to change the background if you previously added this information to the original framework - how is it explained to conditionally change the background of the panel with facet_grid?
However - is there a way to do this “in the geom_rect formula” without the need, for example, to start the regression separately, bind them to the original data framework, and then use this as a variable for geom_rect ()? is there any way for geom_rect () to use the information from stat_smooth ()?
Wouter
a good example of a simple regression line from a previous question:
library(ggplot2) x <- rnorm(100) y <- + .7*x + rnorm(100) f1 <- as.factor(c(rep("A",50),rep("B",50))) f2 <- as.factor(rep(c(rep("C",25),rep("D",25)),2)) df <- data.frame(cbind(x,y)) df$f1 <- f1 df$f2 <- f2 ggplot(df,aes(x=x,y=y))+geom_point()+facet_grid(f1~f2)+stat_smooth(method="lm",se=FALSE)
source share