Gordon Beattie
Gordon Beattie

Reputation: 135

Creating a grouped boxplot with separately coloured dots in ggplot

I am trying to make a grouped boxplot (by the "majorCluster" and "treatment" variables) with overlaid dots which are coloured by a different variable (the "aid" variable). All the approaches I've tried have changed the colouring of the dots, but the positions fail to match with the boxplot. Thanks in advance for any assistance!

Here is my code:

  ggplot(data = boxplot.data, aes(x=majorCluster, y=expa)) +
  geom_boxplot(aes(color = fct_rev(factor(treatment))), width = 0.5, size = 0.4, position = position_dodge(0.8)) +
  geom_dotplot(binaxis = "y", stackdir = "center", trim = FALSE, dotsize = 0.5,position = position_dodge(0.8), aes(color = fct_rev(factor(treatment)), fill = fct_rev(factor(treatment)))) +
  theme_classic()

Here is the output (desired output = dots have different colours based on "aid" variable) enter image description here

Here is the data (dput):

boxplot.data <- structure(list(aid = c("Anti-PD-L1 A", "Anti-PD-L1 A", "Anti-PD-L1 A", 
"Anti-PD-L1 A", "Anti-PD-L1 B", "Anti-PD-L1 B", "Anti-PD-L1 B", 
"Anti-PD-L1 B", "Anti-PD-L1 C", "Anti-PD-L1 C", "Anti-PD-L1 C", 
"Anti-PD-L1 C", "Anti-PD-L1 D", "Anti-PD-L1 D", "Anti-PD-L1 D", 
"Anti-PD-L1 D", "Untreated A", "Untreated A", "Untreated A", 
"Untreated A", "Untreated B", "Untreated B", "Untreated B", "Untreated B"
), majorCluster = c("1", "2", "3", "4", "1", "2", "3", "4", "1", 
"2", "3", "4", "1", "2", "3", "4", "1", "2", "3", "4", "1", "2", 
"3", "4"), expa = c(0.00229641856789997, 0.00449718430290869, 
0.0079476155988667, 0.0323137314979365, 0.125655399176487, 0.0737804421330638, 
0.0552815368690545, 0.0467782242054685, 0.00143345514424931, 
0.0055910433498606, 0.00647918094834399, 0.0150090372519559, 
0.0167864512842503, 0.0243240630288888, 0.0404105646363485, 0.0267541240801361, 
0.00925515666671728, 0.0115509845370231, 0.0276374627000041, 
0.0438400723313962, 0.0403818878060491, 0.0481121640688924, 0.0545189407651033, 
0.0764620370634215), migr = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), tran = c(0.335015348105744, 
0.376269513326694, 0.449228611310031, 0.446682573678422, 0.842635301541922, 
0.765588148505148, 0.683233418019828, 0.610651263240787, 0.18994141256059, 
0.232057278886713, 0.281840445619523, 0.351757256110902, 0.472126089570835, 
0.599354493054942, 0.707113638164233, 0.810238120454871, 0.483691176581404, 
0.609864164601998, 0.685779002499795, 0.716753100388738, 0.743285518118052, 
0.708765108329113, 0.74453010103349, 0.912753289862157), treatment = c("Atezo", 
"Atezo", "Atezo", "Atezo", "Atezo", "Atezo", "Atezo", "Atezo", 
"Atezo", "Atezo", "Atezo", "Atezo", "Atezo", "Atezo", "Atezo", 
"Atezo", "None", "None", "None", "None", "None", "None", "None", 
"None")), row.names = c("17", "21", "31", "41", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "111", "211", 
"311", "411", "51", "61", "71", "81"), class = "data.frame")

Upvotes: 1

Views: 863

Answers (1)

tjebo
tjebo

Reputation: 23767

Add group to your aes call in geom_point (which I use instead of geom_dot, because I guess this is what you want...)

Use a fillable shape for the use of fill, so you can use both color and fill aesthetic for different legends

library(tidyverse)

ggplot(data = boxplot.data, aes(x=majorCluster, y=expa)) +
  geom_boxplot(aes(color = fct_rev(factor(treatment))), width = 0.5, size = 0.4, position = position_dodge(0.8)) +
  geom_point(position = position_dodge(0.8), 
             aes(group = fct_rev(factor(treatment)), 
                 fill = aid), shape = 21) +
  theme_classic()

Created on 2020-06-09 by the reprex package (v0.3.0)

Upvotes: 1

Related Questions