Search code examples
rggplot2plotboxplotjitter

How do I colour jitter points to be different colours in a geom_boxjitter plot?


I'm trying to match the colours of the points from the jitter plot to the colours of the boxes. I can colour them one colour or the other, but I can't seem to set them to match the colour of the boxes.

Alternatively, I've figured out how to change the colours of the jitter points using geom_jitter but can't seem to figure out how to shift them to the side of the boxplot like the geom_boxjitter would have them. I thought maybe I could add "position = ..." to my geom_jitter() but that didn't seem to work...

Jitter box plot with dots in black Jitter box plot with dots in black

Jitter box plot with correct colour dots, but overlapping with boxes JItter box plot with correct colour dots, but overlapping with boxes

#hybrid jitter-box with jitter points all same colour
ggplot(all.bio2)+
  geom_boxjitter(aes(x=season, y=S.chao1, fill=season),
                                jitter.colour = ,
                                outlier.colour = NULL, outlier.shape = 1,
                                errorbar.draw = T, 
                                errorbar.length = 0.2)+
  theme(panel.background = element_rect(fill = 'white', colour = 'black'))

#hybrid jitter-box with different coloured points but overlapping with boxes
ggplot(all.bio2)+
  geom_boxjitter(aes(x=season, y=S.chao1, fill=season),
                                jitter.colour = NA,
                                outlier.colour = NA, outlier.shape = 1,
                                errorbar.draw = T, 
                                errorbar.length = 0.2)+
  geom_jitter(aes(x=season, y=S.chao1, colour=season), width = 0.15)+
  theme(panel.background = element_rect(fill = 'white', colour = 'black'))

Solution

  • Based on this post How to plot a hybrid boxplot: half boxplot with jitter points on the other half?, you have to use jitter.color = NA and jitter.shape = 21 in order to have the same color between the boxplot and jitter points

    So, for your code, you should try:

    library(ggplot2)
    library(ggpol)
    
    ggplot(all.bio2, aes(x = as.factor(season), y = S.chao1, fill= as.factor(season))) +
      geom_boxjitter(jitter.shape = 21, jitter.color = NA,
                     outlier.colour = NULL, outlier.shape = 1,
                     errorbar.draw = T,
                     errorbar.length = 0.2)+
      theme(panel.background = element_rect(fill = 'white', colour = 'black'))
    

    It works for me (using mtcars dataset)

    Example (using mtcars dataset)

    library(ggpol)
    library(ggplot2)
    df = mtcars[c(1:20),]
    ggplot(df, aes(x = as.factor(cyl), y = mpg, fill= as.factor(cyl))) +
      geom_boxjitter(jitter.shape = 21, jitter.color = NA,
                     outlier.colour = NULL, outlier.shape = 1,
                     errorbar.draw = T,
                     errorbar.length = 0.2)+
      theme(panel.background = element_rect(fill = 'white', colour = 'black'))
    

    enter image description here