0

I have included reproducible code from the mtcars data set below:

library(ggplot2)    

ggplot(mtcars,aes(as.factor(vs),disp))+ 
  stat_boxplot(geom ='errorbar',position=position_dodge(width=.88),aes(fill=as.factor(am))) + 
  geom_boxplot(position=position_dodge(width=.88),aes(fill=as.factor(am)))+
  geom_dotplot(binaxis='y', stackdir='center',dotsize=0.25,position=position_dodge(width=.88),stackratio=1.1,aes(fill=as.factor(am),color=as.factor(am)))

I would like to make the dots from the dotplot grey/black, while having control over the fill colour of the boxplots. As you can see, I need the fill colour aesthetic for both the boxplots and dotplots in order to separate the three factors on the x axis by the necessary group. Additionally, the dotplot's color aesthetic maps to the outline of the dots, while the fill aesthetic maps to the inside of the dots, so I need to use both, since I would like one seamless point. Using the scale_fill_manual function changes the colour of both boxplots and dots, but I need to have control over the colour of both separately.

I have found this other question on stackoverflow but I'm struggling to apply it to my case.

How to scale the size of line and point separately in ggplot2

Gerard
  • 35
  • 9

1 Answers1

1

In geom_dotplot you could only map factor(am) to color and override the values at end using scale_color_manual

ggplot(mtcars, aes(as.factor(vs), disp)) +
  stat_boxplot(geom = 'errorbar',
               position = position_dodge(width = .88),
               aes(fill = as.factor(am))) +
  geom_boxplot(position = position_dodge(width = .88), aes(fill = as.factor(am))) +
  geom_dotplot(
    binaxis = 'y',
    stackdir = 'center',
    dotsize = .5, # changed dotsize to .5 to better see the points
    position = position_dodge(width = .88),
    stackratio = 1.1,
    aes(color = as.factor(am)),
    fill = "grey70", 
    show.legend = FALSE
  ) +
  scale_color_manual(values =  c("black", "black"))

enter image description here

markus
  • 25,843
  • 5
  • 39
  • 58
  • Yes! Thank you, this is a great work-around. Sad I didn't come up with it myself (I swear I tried everything else) – Gerard Nov 23 '18 at 12:16