Search code examples
rcolorsboxplot

Coloring the dots in a boxplot according to a variable not included in the boxplot in R


I'm trying to add color to the dots of a boxplot in R based on a variable not included in the boxplot. The other variable is a continuous variable, but it could be grouped as a character or factor variable if there is not a solution for this as a continuous variable. Using ggplot2. First time on here, apologies if there's any issues with this post.

Unsure how to even go about this, still very new to R. Made this example using mtcars dataset, where in this example I would want to add color depending on the disp variable.

    library(ggplot2); library(dplyr)
    mtcarscol <- mutate(mtcars, gear.factor = as.factor(gear))
    ggplot(mtcarscol, aes(x = gear.factor, y = mpg)) +
      geom_boxplot() +
      geom_dotplot(binaxis = 'y', stackdir = 'center', dotsize=.75)

Solution

  • For the fill color of the dots, use fill inside aes() for the dot layer:

    ggplot(mtcars, aes(x = factor(gear), y = mpg)) +
       geom_boxplot() +
      geom_dotplot(aes(fill = factor(am)), binaxis = 'y', stackdir = 'center', dotsize=.75)
    

    enter image description here

    I believe with geom_dotplot you can only use discrete fill colors, not continuous, so I used am to demonstrate. But if you create a binned disp column that is a factor you could use that.