rggplot2facet-wrapgeom-textannotate

Annotate text on individual facet multiple times


I came from this post answering on how to add text to text to the plot. However, my problem goes beyond because I need to add text multiple times in multiple locations. The annotate functions seems not to work, and I have tried the geom_text but somehow I am not capable of making it wokr

How can I do this, add multiple "+", "++" or "*"

enter image description here

I leave here my database:

p1 <- ggplot(data = df_plot2, aes( x = RQ_gapdh, y = gen, xmin = after_scale(1),
  fill = Color)) +
  geom_col(width = .5) +
    scale_fill_manual(values = c("indianred1", "olivedrab3" ))+
  labs(
    y = "Genes", x = expression(2^{-Delta*Delta*Ct}),
    title = "Gene expression",
    subtitles = "1 year post-intervention") +   
  guides(fill = "none") +
  facet_wrap(~grup_int,
    labeller = labeller(
      grup_int = c(
        "1" = "Olive oil",
        "2" = "Nuts",
        "3" = "Low-fat diet"
      )
    )
  )  + 
  theme(axis.text.y = element_text(face = "italic", family = "serif"))+
   geom_vline(xintercept = 1, linetype = "dashed", color = "black")+
     scale_x_continuous(limits = c(0.75, 1.25), breaks = c(0.75, 1, 1.25))


df_plot2<- structure(list(gen = structure(c(11L, 11L, 11L, 10L, 10L, 10L, 
9L, 9L, 9L, 8L, 8L, 8L, 7L, 7L, 7L, 6L, 6L, 6L, 5L, 5L, 5L, 4L, 
4L, 4L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L), levels = c("SCARB1", 
"RXRB", "RXRA", "PPARG", "PPARD", "PPARA", "NR1H3", "NR1H2", 
"CAV1", "ABCG1", "ABCA1"), class = "factor"), grup_int = structure(c(3L, 
2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L
), levels = c("1", "2", "3"), class = "factor"), time = c("3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3"), RQ_gapdh = c(1.019, 1.218, 1.183, 
0.926, 1.132, 1.052, 0.944, 0.9, 0.947, 0.936, 1.117, 0.961, 
0.987, 1.182, 0.971, 1.008, 0.985, 0.951, 0.935, 1.149, 1.058, 
0.914, 1.01, 0.952, 0.913, 1.134, 1.112, 0.985, 1.074, 1.135, 
1.019, 1.138, 0.989), Color = structure(c(2L, 2L, 2L, 1L, 2L, 
2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 
1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L), levels = c("FALSE", 
"TRUE"), class = "factor")), row.names = c(8L, 19L, 30L, 11L, 
22L, 33L, 10L, 21L, 32L, 5L, 16L, 27L, 4L, 15L, 26L, 1L, 12L, 
23L, 2L, 13L, 24L, 3L, 14L, 25L, 6L, 17L, 28L, 7L, 18L, 29L, 
9L, 20L, 31L), class = "data.frame")

Solution

  • Bascially what was stated in comments, add annotations to target gen and grup_int combinations so those can be used with geom_text(), first few as an example:

    library(ggplot2)
    library(dplyr)
    
    df_plot2 %>% 
      mutate(annotation = case_when(gen == "ABCA1" & grup_int == 1 ~ "++",
                                    gen == "PPARD" & grup_int == 1 ~ "+" ,
                                    gen == "ABCG1" & grup_int == 2 ~ "+++" ,
                                    gen == "NR1H2" & grup_int == 2 ~ "+" )) %>% 
    ggplot(aes( x = RQ_gapdh, y = gen, xmin = after_scale(1),
      fill = Color)) +
      geom_col(width = .5) +
        scale_fill_manual(values = c("indianred1", "olivedrab3" ))+
      geom_text(aes(label = annotation), nudge_x = .01, hjust = "left", fontface = "bold") +
      labs(
        y = "Genes", x = expression(2^{-Delta*Delta*Ct}),
        title = "Gene expression",
        subtitles = "1 year post-intervention") +   
      guides(fill = "none") +
      facet_wrap(~grup_int,
        labeller = labeller(
          grup_int = c(
            "1" = "Olive oil",
            "2" = "Nuts",
            "3" = "Low-fat diet"
          )
        )
      )  + 
      theme(axis.text.y = element_text(face = "italic", family = "serif"))+
       geom_vline(xintercept = 1, linetype = "dashed", color = "black")+
         scale_x_continuous(limits = c(0.75, 1.25), breaks = c(0.75, 1, 1.25))
    #> Warning: Removed 29 rows containing missing values (`geom_text()`).
    

    Created on 2023-11-20 with reprex v2.0.2