PesKchan
PesKchan

Reputation: 968

Adding p value on top of grouped bar plot

This is my data which I'm trying to plot

dput(results)
structure(list(ontology = c("CC", "BP", "MF", "CC", "BP", "MF", 
"CC", "BP", "MF"), breadth = structure(c(3L, 3L, 3L, 2L, 2L, 
2L, 1L, 1L, 1L), .Label = c("10", "30", "100"), class = "factor"), 
    enrichment = c(4.09685904270847, 8.04193317540539, 5.5801230522415, 
    4.52127958016442, 8.9221766387218, 5.68189764335457, 4.25046722366786, 
    9.49038239297713, 6.75423163834793), p = c(0, 0, 0, 0, 0, 
    0, 2.09057402562873e-221, 0, 0)), class = "data.frame", row.names = c(NA, 
-9L))

My code

results = read.delim("data/GO/LC-GO-enrichment_new.txt") %>%
  mutate(breadth = factor(breadth))
p = ggplot(results, aes(x = breadth, y = enrichment, fill = ontology,
                        color = ontology)) + 
  geom_col(position = 'dodge', width = 0.8) + 
  labs(x = "Breadth", y = "Odds ratio") + 
  scale_fill_manual(values = ryb8[c(1, 5, 8)], name = "Ontology") +
  scale_color_manual(values = darken(ryb8[c(1, 5, 8)], 1.3), 
                     name = "Ontology") +
  scale_y_log10(expand = c(0.01, 0)) +
  sci_theme
p

I get something like this my_output

is there a way the pvalue can be added similar to this

desired visualisation or its done post making the figure manually .

Any help or suggestion would be really helpfu;

Upvotes: 0

Views: 1086

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173803

You could simply add the p values as a text layer. Note though, that in your data, each bar has a p value, so it's not clear where the groupwise p values are coming from.

library(ggplot2)

ggplot(results, aes(x = breadth, y = enrichment, fill = ontology)) + 
  geom_col(position = 'dodge', width = 0.8, 
           aes(color = after_scale(colorspace::darken(fill, 1.3)))) + 
  geom_text(aes(label = paste("p", scales::pvalue(p)), group = ontology),
            vjust = -1, position = position_dodge(width = 0.8)) +
  labs(x = "Breadth", y = "Odds ratio", fill = "Ontology") + 
  scale_fill_manual(values = c("#d63228", "#dff2f8", "#4575b5")) +
  scale_y_log10(expand = c(0.05, 0)) +
  theme_classic(base_size = 16) +
  theme(legend.position = "top")

enter image description here

Upvotes: 1

Related Questions