I am trying to make group boxplots displayed in a grid layout and I'd like a common legend for each group displayed on the side. I already did this for one subset of the data, here's an example of the code
group.colors=c("V"= "#F8766D", "W"= "#619CFF", "X" ="#00BA38", "Y"="#E69F00", "Z"="#999999")
g1=ggplot(data=df[df$analyte=="BP3",], aes(x=analyte, y=concentration, fill=Lab_ID))+geom_boxplot(outlier.shape = NA)+geom_point(aes(group=Lab_ID),position=position_dodge(width=0.75), size=1.5, alpha=.9)+theme_bw()+theme(legend.position = "none")
g1=g1+scale_fill_manual(values=group.colors)+ylab("concentration (ng/mL)")+xlab("")
g2=ggplot(data=df[df$analyte=="BPA",], aes(x=analyte, y=concentration, fill=Lab_ID))+geom_boxplot(outlier.shape = NA)+geom_point(aes(group=Lab_ID),position=position_dodge(width=0.75), size=1.5, alpha=.9)+theme_bw()+theme(legend.position = "none")
g2=g2+scale_fill_manual(values=group.colors)+ylab("concentration (ng/mL)")+xlab("")
f=ggarrange(g1,g2,nrow=1,ncol=2, common.legend=TRUE, legend="right")
annotate_figure(f,top= text_grob("Title", face = "bold", size = 16))
For some reason when I use the same code on a different set of analytes,the legend is not listing all of the groups even though the groups are in the boxplots
I tried including limits in scale_fill_manual but it did nothing