Reputation: 3
I am trying to use ggplot
in order to overlay geom_rect
under some boxplots.
I want the grey rectangle will be behind the box plots but I can't do it for some reason.
This is the code that I'm using:
ggplot(data, aes(x = reorder(genotype, -Shann.div, FUN = median), y = Shann.div)) +
geom_jitter(color="black", size=0.3, alpha=0.9) + geom_boxplot(outlier.shape = NA, coef = 0) +
geom_hline(yintercept = avg, color="red") +
#geom_hline(yintercept = (avg + 2 * SE), linetype='dashed', color="black") +
#geom_hline(yintercept = (avg - 2 * SE), linetype='dashed', color="black") +
geom_rect(aes(xmin=0, xmax=Inf, ymin=avg - SD, ymax=avg + SD), fill="grey", alpha=0.01) +
theme(panel.background = element_blank()) +
theme(axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
ggtitle('Microbial UTO - Shannon diversity median') + xlab('genotype')
Upvotes: 0
Views: 1061
Reputation: 5747
The ggplot
package draws the geom
layers in the order that you declare them. If you want the geom_rect
layer in the back, put it before the other layers in the code:
ggplot(data, aes(x = reorder(genotype, -Shann.div, FUN = median), y = Shann.div)) +
geom_rect(aes(xmin=0, xmax=Inf, ymin=avg - SD, ymax=avg + SD), fill="grey", alpha=0.01) +
geom_jitter(color="black", size=0.3, alpha=0.9) + geom_boxplot(outlier.shape = NA, coef = 0) +
geom_hline(yintercept = avg, color="red") +
#geom_hline(yintercept = (avg + 2 * SE), linetype='dashed', color="black") +
#geom_hline(yintercept = (avg - 2 * SE), linetype='dashed', color="black") +
theme(panel.background = element_blank()) +
theme(axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
ggtitle('Microbial UTO - Shannon diversity median') + xlab('genotype')
Upvotes: 2