Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create odds ratio and 95 % CI plot in R

Tags:

plot

r

I have estimates of odds ratio with corresponding 95% CI of six pollutants overs 4 lag periods. How can I create a vertical plot similar to the attached figure in R? The figure below was created in SPSS. Sample data that produced the figure is the following:

lag pollut  or  lcl ucl
0   CO  0.97    0.90    1.06
0   PM10    1.00    0.91    1.09
0   NO  0.97    0.92    1.02
0   NO2 1.01    0.89    1.15
0   SO2 0.97    0.85    1.11
0   Ozone   1.00    0.87    1.15
1   CO  1.03    0.95    1.10
1   PM10    0.93    0.86    1.01
1   NO  1.01    0.97    1.06
1   NO2 1.08    0.97    1.20
1   SO2 0.94    0.84    1.04
1   Ozone   0.94    0.84    1.04
2   CO  1.09    1.02    1.16
2   PM10    1.04    0.96    1.13
2   NO  1.04    1.00    1.08
2   NO2 1.07    0.96    1.18
2   SO2 1.05    0.95    1.17
2   Ozone   0.93    0.84    1.03
3   CO  0.98    0.91    1.06
3   PM10    1.14    1.05    1.24
3   NO  0.99    0.95    1.04
3   NO2 1.01    0.91    1.12
3   SO2 1.11    1.00    1.23
3   Ozone   1.00    0.90    1.11

Odds ratio and 95 % CI plot created in SPSS

like image 760
Meso Avatar asked Dec 15 '22 17:12

Meso


1 Answers

You can also do this with ggplot2. The code is somewhat shorter:

 dat <- read.table("clipboard", header = T)
 dat$lag <- paste0("L", dat$lag)

 library(ggplot2)

 ggplot(dat, aes(x = pollut, y = or, ymin = lcl, ymax = ucl)) + geom_pointrange(aes(col = factor(lag)), position=position_dodge(width=0.30)) + 
 ylab("Odds ratio & 95% CI") + geom_hline(aes(yintercept = 1)) + scale_color_discrete(name = "Lag") + xlab("")

enter image description here

EDIT: Here is a version is closer to the SPSS figure:

ggplot(dat, aes(x = pollut, y = or, ymin = lcl, ymax = ucl)) + geom_linerange(aes(col = factor(lag)), position=position_dodge(width=0.30)) +
geom_point(aes(shape = factor(lag)), position=position_dodge(width=0.30)) + ylab("Odds ratio & 95% CI") + geom_hline(aes(yintercept = 1)) + xlab("")
like image 172
Mikko Avatar answered Dec 31 '22 04:12

Mikko