1
votes

As a total newby in R (no coding experience whatsoever) I have the following problem. I am trying to create a grouped forest plot of odds ratio's (blobbogram). The horizontal axis should contain the OR. The vertical axis the variables. Each variable contains the OR (including lower and upper limit) for group A and group B. So, per variable shown on the vertical axis, 2 lines should be visible. This website and this website should give you kind of the idea of the grouping.

I found quite a nice format for forest plots from Mike Barnkob which I have been trying to adapt to my needs.

I have been scrambling through stackoverflow (e.g. here). With this method I have to split the dataframe, I suppose alternate ways may be possible as well.

Please find below the code, with some dummy dataframe for just one group.

df <- data.frame(Outcome=c("Outcome A", "Outcome B", "Outcome C", "Outcome D"),
   OR=c(1.50, 2.60, 1.70, 1.30),
   Lower=c(1.00, 0.98, 0.60, 1.20),
   Upper=c(2.00, 3.01, 1.80, 2.20)
                      )
if (!require('ggplot2')) install.packages('ggplot2'); library('ggplot2')  

Outcome_order <- c('Outcome C', 'Outcome A', 'Outcome B', 'Outcome D')

p <- ggplot(df, aes(x=factor (Outcome, level=Outcome_order), y=OR, ymin=Lower, ymax=Upper)) + 
  geom_linerange(size=5, colour="#a6d8f0") +
  geom_hline(aes(x=0, yintercept=1), lty=2) +
  geom_point(size=3, shape=21, fill="#008fd5", colour="white", stroke = 0.5) +
  scale_x_discrete(name="(Post)operative outcomes") +
  scale_y_continuous(name="Odds ratio", limits = c(0.5, 5)) +
  coord_flip() +
  theme_minimal()
p

p + annotate("text", x=4.1, y=2.25, label="*")

Does anyone know how to add a second group per variable, ideally with a legend above or aside? Thank you in advance!

1
you mean another group which is tested for outcome A-D, and you would like odds ratio plotted side by side? - StupidWolf
Yes, another group that has been tested for outcome A-D. I would like to get the odds ratios plotted pairwise. Either side by side or above and below by flipping the plot with coord_flip(). - Robert Fichtinger

1 Answers

3
votes

The trick is to combine them into a single data frame, and introduce another variable, "group" in my example below. When you make the plot, you need to specify postion=position_dodge() so that they would plot side by side. See if below is what you want.. I am really bad with colors so you might need to specify the colors for the other group

library('ggplot2') 

Outcome_order <- c('Outcome C', 'Outcome A', 'Outcome B', 'Outcome D')

#this is the first dataset you have
df1 <- data.frame(Outcome=c("Outcome A", "Outcome B", "Outcome C", "Outcome D"),
   OR=c(1.50, 2.60, 1.70, 1.30),
   Lower=c(1.00, 0.98, 0.60, 1.20),
   Upper=c(2.00, 3.01, 1.80, 2.20))
# add a group column
df1$group <- "X"
# create a second dataset, similar format to first
df2 <- df1
# different group
df2$group <- "Y"
# and we adjust the values a bit, so it will look different in the plot
df2[,c("OR","Lower","Upper")] <- df2[,c("OR","Lower","Upper")] +0.5

# combine the two datasets                      
df = rbind(df1,df2)
# you can do the factoring here
df$Outcome = factor (df$Outcome, level=Outcome_order)

#define colours for dots and bars
dotCOLS = c("#a6d8f0","#f9b282")
barCOLS = c("#008fd5","#de6b35")


p <- ggplot(df, aes(x=Outcome, y=OR, ymin=Lower, ymax=Upper,col=group,fill=group)) + 
#specify position here
  geom_linerange(size=5,position=position_dodge(width = 0.5)) +
  geom_hline(yintercept=1, lty=2) +
#specify position here too
  geom_point(size=3, shape=21, colour="white", stroke = 0.5,position=position_dodge(width = 0.5)) +
  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