2

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"))

Figura Script

divibisan
  • 11,659
  • 11
  • 40
  • 58
  • It's not clear what you wanted. Can you draw the desired output in Paint or MS Word or any software you have? – Tung Aug 15 '18 at 01:30
  • These might help https://stackoverflow.com/a/50827425/ & https://stackoverflow.com/a/49377292/ – Tung Aug 15 '18 at 01:31
  • It is interesting... I used `scale_color_manual` and saw that 9 values are being requested. I replotted the graph multiple times, each time supplying `#000000` to a different position, while the rest were `#ffffff`, and observed that the order goes this way: `subgroup2==A` > `subgroup2==B` > `geom_hline #dashed` > `subgroup2==C` > `subgroup2==D` > `geom_hline #solid` > `subgroup==m1` > `subgroup==m2` > `geom_hline #dotted`. More intriguing is that the order that the layers are added seems to have no effect on the above order... – Matthew Hui Aug 15 '18 at 07:53

0 Answers0