How to add p values calculated elsewhere to horizontal graphs using ggplot and ggpubr?

128 views Asked by At

I have been trying to create a horizontal forest plot to show odds ratios, and I cannot figure out how to add p value with brackets. Followed the tutorial by ggpubr but it comes up with an error. I believe the error relates to coord.flip but I can't figure out how to change it. Here is my data:

order<-data.frame(
  Order=factor(c(1:6),levels=c(6:1)),
  OR=c(1,0.92,0.84,0.83,0.81,0.82),
  CI_low=c(NA,0.87,0.79,0.77,0.74,0.74),
  CI_high=c(NA,0.99,0.90,0.89,0.88,0.90),
  p_between=c(NA,0.01550,0.00610,0.72685,0.61190,0.86702),
  p_between_sig=c(NA,"*","**","NS","NS","NS"),
  p_overall=1.662490e-09,
  p_overall_sig=c("****")
)

My p values are calculated elsewhere so I made stat.test according to ggpubr stat_pvalue_manual

stat.test<-data.frame(
                group1=c(1,2,3,4,5,1),
                 group2=c(2,3,4,5,6,6),
                 p=c(0.02,0.01,0.73,0.61,0.87,1.6e-09),
                 p.signif=c("*","**","NS","NS","NS","****"))

Forest Plot

library(ggplot2)
library(ggpubr)
fp <- order %>%
  ggplot(aes(y=Order, x=OR, label=Order)) +
  geom_point(size=4, shape=15) +
  theme_classic()+
  geom_errorbarh(aes(xmin=CI_low, xmax=CI_high), height=0)+
  geom_vline(xintercept=1, linetype='longdash') +
  coord_cartesian(xlim=c(.7, 1.25),ylim=c(1,7))+
  labs(x="Odds Ratio",y="Order")+
  annotate("text", x = .85, y = 7, label = "Lower Detection") +
  annotate("text", x = 1.1, y = 7, label = "Higher Detection")
fp

enter image description here

Then I get an error while trying to add P values

fp +
  stat_pvalue_manual(stat.test, label = "p.signif", tip.length=0.01,
                     y.position = c(1.05,1.05,1.05,1.05,1.05,1.1),
                     coord.flip=T)

Error: Discrete value supplied to continuous scale

My desired output is something like this. How do I fix my code? enter image description here

1

There are 1 answers

1
Allan Cameron On BEST ANSWER

I think you'll need to manually flip the axes:

order %>%
  ggplot(aes(y = OR, x = Order, label = Order)) +
  geom_point(size = 4, shape = 15) +
  theme_classic() +
  geom_errorbar(aes(ymin = CI_low, ymax = CI_high), width = 0) +
  geom_hline(yintercept = 1, linetype = 'longdash') +
  labs(y = "Odds Ratio", x = "Order") +
  ylim(c(0.7, 1.2)) +
  annotate("text", y = .85, x = 6.5, label = "Lower Detection") +
  annotate("text", y = 1.1, x = 6.5, label = "Higher Detection") +
  stat_pvalue_manual(stat.test, label = "p.signif", tip.length = 0.01,
                     y.position = c(1.05, 1.05, 1.05, 1.05, 1.05, 1.1),
                     coord_flip = TRUE, 
                     vjust = c(5, 5, 5, 5, 5, 21.5), hjust = -1) +
  coord_flip()

enter image description here