How to create odds and 95% CI-graph in R

I have estimates of the odds ratio with the corresponding 95% CI of six pollutants in excess of 4 lag periods. How to create a vertical graph similar to the attached figure in R? The figure below was created in SPSS. An example of the data that led to the figure is as follows:

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

+4
source share
2 answers

Assuming your data is in datf ...

First, I would choose exactly what you need in order to make the right choice.

 datf <- datf[order(datf$pollut, datf$lag), ] 

You need space before and after each lab grouping, so I would add extra lines that have NA. This makes the job easier, because then you will automatically have gaps in your conversations.

 datfPlusNA <- lapply(split(datf, datf$pollut), function(x) rbind(NA, x, NA)) datf <- do.call(rbind, datfPlusNA) 

Now that you have the data.frame sorted and with additional NAs, drawing is simple.

 nr <- nrow(datf) # find out how many rows all together with(datf, {# this allows entering your commands more succinctly # first you could set up the plot so you can select the order of drawing plot(1:nr, or, ylim = c(0.8, 1.3), type = 'n', xaxt = 'n', xlab = '', ylab = 'Odds Ratio and 95% CI', frame.plot = TRUE, panel.first = grid(nx = NA, ny = NULL)) # arrows(1:nr, lcl, 1:nr, ucl, length = 0.02, angle = 90, code = 3, col = factor(lag)) # you could use arrows above but you don't want ends so segments is easier segments(1:nr, lcl, 1:nr, ucl, col = factor(lag)) # add your points points(1:nr, or, pch = 19, cex = 0.6) xLabels <- na.omit(unique(pollut)) axis(1, seq(4, 34, by = 6) - 0.5, xLabels) }) abline(h = 1.0) 

There are packages that make this easier, but if you can do it, you can start making any graphics you can imagine.

enter image description here

+3
source

You can also do this with ggplot2. The code is slightly 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 the version closer to the SPSS score:

 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("") 
+9
source

Source: https://habr.com/ru/post/1446078/


All Articles