-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforest_plot.qmd
131 lines (96 loc) · 3.64 KB
/
forest_plot.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
---
title: "forest_plot"
author: "Kasandra Ascuña-Durand / Ariana Cardenas"
format: html
editor: visual
---
## Conjunto de datos
```{r}
forest <- readr::read_csv(
"data/processed/forest_study.csv")
```
## Librerías
```{r}
library(tidyverse)
library(ggplot2)
library(grid)
library(forestploter)
```
## Exploración
```{r}
colnames(forest)
```
## Paso 1: Ajustar el df para el forest plot
```{r}
forest$var <- ifelse(is.na(forest$IRR1),
forest$var,
paste0(" ", forest$var))
forest$model_type1 <- replace(as.character(forest$model_type1), is.na(forest$model_type1), "")
forest$pvalue1 <- replace(as.character(forest$pvalue1), is.na(forest$pvalue1), "")
forest$model_type2 <- replace(as.character(forest$model_type2), is.na(forest$model_type2), "")
forest$pvalue2 <- replace(as.character(forest$pvalue2), is.na(forest$pvalue2), "")
forest$`Adjusted model` <- paste(rep(" ", 20), collapse = " ")
forest$`Unadjusted model` <- paste(rep(" ", 20), collapse = " ")
# Create a confidence interval column to display
forest$`RR (95% CI)1` <- ifelse(is.na(forest$IRR1), "",
sprintf("%.2f (%.2f to %.2f)",
forest$IRR1, forest$lower1, forest$upper1))
forest$`RR (95% CI)2` <- ifelse(is.na(forest$IRR2), "",
sprintf("%.2f (%.2f to %.2f)",
forest$IRR2, forest$lower2, forest$upper2))
forest <- forest %>%
relocate(`Adjusted model`, .after = `upper1`) %>%
relocate(`RR (95% CI)1`, .after = `Adjusted model`) %>%
relocate(`pvalue2`, .after = `RR (95% CI)2`)
head(forest)
#Adjusting the names - skeleton for the forest plot
g <- forest[,c(1, 6:8, 13:15)]
colnames(g) <- c("Centralities",
" ","RR (95% CI)","p-value"," ",
"RR (95% CI)","p-value")
```
## Graficar el forest plot
```{r fig.align="center", echo = FALSE,fig.height = 12, fig.width = 14}
tm <- forest_theme(core=list(
#fg_params=list(hjust = 1, x = 0.9),
bg_params=list(fill = c("lightgray", "white", "white"))),
colhead=list(fg_params=list(hjust=0.5, x=0.5)))
study <- forestploter::forest(g,
est = list(forest$IRR1,
forest$IRR2),
lower = list(forest$lower1,
forest$lower2),
upper = list(forest$upper1,
forest$upper2),
ci_column = c(2, 5),
ref_line = c(1, 1),
xlim = list(c(-1, 5), c(-1, 5)),
theme = tm
)
# Bold grouping text
study <- forestploter::edit_plot(study,
row = c(1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40),
gp = grid::gpar(fontface = "bold"))
# Insert text at the top
study <- insert_text(study,
text = "Adjusted model",
col = 2:4,
part = "header",
gp = grid::gpar(fontface = "bold"))
study <- add_text(study,
text = "Unadjusted model",
col = 5:7,
row = 1,
part = "header",
gp = grid::gpar(fontface = "bold"))
# Add underline at the bottom of the header
study <- add_border(study, part = "header", row = 1, where = "top")
study <- add_border(study, part = "header", row = 2, where = "bottom")
# study <- add_border(study, part = "header", row = 1, col = 2:4,
# gp = gpar(lwd = 2))
# study <- add_border(study, part = "header", row = 1, col = 5:7,
# gp = gpar(lwd = 2))
plot(study)
```
```{r fig.align="center", echo = FALSE,fig.height = 12, fig.width = 14}
```