Plotting Odds Ratios (aka a forrestplot) with ggplot2 -

Reading time ~1 minute

Hi,

if you like me work in medical research, you have to plot the results of multiple logistic regressions every once in a while. As I have not yet found a great solution to make these plots I have put together the following short skript. Do not expect too much, it’s more of a reminder to my future self than some mind-boggling new invention. The code can be found below the resulting figure looks like this:

evolution

Here comes the code. It takes the model and optionally a title as an input and generates the above plot.

plot_odds<-function(x, title = NULL){
tmp<-data.frame(cbind(exp(coef(x)), exp(confint(x))))
odds<-tmp[-1,]
names(odds)<-c('OR', 'lower', 'upper')
odds$vars<-row.names(odds)
ticks<-c(seq(.1, 1, by =.1), seq(0, 10, by =1), seq(10, 100, by =10))

ggplot(odds, aes(y= OR, x = reorder(vars, OR))) +
geom_point() +
geom_errorbar(aes(ymin=lower, ymax=upper), width=.2) +
scale_y_log10(breaks=ticks, labels = ticks) +
geom_hline(yintercept = 1, linetype=2) +
coord_flip() +
labs(title = title, x = 'Variables', y = 'OR') +
theme_bw()
}

P.s. I know about ggplots “annotation_logticks” but they messed up my graphics, also it is not very often that ORs span more than three orders of magnitude. If they do consider playing with ggplots function or update the line beginning with “ticks <- “ in the above example

Update 29-01-2013: I replaced the nasty “ as they resulted in some nasty copy-past errors…

Publication-lists-4-Your-Website

Alle Jahre wieder braucht man einen aktuellen Lebenslauf. Wenn man in der Wissenschaft arbeitet, besteht dieser zu 80% aus einer aktuelle...… Continue reading

Relaunch on Jekyll

Published on June 04, 2015