I am trying to create a simple scatterplot with a red y=x+0 line using geom_abline(). I am also adding two blue lines that surround the red y=x+0 line. These two blue lines have the formula y=x-ciVal and y=x+ciVal, where ciVal is defined by the user. Any dots in the scatterplot that fall between these two blue lines are removed. I have this successfully working with an example below:
library(ggplot2)
set.seed(1)
x = runif(20,0,3)
y = runif(20,0,2)
ciVal <- 0.5
myMax = max(c(x,y))
myMin = min(c(x,y))
keep <- sign(resid(lm(y-x-ciVal ~ 0)))==1 | sign(resid(lm(y-x+ciVal ~ 0)))==-1
data <- data.frame(dp = paste0("DataPoint",1:sum(keep==TRUE)), x = x[keep], y = y[keep])
data$dp <- as.character(data$dp)
ggplot(data = data, aes(x=x,y=y)) + geom_point(size=0.5) + geom_abline(intercept = 0, color = "red", size = 0.25) + geom_abline(intercept = ciVal, color ="blue", size = 0.25) + geom_abline(intercept = -1*ciVal, color ="blue", size = 0.25) + scale_x_continuous(limits = c(myMin, myMax)) +
scale_y_continuous(limits = c(myMin, myMax))
What I am trying to do now is increase the aesthetics of the plot. Namely, instead of having two blue lines surrounding the red line, I just want some type of shading between the two blue lines. Whether or not the two blue lines themselves remain as the border of the shading can be either way, but I prefer for them to be removed.
I am not sure if I could do this by just making the one red geom_abline() and simply adding a constant shade to both sides of it, or if I need to shade between the two blue geom_ablines().
I have seen similar questions (r - ggplot2 - create a shaded region between two geom_abline layers and shading area between two lines in r).
However, I do not know if they necessarily apply in this situation. It is difficult for me to create a polygon for this case because the shaded region leaves the axes of the plot and it is difficult for me to determine the corners of the polygon as the ciVal value can change as can the (maximum and minimum value of the) data which create the limits of the axes.
Any suggestions would be much appreciated.