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")
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
How can I procceed ?


