The annotate function can do exactly as you indicate. The think to remember is that discrete scales have a continuous scale underneath and that the positions of discrete categories is match(x, sort(unique(x))
, so that you bar should start around 3.5 for the iris dataset.
library(ggplot2)
ggplot(iris, aes(Species, y = Petal.Length)) +
geom_violin() +
annotate(
xmin = 3.5, xmax = 3.75,
ymin = c(-Inf, 3, 6),
ymax = c(3, 6, Inf),
geom = "rect",
fill = c("dodgerblue", "tan", "red")
)

Created on 2021-07-16 by the reprex package (v1.0.0)