I am trying to use ggplot
to shade the region between the vertical line and the diagonal line as shown below. Is there a way to do this?
df <- data.frame(x=c(1,-1), y=c(1,-1))
ggplot(df, aes(x, y))
geom_point()
geom_abline(slope=-1)
geom_vline(xintercept = 0)
CodePudding user response:
It is a bit tricky, but if you know the coordinates of the area (Inf), you can use them to fill the shades using geom_polygon
like this:
library(ggplot2)
df <- data.frame(x=c(1,-1), y=c(1,-1))
upper_area <- data.frame(x=c(-Inf,0,0),y=c(Inf,Inf,0))
down_area <- data.frame(x=c(0,0,Inf),y=c(0,-Inf,-Inf))
ggplot(df, aes(x, y))
geom_point()
geom_abline(slope=-1)
geom_vline(xintercept = 0)
geom_polygon(aes(x=x, y=y), data=upper_area, fill="red")
geom_polygon(aes(x=x, y=y), data=down_area, fill="blue")