Conditional panel background formatting in GGplot2

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) 
+4
source share
1 answer

This is not quite a solution, but a job. But it seems to have worked out well. Both of the pages you linked to had every part of the solution. James's solution here tells you how to extract set values ​​from stat_smooth . Joran's solution here tells how to use geom_rect to fill the background.

 # generating data: Usage of set.seed for reproducibility # also I changed the multiplication constant to 0.1 to have # at least one negative slope. require(ggplot2) set.seed(12) x <- rnorm(100) y <- + .1*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 # first generate your plot in this manner and run it # from James' post, the part outfit=fit<<-..y.. will store # the output of fitted values in "fit" g <- ggplot(df,aes(x=x,y=y)) + geom_point()+facet_grid(f1~f2) g <- g + stat_smooth(aes(outfit=fit<<-..y..), method="lm",se=FALSE) # now run g to generate "fit" g # now extract the slope for each facet and # construct the data.frame for geom_rect (as per Joran post) # Edit: Just to add more info about "fit". By default it contains # 80 values per facet. Hence the 80*4 = 320 slopes <- fit[seq(2, 320, by = 80)] - fit[seq(1, 320, by = 80)] tp <- unique(df[, c('f1', 'f2')]) tp <- transform(tp, slopes=slopes, x=1, y=1) tp$pos_neg <- ifelse(slopes > 0, 1, 0) tp$pos_neg <- factor(tp$pos_neg) # now plot again (but with geom_rect) g <- ggplot(df,aes(x=x,y=y)) g <- g + geom_rect(data = tp, aes(fill = pos_neg), xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.5) g <- g + geom_point() + facet_grid(f1~f2) + stat_smooth(method = "lm",se = FALSE) g 

Result looks like this . I'm not sure if this is what you expect. Strictly speaking, you calculate the set values ​​twice, but both times you calculate it implicitly with stat_smooth . As I said, this is just a job.

+4
source

All Articles