This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Adding asterisks to a ggplot2 bar plot to show significance

Hi guys. I'm working with a data set and I've written a for loop that generates barplots for my input tRNA data. I've been able to generate my bar plots. However, I have a column with my P values and I want to annotate on my bar plots any significant log fold changes (Padj<=0.05) with an asterisk, and insignificant values (Padj>0.05) with NS. How do I do this and incorporate it into my for loop. Been stuck on this all weekend. THANKS IN ADVANCE!!!

*head(df) ...

  Isodecoder Anticodon Loci Fragment_type                  Label        LFC         Padj
1        Ala       AGC    1   wholecounts  Ala-AGC-1-wholecounts  3.6111408 8.890000e-10
2        Ala       AGC   10   wholecounts Ala-AGC-10-wholecounts  0.3910508 6.404589e-01
3        Ala       AGC   12   wholecounts Ala-AGC-12-wholecounts         NA           NA
4        Ala       AGC    2   wholecounts  Ala-AGC-2-wholecounts  0.2785932 4.741744e-01
5        Ala       AGC    3   wholecounts  Ala-AGC-3-wholecounts  1.3492899 3.269910e-04
6        Ala       AGC    4   wholecounts  Ala-AGC-4-wholecounts -0.4200119 1.475002e-01

*This is my current code:

    df <- read.delim("/mnt/data/wholecounts_barplot_input.txt", stringsAsFactors = FALSE)

library(ggplot2)


plot_list <- list()

for (iso in c("Ala", "Arg", "Asn", "Asp", "Cys", "Gln", "Glu", "Gly", "His", "Ile", "Leu", "Lys", "Met", "Phe", "Pro", "SeC", "Ser", "Thr", "Trp", "Tyr", "Val", "iMet")) {

  df2 <- subset(df, Isodecoder == iso) 


  temp_plot <- ggplot(data = df2, aes(x = Label, y = LFC, fill = Anticodon))+
                      geom_bar(stat = "identity")+
                      xlab("tRNA")+
                      ylab("Log2FC")+
                      ylim(c(-2, 4))+
                      theme_bw()+
                      theme(axis.text.x = element_text(size = 12, angle = 45, hjust = 1), plot.margin = margin(0.5,0.5,0.5,2, "cm"))+
                      ggtitle(iso, "Full Length tRNA LFCs")


  plot_list[[iso]] <- temp_plot


  pdf(paste(iso, "wholecounts_LFCs.pdf", sep = "_"), bg = "white", width = 15, height = 7)
  print(temp_plot)
  dev.off()
}
rna-seq ggplot2 r

1 answer

Annotate bars with geom_text:

ggplot(data = df1, aes(x = Label, y = LFC, 
                       fill = Anticodon,
                       label = ifelse(Padj < 0.05, "*", "NS")) # <--- See here 
       ) +
  geom_bar(stat = "identity") +
  geom_text(vjust = 0) + # <----------------------------------------- and here
  xlab("tRNA") +
  ylab("Log2FC") +
  ylim(c(-2, 4)) +
  theme_bw() +
  theme(axis.text.x = element_text(size = 12, angle = 45, hjust = 1), 
        plot.margin = margin(0.5, 0.5, 0.5, 2, "cm"))

Perfect! Thanks for your help!

Can you do this for boxplots also?

Log in to answer this question.