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!
