Search code examples
rggplot2r-forestplot

How to add OR and 95% CI as text into a forest plot?


I want to make a forest plot by subgroup, exactly as 'stupidWolf' suggests in his answer to this question in this link (if you scroll down to the last question, you would be able to see the forest plot by subgroups) Forest plot with subgroups in GGPlot2 But I want to add the OR and the 95% CI to that plot. Does anyone have a code that can add that information into in the forest plot?

I am able to make a forest plot by sub group in a normal ggplot. That is not a problem. But I can't figure out how to add the OR and 95% CI into the forest plot. should I use something else than ggplot? I hope someone can help! I have no clue where to begin with. Thanks in advance!


Solution

  • Using the data in the linked question, we can add the odds ratio and confidence intervals like this:

    ggplot(df, aes(x = Outcome, y = OR, ymin = Lower, ymax = Upper,
                   col = group, fill = group)) + 
      geom_linerange(linewidth = 5, position = position_dodge(width = 0.5)) +
      geom_hline(yintercept = 1, lty = 2) +
      geom_point(size = 3, shape = 21, colour = "white", stroke = 0.5,
                 position = position_dodge(width = 0.5)) +
      geom_text(aes(y = 3.75, group = group, 
                    label = paste0("OR ", round(OR, 2), ", (", round(Lower, 2), 
                                   " - ", round(Upper, 2), ")")), hjust = 0,
                position = position_dodge(width = 0.5), color = "black") +
      scale_fill_manual(values = barCOLS) +
      scale_color_manual(values = dotCOLS) +
      scale_x_discrete(name = "(Post)operative outcomes") +
      scale_y_continuous(name = "Odds ratio", limits = c(0.5, 5)) +
      coord_flip() +
      theme_minimal()
    

    enter image description here