Is there a way to facet_wrap()
by the slope of a geom_line
in R or ggplot, such that for each geom_line
, all regions of that line with positive slope are placed in one facet, all regions with negative slope are placed in another facet, and all regions with no slope are placed in a third facet?
In the following minimal example, for instance, the target would be to place the rising segments of lines a and b in the positive slope facet, the falling segments of lines a and b in the negative facet, and the segments of a and b with no slope in the no slope facet:
library(ggplot2)
a.df <- data.frame(matrix(c('A',0,2,'A',1,6,'A',2,6,'A',3,4,'B',0,5,'B',1,4,'B',2,4,'B',3,6),ncol=3,byrow=TRUE))
a.df$X2 <- as.numeric(as.character(a.df$X2))
a.df$X3 <- as.numeric(as.character(a.df$X3))
ggplot(a.df, aes(x=X2,y=X3,colour=X1)) +
geom_line()
I tried your response MrFlick, but it looks like its placing lines in the positive or negative slope category if their global slope is positive or negative. I'm wondering if it would be possible to place all of the subregions of each line that have positive or negative slope into different facets. Is there a way to modify your code to accomplish this task? I would be grateful for any help you can offer on this question.