I am attempting to add a legend to my boxplot with this example data
BM math loginc
1 2 1.4523
0 3 2.3415
1 1 0.6524
1 3 2.4562
0 1 3.5231
0 2 2.4532
Essentially, I have two groups BM = 0 and BM = 1, 3 categories in each group (math=1, 2 or 3), and a value of loginc.
boxcolors=c('gray70','orange','red','gray70','orange','red')
bothboxplot=ggplot(both, aes(x=math,y=loginc))+
geom_boxplot(fill=boxcolors)+
stat_summary(fun.y=mean,color=line,geom = "point",shape=3,size=2)+
scale_x_discrete(name='Site Category')+
scale_y_continuous(name='Log(Incidence/100,000)')+
facet_grid(.~BM)
bothboxplot
This yeilds the following plot:
This plot is entirely correct except for the lack of a legend. I have played around with the placement of the aes() and it won't work. When aes() is placed within the ggplot() rather than the geom_plot(), my fill statement gives the error ("Error: Aesthetics must be either length 1 or the same as the data (187): fill".
Ideally the legend I would like would have names of the 1,2,3 math categories, their corresponding colors, and the (+) symbol in each box to be labelled "Mean".