Home > other >  How to add the star(s) to my barplot (in ggplot) based on the values in the padj column?
How to add the star(s) to my barplot (in ggplot) based on the values in the padj column?

Time:08-27

I want to draw a barplot for the data below with the significance level in the padj column.

Here is df:

Genes   CancerType  log2FC  padj
Gene1   Breast  0.230749533 8.01E-07
Gene2   Breast  0.070357349 0.04819805
Gene3   Breast  -0.239713974    0.00059344
Gene4   Breast  0.703365372 5.18E-32
Gene5   Breast  0.327406476 1.01E-12
Gene1   Colon   0.332890058 7.21E-07
Gene2   Colon   0.09527495  0.227845727
Gene3   Colon   -0.178301154    0.196460613
Gene4   Colon   0.924092152 2.70E-16
Gene5   Colon   0.612677767 2.20E-12
Gene1   Lung    0.829893225 5.96E-24
Gene2   Lung    0.2606768   4.57E-05
Gene3   Lung    -0.1821214  0.053233199
Gene4   Lung    0.692721414 4.83E-15
Gene5   Lung    0.688720644 1.83E-27
Gene1   Stomach 0.494758767 1.54E-07
Gene2   Stomach 0.125857219 0.107850978
Gene3   Stomach -0.016071377    0.862073856
Gene4   Stomach 0.203326756 0.094677874
Gene5   Stomach 0.784281834 1.42E-19

And here is the script:

ggplot(data = df, aes(x = CancerType, y = log2FC , fill = Genes))  
  geom_bar(stat = "identity" , width = 0.7,show.legend = T)  theme_bw()   facet_wrap(~ Genes) 
  theme(axis.text.x = element_text(size =10, angle = 45, hjust = 1 , colour = "black"))

I don't know how to add the star(s) to each of my barplot based on the values in the padj column.

Thanks for any help.

CodePudding user response:

Here is an option.

You can first create two new columns, one (sig_label) containing the labels based on the padj column and another one, label_position, containing where the label will be place on the y-axis. You can control how far from the bars the labels will be placed by changing the value of nudge.

library(dplyr)
library(ggplot2)
nudge <- 0.075
df |> 
  mutate(
    sig_label = case_when(
      padj < 0.001 ~ "***",
      padj < 0.01 ~ "**",
      padj < 0.05 ~ "*",
      padj >= 0.05 ~ ""
    ),
    label_position = ifelse(log2FC > 0, log2FC   nudge, log2FC - nudge)
  ) |> 
ggplot(aes(x = CancerType, y = log2FC , fill = Genes))  
  geom_bar(stat = "identity" , width = 0.7,show.legend = T)  theme_bw()   facet_wrap(~ Genes) 
  theme(axis.text.x = element_text(size =10, angle = 45, hjust = 1 , colour = "black"))  
  geom_text(aes(label = sig_label, y = label_position))

output

  • Related