add pvalue and odd ratio on a barplot

Viewed 144

I Have a dataframe with values that I ploted on a barplot and I would like to add pvalue and odd ratio between the differents groups that I compare.

Here are my data and the barplot codes.

data=data.frame(Liste=c("List1", "List2", "List3", "List4"), 
                TPA=c(9,8,25,3), 
                TPB=c(7,18,14,12))

data=data %>% mutate (tot=TPA+TPB)

> data
  Liste TPA TPB tot
1 List1   9   7  16
2 List2   8  18  26
3 List3  25  14  39
4 List4   3  12  15

tab14=melt(as.data.table(data), id.vars = c("Liste", "tot") )
tab15= tab14 %>% mutate(pct=value/tot*100)

ggplot(tab15, aes(x=Liste, y=pct, fill=variable)) + 
  geom_bar(stat = "identity",position="dodge")

enter image description here

I would like to add p value and odd ratio obtained in the res table Below is the expected result (values added by hand)

idx = t(combn(seq_along(data$Liste),2))


res = lapply(1:nrow(idx),function(i){
  test = fisher.test(data[idx[i,],c("TPA","TPB")])
  data.frame(
    group1 = data$Liste[idx[i,1]],
    group2 = data$Liste[idx[i,2]],
    odds_ratio = as.numeric(test$estimate),
    p = as.numeric(test$p.value)
  )
})

res = do.call(rbind,res)

> res
  group1 group2 odds_ratio           p
1  List1  List2  2.8162459 0.120542971
2  List1  List3  0.7244134 0.760816253
3  List1  List4  4.8573292 0.065892860
4  List2  List3  0.2546791 0.011637052
5  List2  List4  1.7538485 0.715806992
6  List3  List4  6.8725049 0.005673158

enter image description here

How can I procceed ?

1 Answers

Odds ratios are two by two comparaison of probabilities between group (list in your case) so it is hard to imagine a simple solution to display it on your one dimension barplot graph.

However, because you use combn, you could represent then in a matrix form like this. We use your code:

library(tidyverse)
library(reshape2)
library(data.table)
library(ggplot2)

Copy paste the code above.

ggplot(res, aes(x = group1, y = group2, fill = odds_ratio)) +
    geom_tile() +
    geom_text(aes(label = paste(round(p, 2))), col= "red", size = 5, show.legend = T) + 
    scale_fill_gradient(low = "gray", high = "black",
                        breaks=seq(0, 6)) +
    theme_classic() +
    xlab("") +
    ylab("") +
    guides(fill=guide_colourbar(title="Odds ratio value")) +
    ggtitle("p-values") +
    theme(plot.title = element_text(color = "red"))

odds ratio

Related