We are trying to make a plot with ggplot2, where the positive areas above the x axis are one color and the negative areas are different.
Given this dataset, I would like the region graph to be shaded with different colors on each side of the axis.
I see a way to divide the data set into two subsets: one positive, where all negative values ββare zero, and one negative with all positive zero values, and then build them separately on the same axis, but it would seem to be more similar there on ggplot way.
The solution published in this question does not give exact results (see below).
Sample data that accurately displays as a bar graph

Generated by the following code:
# create some fake data with zero-crossings yvals=c(2,2,-1,2,2,2,0,-1,-2,2,-2) test = data.frame(x=seq(1,length(yvals)),y=yvals) # generate the bar plot ggplot(data=test,aes(x=x,y=y)) + geom_bar(data=test[test$y>0,],aes(y=y), fill="blue",stat="identity", width=.5) + geom_bar(data=test[test$y<0,],aes(y=y), fill="red",stat="identity", width=.5)
RLE approach is not common
The RLE approach proposed in another question creates artifacts associated with null intersections when applied to our dataset:

Generated by the following code ( do not use ):
# set up grouping function rle.grp <- function(x) { xx <- rle(x) xx$values = seq_along(xx$values) inverse.rle(xx) }
See the final answer below suggested by @baptiste and Kohske.