Adding group notations outside of the plots

Viewed 41

I'm trying to add notations for groups outside of ggplot like illustrated in the picture below. I have tried with facet_grid but problems arise when it breaks plots apart from each other so it will not be aligned with the plot that is coming next to it

Example of the desired output

Code to generate the plot:

df %>%
  pivot_longer(cols=biomarkers) %>%
  left_join(., gene_pct_df, by=c("name" = "biomarkers")) %>%
  mutate(bio_factor = as.numeric(factor(biomarker_groups))) %>% 
  mutate(color_scheme = case_when(
    value == "CC" & bio_factor == 1 ~ "a",
    value == "CC" & bio_factor == 2 ~ "b",
    value == "CC" & bio_factor == 3 ~ "c",
    TRUE ~ "d")) %>%
  ggplot(aes(y=reorder(name, bio_factor))) +
  geom_raster(aes(x=factor(patient), fill=color_scheme,
                  alpha=color_scheme),
              show.legend = F) +
  geom_text(aes(x=factor(patient), label=value), size=3,
            show.legend = F) +
  scale_fill_manual(values = c("#F8766D", "#00BA38" ,"#619CFF", "white"),
                    labels = c("a", "b", "c", "d")) +
  scale_alpha_manual(values = c(0.9, 0.9, 0.9, 0.01),
                     labels = c("a", "b", "c", "d")) +
  theme(axis.title.x=element_blank(),
        axis.ticks.y=element_blank(),
        axis.title.y=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks = element_blank(),
        legend.title = element_blank(),
        plot.margin = unit(c(1,1,1,1), "cm"))

Example data to test the function:

n_pat <- 15
patient <- 1:n_pat
treatment <- sample(c("Drug A", "Drug B"), n_pat, replace=TRUE)
change <- rnorm(n_pat, 0, 20)
biomarkers <- c("T790M","Ex19del","L959R","Ex20Ins","MET","ERBB2","EGFR",
                "EGFR2","PIK3CA","KRAS","CDKN2","RB1","ALK","KIT","MET2",
                "Other")
genes <- matrix(sample(x=c("CC", "AA", "AC"), replace=TRUE, size=n_pat * length(biomarkers)), nrow=n_pat, ncol=length(biomarkers))
biomarker_groups <- c(rep("Baseline", 4), rep("SCNA", 3), rep("SNV", 9))
df <- data.frame(patient, treatment, change, genes)
df <- data.frame(patient, treatment, change, genes)
colnames(df) <- c("patient", "treatment", "change", biomarkers)

genes_df <- df %>%
  select(biomarkers)

pcts <- colSums(genes_df == "CC") / length(df)

gene_pct_df <- data.frame(pcts, biomarker_groups, biomarkers)


1 Answers

You can specify facet_wrap with free_y for both scale and space, then adjust the panel spacing in theme():

df %>%
  pivot_longer(cols=biomarkers) %>%
  left_join(., gene_pct_df, by=c("name" = "biomarkers")) %>%
  mutate(bio_factor = as.numeric(factor(biomarker_groups))) %>% 
  mutate(color_scheme = case_when(
    value == "CC" & bio_factor == 1 ~ "a",
    value == "CC" & bio_factor == 2 ~ "b",
    value == "CC" & bio_factor == 3 ~ "c",
    TRUE ~ "d")) %>%
  ggplot(aes(y=reorder(name, bio_factor))) +
  geom_raster(aes(x=factor(patient), fill=color_scheme,
                  alpha=color_scheme),
              show.legend = F) +
  geom_text(aes(x=factor(patient), label=value), size=3,
            show.legend = F) +
  scale_fill_manual(values = c("#F8766D", "#00BA38" ,"#619CFF", "white"),
                    labels = c("a", "b", "c", "d")) +
  scale_alpha_manual(values = c(0.9, 0.9, 0.9, 0.01),
                     labels = c("a", "b", "c", "d")) +
  facet_grid(biomarker_groups ~.,
  switch="y",scales="free_y",space = "free_y") + 

  theme(axis.title.x=element_blank(),
        axis.ticks.y=element_blank(),
        axis.title.y=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks = element_blank(),
        legend.title = element_blank(),
        panel.spacing.y=unit(0,"lines"),
        strip.placement = "outside",
        plot.margin = unit(c(1,1,1,1), "cm")) 

enter image description here

Related