I want to set background colors in my ggplot to highlight ranges of data. Particularly, I want to highlight [-0.1,0.1]
with, say, green,[-0.25,-0.1)
and (0.1,0.25]
with orange. In other words, what I need are bars with some alpha-transparency whose y-limits are the plot's y range, and x-limits are set by me.
Ideally, I would want something that would not be sensitive to coord_cartesian(...)
(as setting vline(...,size = X)
would). Additionally, it would be nice to have something independent from any data, and based solely on plot coordinates. I tried geom_segment
, but I could not figure it our how to set a width that would work.
library(ggplot2)
x <- c(seq(-1, 1, by = .001))
y <- rnorm(length(x))
df <- as.data.frame(x=x,y=y)
ggplot(df,aes(x,y)) +
geom_point(aes(y*abs(x)),alpha=.2,size=5) +
theme_bw() +
coord_cartesian(xlim = c(-.5,.5),ylim=c(-1,1))