Search code examples
rggplot2colorsboxplotjitter

ggplot - geom_point: setting manual fill color for point jitter without changing boxplot fill color


I've been generating a grouped boxplot to show the data corresponding to two parameters (deltaF and deltaD) quantified for three 'samples' (aDM31_WT, bDM75_WT, cDM31_NS). for aDM31_WT and bDM75_WT, there's plenty of replicates, so I'm showing a boxplot with point jitter. For cDM31_NS, there's only 2 replicates, so I'm only showing the individual datapoints, and no boxplot.

enter image description here

I want the 4 jitter points for cDM31_NS to have a BLUE fill. whilst all other costum colors remains unchanged. I haven't been able to figure out how.

here's my code:

library(dplyr)
library(ggplot2)

test <- read.table("Fig3G.txt", header = T)
attach(test)
names(test)

graph2 <- ggplot(test, aes(x = parameter, y = value, fill = sample, color = sample)) +
  scale_color_manual(values = c("black", "black", "white", "black", "black", "white")) +
  stat_boxplot(geom = "errorbar", position = position_dodge(width = 0.75), width = 0.2, show.legend = FALSE) +
  scale_fill_manual(values = c("#FFFFFF", "#CC6633", "#FFFFFF", "#FFFFFF", "#CC6633", "#FFFFFF")) +
  scale_y_continuous(position = "right", limits = c(-160, 0)) +
  geom_boxplot(outlier.shape = NA, show.legend = TRUE) +
  scale_x_discrete(limits = c("deltaF", "deltaD")) +
  geom_point(color = "black", size = 0.9, alpha = 0.4, show.legend = FALSE, position = position_jitterdodge(jitter.width = 0.2)) +
  theme(legend.position = "none") +
  theme_linedraw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

graph2

I've tried setting a seperate scale_fill_manual code to te geom_point function, but it simply applies any change in color to the boxplot fills too.


Solution

  • I believe this could help: Allowing for different colors for boxplot and overlying points in ggplot2

    So the following should work in your case:

    library(ggnewscale)
    
    graph2 <- ggplot(test, aes(x = parameter, y = value, fill = sample, color = sample)) +
      scale_color_manual(values = c("black", "black", "white", "black", "black", "white")) +
      stat_boxplot(geom = "errorbar", position = position_dodge(width = 0.75), width = 0.2, show.legend = FALSE) +
      scale_fill_manual(values = c("#FFFFFF", "#CC6633", "#FFFFFF", "#FFFFFF", "#CC6633", "#FFFFFF")) +
      scale_y_continuous(position = "right", limits = c(-160, 0)) +
      geom_boxplot(outlier.shape = NA, show.legend = TRUE) +
      scale_x_discrete(limits = c("deltaF", "deltaD")) +
      ## Solution begins
      new_scale_color() +
      geom_point(aes(color = sample), size = 0.9, alpha = 0.4, show.legend = FALSE, position = position_jitterdodge(jitter.width = 0.2)) +
      scale_color_manual(values = c("black", "black", "blue", "black", "black", "blue"))
      ## Solution ends
      theme(legend.position = "none") +
      theme_linedraw() +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
    
    graph2