To benchmark different groups vs the whole population I'm trying to add the population box of the population boxplot to the plot. When the axis is numeric this is easily achieved by means of geom_area
and geom_hline
:
m <- median(mtcars$mpg)
Q1 <- quantile(as.numeric(mtcars$mpg), c(0.25))
Q3 <- quantile(as.numeric(mtcars$mpg), c(0.75))
ggplot(mtcars, aes(x=cyl, y=mpg))+
geom_rect(aes(xmin = -Inf , xmax = Inf , ymin = Q1 , ymax = Q3 ),fill = "blue", alpha = .002)+
geom_hline(yintercept= m,colour = "white", size=1) + scale_x_discrete(breaks=(factor(mtcars$cyl))) + geom_boxplot(aes(group=cyl))+ coord_flip(xlim=c(3,9)) +
geom_point() + theme_classic()
With a factor on the x-axis xmax=Inf
and xmin=-Inf
doens't work (which can be expected):
m <- median(esoph$ncases)
Q1 <- quantile(as.numeric(esoph$ncases), c(0.25))
Q3 <- quantile(as.numeric(esoph$ncases), c(0.75))
ggplot(esoph, aes(x=agegp, y=ncases))+
geom_rect(aes(xmin = -Inf , xmax = Inf , ymin = Q1 , ymax = Q3 ),fill = "blue", alpha = .002)+
geom_hline(yintercept= m,colour = "white", size=1) + geom_boxplot(aes(group=agegp))+ coord_flip() + geom_point() +
theme_classic()
This works but is not what I want. I want the area to cover the whole plot. Also the colour of the area is different compared to the colour on the first plot (probably related with the behaviour of alpha
)?
ggplot(esoph, aes(x=agegp, y=ncases))+
geom_rect(aes(xmin = min(agegp) , xmax = max(agegp) , ymin = Q1 , ymax = Q3 ),fill = "blue", alpha = .002)+
geom_hline(yintercept= m,colour = "white", size=1) + geom_boxplot(aes(group=agegp))+ coord_flip() + geom_point() +
theme_classic()
Solutions/suggestions?