Remove ggplot2 layers after gghighlight layer

42 Views Asked by At

Pls how can i remove the grey jitter points in the highlighted plot (CondA and Control)?

set.seed(123)  # Setting a seed for consistent jittering
ggplot(data_in, aes(x = condition, y = count)) + 
  geom_boxplot(aes(group = condition), color = "black") + 
  geom_jitter(width = 0.2, aes(color = condition), size = 3) + 
  geom_label_repel(data=data_in %>% filter(!condition %in% c("Control", "CondA")), 
                   aes(label = Samples)) +

  gghighlight(condition %in% c("Control", "CondA"),
            label_key = Samples,
            use_group_by = FALSE) +   
  theme_minimal()

dataset to reproduce plots

data_in <- data.frame(
  Samples = c("Control-1", "Control-2", "Control-3", "A-1", "A-2", "A-3", 
              "C-1", "C-2", "C-3", "Q-1", "Q-2", "Q-3", 
              "QC-1", "QC-2"),
  count = c(16.367418, 13.025304, 9.541327, 64.119111, 55.478344, 51.955399, 
            19.188755, 8.646904, 8.564548, 13.460694, 18.926368, 13.219077, 
            17.307382, 11.966919),
  condition = c("Control", "Control", "Control", "CondA", "CondA", "CondA", "CondC", 
                "CondC", "CondC", "CondQ", "CondQ", "CondQ", "CondQC", "CondQC")
)
1

There are 1 best solutions below

1
On BEST ANSWER

You can pass list(colour = "#00000000") to the unhighlighted_params argument of gghighlight:

library(tidyverse)
library(ggrepel)
library(gghighlight)

set.seed(123) 

ggplot(data_in, aes(x = condition, y = count)) + 
  geom_boxplot(aes(group = condition), color = "black") + 
  geom_jitter(width = 0.2, aes(color = condition), size = 3) + 
  geom_label_repel(data = filter(data_in, !condition %in% c("Control", "CondA")), 
                   aes(label = Samples)) +
  gghighlight(condition %in% c("Control", "CondA"),
              label_key = Samples,
              unhighlighted_params = list(colour = "#00000000"),
              use_group_by = FALSE) +   
  theme_minimal()

Created on 2023-10-17 with reprex v2.0.2