I have a line plot in ggplot2 that is faceted by a categorical variable, and within each faceted plot, the lines are colored by a different categorical variable. I fit a model based on the second categorical variable within the faceted plots. I then want the plots to display the slope + r^2 of the regression line for each.
The answer to this question works, but only on a non-faceted plot with a single regression line, and I have been unable to find how to accomplish this in my circumstances.
I created some sample data...
ex.ID <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8)
ex.month <- c(0,6,12,18,24,0,6,12,18,24,0,6,12,18,24,0,6,12,18,24,0,6,12,18,24,0,6,12,18,24,0,6,12,18,24,0,6,12,18,24)
ex.label <- c("A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","A","A","A","A","A")
ex.score <- c(100,100,89,85,70,95,90,90,86,80,84,80,71,68,60,100,100,98,90,92,100,95,90,85,80,98,96,90,83,81,85,80,78,72,69,95,90,89,85,80)
ex.status <- c("Y","Y","Y","Y","Y","Z","Z","Z","Z","Z","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","X","X","X","X","X","Z","Z","Z","Z","Z","Y","Y","Y","Y","Y","X","X","X","X","X")
ex.df <- data.frame(ex.ID,ex.month,ex.label,ex.score,ex.status)
I then plotted in the ggplot2 package.
ggplot(ex.df)+
geom_line(aes(x=ex.month,y=ex.score,group=ex.ID,color=ex.status)) +
facet_grid(.~ex.label) +
geom_smooth(aes(x=ex.month,y=ex.score,group=ex.status),method="lm")
I want to be able to display the slope and r^2 of each of these individual models on my graph. Is there some way to accomplish this on a faceted plot with multiple regression lines?