Here is an example of a forest plot containing combined results of risk ratios and -differences for a comparison of two exposure groups from a hypothetical study. Some familiarity with the forestploter package is assumed. The reader might want to look at the simple forest plot before continuing.
Data
The data contain results on number of individuals, events, along with risks for each exposure group and (displayed below) risk ratios and -differences with 95% confidence interval limits.
library(forestploter)library(grid)library(data.table)library(dplyr)results <-fread("./data/RatioAndDifferenceFPdata.csv")# empty spaces necessary for the forest functionresults$blank <-" "results$smallblank <-" "# overview of selected variablesprint(results[,c(1, 8:13)], digits =5)
level rr rr_lcl rr_ucl rd rd_lcl rd_ucl
<char> <num> <num> <num> <num> <num> <num>
1: Overall 0.68126 0.63348 0.73264 -2.5491968 -3.00210 -2.09630
2: Sex NA NA NA NA NA NA
3: Female 0.63379 0.56410 0.71210 -2.6392561 -3.26455 -2.01396
4: Male 0.71725 0.65356 0.78715 -2.4467261 -3.09398 -1.79947
5: Age NA NA NA NA NA NA
6: <65 years 0.99311 0.70261 1.40373 -0.0049484 -0.25244 0.24254
7: >65 years 0.66629 0.61934 0.71680 -4.3084973 -5.03174 -3.58526
8: Heart failure NA NA NA NA NA NA
9: No 0.64108 0.58706 0.70008 -2.3047929 -2.72852 -1.88107
10: Yes 0.82404 0.72938 0.93100 -3.8272787 -6.15827 -1.49629
11: Hypertension NA NA NA NA NA NA
12: No 0.57571 0.48934 0.67732 -2.6357262 -3.34096 -1.93049
13: Yes 0.71282 0.65721 0.77314 -2.5150210 -3.08566 -1.94439
Numbers of events, numbers at risk, and risk estimates are combined to one text variable. Text variables for risk ratios and -differences with 95% confidence intervals are also created. Some indentation is made for selected values of level.
A final touch of the data is to give the columns/variables names that will be reasonable for the forest plot
colnames(results) <-c("Group","Events / N (risk)","Events / N (risk)",colnames(results)[4:6], # these column names are kept as they are"",colnames(results)[8:10], # ... also these"","","")
The data are now ready for plotting.
First draft
In order to get forest to make several forest plots, it is necessary to provide a list of results (point estimates and confidence interval limits). It is furthermore necessary to provide two columns where forest plots should be inserted (ci_column takes a vector of length 2). Note that, generally there are two specifications for parameters other than data in the code snippet below. The risk ratio scale has been log-transformed while the risk difference scale is kept linear - mostly just show it is possible. Note that column 13 (the small blank) is included several times for padding, this might not be strictly necessary.