forked from rdboyes/forester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
180 lines (134 loc) · 5.81 KB
/
README.Rmd
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# forester
<!-- badges: start -->
<!-- badges: end -->
The goal of forester is to make it easy for you to create a publication-quality forest plot with as much or as little information displayed on either side as you require.
## Installation
This package is currently early in development, and must be installed from this github repo.
``` r
devtools::install_github("rdboyes/forester")
```
## Example
Suppose we wish to replicate the following figure published in the NEJM [1]:
![](man/figures/target_figure.jpg)
forester simply requires the left side of the table (in this case, three columns with Subgroups and counts for each of two groups) and vectors which contain the point estimates and confidence intervals.
```{r example}
library(forester)
table <- readxl::read_excel(here::here("inst/extdata/example_figure_data.xlsx"))
# indent the subgroup if there is a number in the placebo column
table$Subgroup <- ifelse(is.na(table$Placebo),
table$Subgroup,
paste0(" ", table$Subgroup))
# remove indent of the first row
table$Subgroup[1] <- "All Patients"
# use forester to create the table with forest plot
forester(left_side_data = table[,1:3],
estimate = table$Estimate,
ci_low = table$`CI low`,
ci_high = table$`CI high`,
display = FALSE,
xlim = c(-100, 25),
file_path = here::here("man/figures/forester_plot.png"))
```
![](man/figures/forester_plot.png)
forester handles the alignment of the graph and the table automatically, so figures with fewer rows or columns should work by simply passing a smaller data frame to the function:
```{r}
forester(left_side_data = table[1:12,1:3],
estimate = table$Estimate[1:12],
ci_low = table$`CI low`[1:12],
ci_high = table$`CI high`[1:12],
display = FALSE,
file_path = here::here("man/figures/fewer_rows.png"))
```
![](man/figures/fewer_rows.png)
```{r}
forester(left_side_data = table[,1],
estimate = table$Estimate,
ci_low = table$`CI low`,
ci_high = table$`CI high`,
display = FALSE,
file_path = here::here("man/figures/fewer_cols.png"))
```
![](man/figures/fewer_cols.png)
## Additional Fonts
While Courier has a certain appeal, you might want to give your tables a more modern look. However, due to the difficulty of aligning all elements when using them, the use of non-monospaced fonts should be considered experimental at this stage.
```{r}
library(extrafont)
loadfonts(device = "win")
windowsFonts("Fira Sans" = windowsFont("Fira Sans"))
forester(left_side_data = table[,1:3],
estimate = table$Estimate,
ci_low = table$`CI low`,
ci_high = table$`CI high`,
display = FALSE,
file_path = here::here("man/figures/forester_plot_fira.png"),
font_family = "Fira Sans")
```
![](man/figures/forester_plot_fira.png)
Adjusting table properties with different fonts will still work:
```{r}
forester(left_side_data = table[1:12,1:3],
estimate = table$Estimate[1:12],
ci_low = table$`CI low`[1:12],
ci_high = table$`CI high`[1:12],
display = FALSE,
font_family = "Fira Sans",
file_path = here::here("man/figures/fewer_rows_fira.png"))
```
![](man/figures/fewer_rows_fira.png)
```{r}
windowsFonts("Times New Roman" = windowsFont("Times New Roman"))
forester(left_side_data = table[1:12,1:3],
estimate = table$Estimate[1:12],
ci_low = table$`CI low`[1:12],
ci_high = table$`CI high`[1:12],
display = FALSE,
font_family = "Times New Roman",
file_path = here::here("man/figures/fewer_rows_times.png"))
```
![](man/figures/fewer_rows_times.png)
## Adding Arrows (Experimental)
```{r}
forester(left_side_data = table[,1:3],
estimate = table$Estimate,
ci_low = table$`CI low`,
ci_high = table$`CI high`,
display = FALSE,
file_path = here::here("man/figures/forester_plot_arrows.png"),
font_family = "Fira Sans",
xlim = c(-100, 25),
xbreaks = c(-100, -75, -50, -25, 0, 25),
arrows = TRUE,
arrow_labels = c("Inclisiran Better", "Placebo Better"))
```
![](man/figures/forester_plot_arrows.png)
## Adding additional ggplot objects (Experimental)
Custom ggplot objects can be passed to the `forester` function using the parameter `add_plot`. To align the plot with the rows of the table, the vertical center of the bottom row is at y = 0, and each row is one unit tall on the y axis. `add_plot_width` can be set to customize the width of the plot (units are relative to the width of the table).
```{r}
library(ggplot2)
library(tibble)
ex_plot <- ggplot(tibble(x = rep(1:7, each = 15), y = rep(0:14, times = 7)), aes(x = x, y = y)) +
geom_point()
forester(left_side_data = table[1:15,1:3],
estimate = table$Estimate[1:15],
ci_low = table$`CI low`[1:15],
ci_high = table$`CI high`[1:15],
display = FALSE,
font_family = "Times New Roman",
add_plot = ex_plot,
file_path = here::here("man/figures/add_dots.png"))
```
![](man/figures/add_dots.png)
## References
1. Ray, K. K., Wright, R. S., Kallend, D., Koenig, W., Leiter, L. A., Raal, F. J., Bisch, J. A., Richardson, T., Jaros, M., Wijngaard, P. L. J., Kastelein, J. J. P., & ORION-10 and ORION-11 Investigators. (2020). Two Phase 3 Trials of Inclisiran in Patients with Elevated LDL Cholesterol. The New England Journal of Medicine, 382(16), 1507–1519.