-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_util.R
177 lines (160 loc) · 5.74 KB
/
plot_util.R
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
####################################
## utility functions for plotting ##
####################################
## generate transformation value and formula for overlayed plots with dual-axis
## compatiable with dplyr and ggplot
convert_scale<-function(from,to){
# convert value
range_rt<-(max(to) - min(to))/(max(from) - min(from))
new_from<-((from - min(from))*range_rt) + min(to)
# generate formula to recover axis (revert the new_from formula)
revert_formula<-c(paste0("~ (. - ",min(to),") * ",1/range_rt),
rep(NA,length(from)-1))
# results
return(list(val=new_from,
formula=revert_formula))
}
plot_dualy<-function(
df, # data.frame
x, # x-axis
y, # primary y-axis - bar plot
y_sec # secondary y-axis - line plot
){
# require(tidyverse)
# covert secondary y to the same scale of primary y
df_cp<-df %>%
mutate(new_col = convert_scale(y_sec,y)[["val"]],
axis_formula = convert_scale(y_sec,y)[["formula"]])
# ggplot object
plt<-ggplot(df_cp,aes(x=x)) +
geom_col(aes(y=y), size = 1, color = "darkblue", fill = "white")+
geom_line(aes(y=new_col), size = 1.5, color="red", group = 1)+
scale_y_continuous(sec.axis = sec_axis(as.formula(df_cp$axis_formula[1]), name = "sec_axis"))
# compatible with ggplot syntax
return(plt)
}
## forestplot
forestplot.HR <- function (
df, # long table
x_idx1="vari", # 1st layer index
x_idx2="vari_cat", # 2nd layer index
y_idx="endpt", # 1st layer y index
est="est", # estimates
lower="lower", # 95% CI lower bound
upper="upper", # 95% CI upper bound
pval="pval", # p value
plt_par = list(), # other plotting parameters passed in forest function
ny = 1, # number of y groups
idx_display = "Variable",
lbl_pval = TRUE, # whether to use p-value for the label column
tm = forest_theme(
arrow_type = "closed",
arrow_label_just = "end"
) # theme parameters for forest function
){
# require(tidyverse,grid,forestploter)
# https://cran.r-project.org/web/packages/forestploter/vignettes/forestploter-intro.html
plt_par$ci_column=Filter(Negate(is.null),list(plt_par$ci_column,2*seq_len(ny)))[[1]]
plt_par$ref_line = Filter(Negate(is.null),list(plt_par$ref_line,rep(1, ny)))[[1]]
plt_par$xlab = Filter(Negate(is.null),list(plt_par$xlab,rep("HR", ny)))[[1]]
plt_par$nudge_y = c(plt_par$nudge_y,0.2)[1]
plt_par$xlim = Filter(Negate(is.null),list(plt_par$xlim,rep(list(c(0, 1.5)),ny)))[[1]]
plt_par$ticks_at = Filter(Negate(is.null),list(plt_par$ticks_at,rep(list(c(0.1, 0.5, 1, 1.5)),ny)))[[1]]
# optional
plt_par$vert_line = plt_par$vert_line
plt_par$arrow_lab = plt_par$arrow_lab
plt_par$x_trans = plt_par$x_trans
# change to internal names for easy reference
nm_map<-data.frame(
ext_nm=c(x_idx1,x_idx2,y_idx,est,lower,upper,pval)) %>%
mutate(int_nm=c("x_idx1","x_idx2","y_idx","est","lower","upper","pval"))
plt_df<-df %>%
select(all_of(nm_map$ext_nm)) %>%
rename_at(vars(nm_map$ext_nm), ~ nm_map$int_nm)
# add an empty column for HR plots and a label column
plt_df %<>%
# CI with asterisks
mutate(
pvalstar = case_when(pval > 0.1 ~ "",
pval <= 0.1 & pval > 0.05 ~ "*",
pval <= 0.05 & pval > 0.01 ~ "**",
pval <= 0.01 & pval > 0.001 ~ "***",
TRUE ~ "****"),
`..` = paste(rep(" ", 20), collapse = " "),
`HR (95% CI)` = sprintf("%.2f (%.2f-%.2f) %s",est, lower, upper, pvalstar)
) %>%
# P-values
mutate(
pvalrd = case_when(
pval >= 0.01 ~ as.character(round(pval,2)),
pval < 0.0005 ~ '0.000',
TRUE ~ as.character(round(pval,3))
),
`..` = paste(rep(" ", 20), collapse = " "),
`P-value` = pvalrd
)
# pivot wide
y_grp<-unique(plt_df$y_idx)
n_grp<-length(y_grp)
if(lbl_pval){
plt_df %<>%
pivot_wider(
id_cols = c("x_idx1","x_idx2"),
names_from = "y_idx",
values_from = c(`..`,est,lower,upper,`P-value`),
names_glue = "{y_idx}.{.value}"
)
}else{
plt_df %<>%
pivot_wider(
id_cols = c("x_idx1","x_idx2"),
names_from = "y_idx",
values_from = c(`..`,est,lower,upper,`HR (95% CI)`),
names_glue = "{y_idx}.{.value}"
)
}
# add header rows
if(lbl_pval){
tidy_col<-c("x_idx2",tidyr::expand_grid(!!!list(b=y_grp,a=c("...",".P-value"))) |> pmap_chr(paste0))
}else{
tidy_col<-c("x_idx2",tidyr::expand_grid(!!!list(b=y_grp,a=c("...",".HR (95% CI)"))) |> pmap_chr(paste0))
}
plt_header<-plt_df[FALSE,tidy_col] %>%
bind_rows(data.frame(
x_idx1 = unique(plt_df$x_idx1),
x_idx2 = unique(plt_df$x_idx1),
idx=0
)) %>%
replace(is.na(.), " ")
plt_df %<>%
mutate_at(tidy_col, ~replace_na(.," ")) %>%
mutate(x_idx2 = paste0(" ",x_idx2)) %>%
bind_rows(plt_header) %>%
arrange(x_idx1,idx) %>% select(-idx)
# collect list for HR segment plots
est_lst<-list()
lower_lst<-list()
upper_lst<-list()
for(i in seq_along(y_grp)){
y<-y_grp[i]
est_lst[[i]]<-unlist(plt_df[,paste0(y,".est")])
lower_lst[[i]]<-unlist(plt_df[,paste0(y,".lower")])
upper_lst[[i]]<-unlist(plt_df[,paste0(y,".upper")])
}
# plot forest
plt_df %<>% rename(!!idx_display:=x_idx2)
p <- forest(plt_df[,c(idx_display,tidy_col[-1])],
est = est_lst,
lower = lower_lst,
upper = upper_lst,
ci_column = plt_par$ci_column,
ref_line = plt_par$ref_line,
vert_line = plt_par$vert_line,
arrow_lab = plt_par$arrow_lab,
xlim = plt_par$xlim,
ticks_at = plt_par$ticks_at,
xlab = plt_par$xlab,
nudge_y = plt_par$nudge_y,
theme = tm)
return(p)
}