Search code examples
rggplot2tidyversefacet

Annotate ggplot2 across multiple facets


I have recently started using the facet_nested function from the ggh4x package and I really like the look of the nested axis. I would like to annotate the plot to show stats that I have run. I have created a dummy dataset to illustrate my problem.

library(tidyverse)
library(markdown)
library(ggtext)
library(ggh4x)


df <- data.frame(pretreatment = c("10NA", "10NA","10NA", "NT", "NT", "NT"),
                 timepoint = c("0 h", "6 h","6 h", "0 h", "6 h", "6 h"),
                 treatment = c("baseline", "10NA", "NT","baseline", "10NA", "NT"),
                 mean_copy_no = c(1000, 1500, 1200, 600, 700, 400), 
                 sample_id = c(1, 2, 3, 4, 5, 6))

df %>%
  ggplot(aes(x=sample_id, y = mean_copy_no, fill = treatment)) +
  geom_col(colour = "black") +
  facet_nested(.~ pretreatment + timepoint + treatment, scales = "free", nest_line = TRUE, switch = "x") +
  ylim(0,2000) +
  theme_bw() +
  theme(strip.text.x = element_text(size = unit(10, "pt")),
        legend.position = "none",
        axis.title.y = element_markdown(size = unit(13, "pt")),
        axis.text.y = element_text(size = 11),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank(),
        strip.text = element_markdown(size = unit(12, "pt")),
        strip.background = element_blank(),
        panel.spacing.x = unit(0.05,"line"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.border = element_blank())

This generates the following plot. plot1

Now my problem is that each of the bars is located within its own facet and not all on one x axis (if you run the code without the theme, it shows it more clearly).

I have drawn how I would like the plot to look. desired_plot

I would like to add lines and stars to indicate significant differences.

I can easilly add the stars, however I am struggling to add the lines and I understand that this may not even be possible because I am using facets to generate the plot. I just wanted to post the question and see if someone has any suggestion on how to do it in R. Or if there is a way to achieve the nested look without using facets.

*Edited for clarity.


Solution

  • One option is to use cowplot after making the ggplot object, where we can add the lines and text.

    library(ggplot2)
    library(cowplot)
    
    results <- df %>%
      ggplot(aes(x=sample_id, y = mean_copy_no, fill = treatment)) +
      geom_col(colour = "black") +
      facet_nested(.~ pretreatment + timepoint + treatment, scales = "free", nest_line = TRUE, switch = "x") +
      ylim(0,2000) +
      theme_bw() +
      theme(strip.text.x = element_text(size = unit(10, "pt")),
            legend.position = "none",
            axis.title.y = element_markdown(size = unit(13, "pt")),
            axis.text.y = element_text(size = 11),
            axis.text.x = element_blank(),
            axis.title.x = element_blank(),
            axis.ticks.x = element_blank(),
            strip.text = element_markdown(size = unit(12, "pt")),
            strip.background = element_blank(),
            panel.spacing.x = unit(0.05,"line"),
            panel.grid.major.x = element_blank(),
            panel.grid.minor.x = element_blank(),
            panel.border = element_blank())
    
    
    ggdraw(results) +
      draw_line(
        x = c(0.07, 0.36),
        y = c(0.84, 0.84),
        color = "black", size = 1
      ) +
      annotate("text", x = 0.215, y = 0.85, label = "*", size = 15) +
      draw_line(
        x = c(0.7, 0.98),
        y = c(0.55, 0.55),
        color = "black", size = 1
      ) +
      annotate("text", x = 0.84, y = 0.56, label = "**", size = 15)
    

    Output

    enter image description here