17
17
# ' @template args-y-yrep
18
18
# ' @param size,alpha Passed to the appropriate geom to control the appearance of
19
19
# ' the `yrep` distributions.
20
- # ' @param ... Currently unused .
20
+ # ' @param ... Currently only used internally .
21
21
# '
22
22
# ' @template return-ggplot
23
23
# '
30
30
# ' `y`. Note that the replicated data from `yrep` is assumed to be
31
31
# ' uncensored.
32
32
# ' }
33
+ # ' \item{`ppc_km_overlay_grouped()`}{
34
+ # ' The same as `ppc_km_overlay()`, but with separate facets by `group`.
35
+ # ' }
33
36
# ' }
34
37
# '
35
38
# ' @templateVar bdaRef (Ch. 6)
50
53
# ' \donttest{
51
54
# ' ppc_km_overlay(y, yrep[1:25, ], status_y = status_y)
52
55
# ' }
56
+ # ' # With separate facets by group:
57
+ # ' group <- example_group_data()
58
+ # ' \donttest{
59
+ # ' ppc_km_overlay_grouped(y, yrep[1:25, ], group = group, status_y = status_y)
60
+ # ' }
53
61
NULL
54
62
55
63
# ' @export
@@ -65,7 +73,8 @@ ppc_km_overlay <- function(
65
73
size = 0.25 ,
66
74
alpha = 0.7
67
75
) {
68
- check_ignored_arguments(... )
76
+ check_ignored_arguments(... , ok_args = " add_group" )
77
+ add_group <- list (... )$ add_group
69
78
70
79
if (! requireNamespace(" survival" , quietly = TRUE )){
71
80
abort(" Package 'survival' required." )
@@ -91,11 +100,25 @@ ppc_km_overlay <- function(
91
100
as.numeric(as.character(.data $ group )),
92
101
1 ))
93
102
103
+ sf_form <- survival :: Surv(value , group ) ~ rep_label
104
+ if (! is.null(add_group )){
105
+ data <- dplyr :: inner_join(data ,
106
+ tibble :: tibble(y_id = seq_along(y ),
107
+ add_group = add_group ),
108
+ by = " y_id" )
109
+ sf_form <- update(sf_form , . ~ . + add_group )
110
+ }
94
111
sf <- survival :: survfit(
95
- survival :: Surv( value , group ) ~ rep_label ,
112
+ sf_form ,
96
113
data = data
97
114
)
115
+ names(sf $ strata ) <- sub(" add_group=" , " add_group:" , names(sf $ strata )) # Needed to split the strata names in ggfortify:::fortify.survfit() properly.
98
116
fsf <- fortify(sf )
117
+ if (any(grepl(" add_group" , levels(fsf $ strata )))){
118
+ strata_split <- strsplit(as.character(fsf $ strata ), split = " , add_group:" )
119
+ fsf $ strata <- as.factor(sapply(strata_split , " [[" , 1 ))
120
+ fsf $ group <- as.factor(sapply(strata_split , " [[" , 2 ))
121
+ }
99
122
100
123
fsf $ is_y_color <- as.factor(sub(" \\ [rep\\ ] \\ (.*$" , " rep" , sub(" ^italic\\ (y\\ )" , " y" , fsf $ strata )))
101
124
fsf $ is_y_size <- ifelse(fsf $ is_y_color == " yrep" , size , 1 )
@@ -114,8 +137,14 @@ ppc_km_overlay <- function(
114
137
alpha = ~ is_y_alpha )) +
115
138
geom_step() +
116
139
hline_at(
117
- c(0 , 0.5 , 1 ),
118
- size = c(0.2 , 0.1 , 0.2 ),
140
+ 0.5 ,
141
+ size = 0.1 ,
142
+ linetype = 2 ,
143
+ color = get_color(" dh" )
144
+ ) +
145
+ hline_at(
146
+ c(0 , 1 ),
147
+ size = 0.2 ,
119
148
linetype = 2 ,
120
149
color = get_color(" dh" )
121
150
) +
@@ -129,3 +158,32 @@ ppc_km_overlay <- function(
129
158
yaxis_ticks(FALSE ) +
130
159
bayesplot_theme_get()
131
160
}
161
+
162
+ # ' @export
163
+ # ' @rdname PPC-censoring
164
+ # ' @template args-group
165
+ ppc_km_overlay_grouped <- function (
166
+ y ,
167
+ yrep ,
168
+ group ,
169
+ ... ,
170
+ status_y ,
171
+ size = 0.25 ,
172
+ alpha = 0.7
173
+ ) {
174
+ check_ignored_arguments(... )
175
+
176
+ p_overlay <- ppc_km_overlay(
177
+ y = y ,
178
+ yrep = yrep ,
179
+ add_group = group ,
180
+ ... ,
181
+ status_y = status_y ,
182
+ size = size ,
183
+ alpha = alpha
184
+ )
185
+
186
+ p_overlay +
187
+ facet_wrap(" group" ) +
188
+ force_axes_in_facets()
189
+ }
0 commit comments