I don't understand how ggplot the recycles color list or linetypes that are set by the user, when geom_boxplot, geom_text and geom_hline are used same time.
I want to set the fill color of my boxplots using a grouping factor with two levels (e.g.white and gray), set the text color of the labels using a grouping factor with four levels (e.g. red, orange, blue, green), and add three horizontal lines (two reference values and line with global mean value of the quantitative variable).
I've tried using scale_color_manual and scale_fill_manual but without satisfactory results: either I get the error: insufficient values in manual scale, the specified hlines didn't show up, or they looked different from what I wanted.
The following example script shows my problem: 1) the lines features are wrong (the mean should be solid and blue), 2) the border color of the boxplots is not black, and 3) the legend doesn't coincide with features displayed.
group<-c(rep("a",8),rep("b",8))
subgroup<-c(rep("m1",4),rep("m2",4),rep("m1",4),rep("m2",4))
subgroup2<-c(rep(c("A","B","C","D"),4))
id<-c(rep(c("q","w","e","r"),2),rep(c("t","y","u","i"),2))
value<-c(0.5,1.5,2.5,3.5,1.8,2.8,3.8,4.8,2.7,3.7,4.7,5.7,4.5,5.5,6.5,7.5)
df<-data.frame(group,subgroup,subgroup2,id,value)
ggplot(df,aes(group,value))+
geom_boxplot(aes(fill=subgroup, colour=subgroup))+
theme_light()+
theme(legend.position="bottom")+
geom_text(data=subset(df, subgroup=="m1"), aes(label=id, color=subgroup2), size=3, position = position_nudge(x = -0.19)) +
geom_text(data=subset(df, subgroup=="m2"), aes(label=id, color=subgroup2), size=3, position = position_nudge(x = 0.19)) +
stat_summary(data=subset(df, subgroup=="m1"),fun.y=mean, geom="point", shape=10, size=2,position = position_nudge(x = -0.19)) +
stat_summary(data=subset(df, subgroup=="m2"),fun.y=mean, geom="point", shape=10, size=2,position = position_nudge(x = 0.19)) +
geom_hline(aes(yintercept = 2.3, colour="green", linetype="dashed")) +
geom_hline(aes(yintercept = 4.7, colour="red", linetype="dotted")) +
geom_hline(aes(yintercept = mean(value), colour="blue", linetype="solid"))
