-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.qmd
388 lines (259 loc) · 5.72 KB
/
custom.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
---
fig-width: 4.5
fig-height: 4
fig-align: center
---
```{r setup, include = FALSE}
source("global.R")
```
# Customize plots {#sec-customize}
## Layers
"Layers" refer to different reference lines or smoothers that are added to plots
by default.
Layers include
- smoothing line through the data: `smooth`
- horizontal reference lines: `hline`
- identity lines: `abline`
For example, `dv_pred()` adds an identity line (`y = x`) for reference as
well as a smoother through the data
```{r}
dv_pred(df)
```
These layers can all be customized or dropped from the plot altogether. This
section shows you how to do that. We'll work on this basic plot as well
as plots that come right out of `pmplots`.
```{r}
p <- ggplot(df, aes(PRED, DV)) + geom_point() + pm_theme()
p
```
### Layer smooth
```{r}
p + pm_smooth()
```
or
```{r, eval = FALSE}
layer_s(p)
```
### Layer abline
```{r}
p + pm_abline()
```
or
```{r, eval = FALSE}
layer_a(p)
```
### Layer hline
```{r}
p + pm_hline(2000)
```
or
```{r, eval = FALSE}
layer_h(p, hline = 2000)
```
### Drop specific layers
Set the layer to `NULL` to drop one or more of these layers from the plot.
```{r}
dv_pred(df, smooth = NULL)
```
```{r}
dv_pred(df, abline = NULL)
```
```{r}
cwres_time(df, hline = NULL)
```
```{r}
dv_pred(df, abline = NULL, smooth = NULL)
```
### Drop all layers
You can also decline to add any layers through `add_layers`
```{r}
dv_pred(df, add_layers = FALSE)
```
### Modify layer specs
For any layer type, you can pass a list of arguments that will get passed on
to the appropriate `geom`.
For example, change the values of argument for `ggplot2::geom_smooth()`
```{r}
cwres_time(
df,
smooth = list(method = "gam", se = TRUE)
)
```
The items in the list getting passed as `smooth` should be arguments in
`ggplot2::geom_smooth()`.
Similarly, you can set `abline` with a list of arguments to get passed to
`geom_abline()` or `hline` with a list of arguments to get passed to
`geom_hline()`.
### Extra reference lines to [C]WRES plots
```{r}
wres_time(df) + geom_3s()
```
## Axes
You can pass lists of arguments as `xs` or `ys` to modify the x- or y-axis,
respectively. These arguments get passed to `ggplot2::scale_x_continuous()` or
`ggplot2::scale_y_continuous()`.
### Modify x-axis
To modify the x-axis of a plot, pass a list of items as the `xs` argument;
these items will get passed to `ggplot2::scale_x_continuous()`.
For example, to make the x-axis log transformed and modify the breaks, write
```{r}
a <- list(trans = "log", breaks = logbr3())
dv_time(df, xs = a)
```
Note: that you can modify the x-axis label of a plot without modifying the scale
using `ggplot2::xlab()`
```{r}
dv_time(df) + xlab("Time (years)")
```
### Modify y-axis
To modify the y-axis of a plot, pass a list of items as the `ys` argument;
these items will get passed to `ggplot2::scale_y_continuous()`.
For example, to make the y-axis log transformed and modify the breaks, write
```{r}
dv_time(df, ys = a, yname = "Y-axis name")
```
There may also be a `yname` argument for a given plot that will let you
directly change the y-axis label as in the example above.
Note: that you can modify the y-axis label of a plot without modifying the scale
using `ggplot2::ylab()`
```{r}
dv_time(df) + ylab("Concentration (centrigrams/gallon)")
```
### Rotate x and y axis labels
#### Specify the angle {.unnumbered .unlisted}
```{r}
dv_pred(df) + rot_x(angle = 90) + rot_y()
```
#### Rotate to vertical {.unnumbered .unlisted}
We are typically rotating the tick labels on the x-axis and
frequently it is convenient to ask for a totally vertical
rendering
```{r}
#| fig-width: 7
#| fig-height: 5
cwres_cat(df, x = "STUDYc") +
facet_wrap(~CPc) + rot_x(vertical = TRUE)
```
### Flip coordinates when labels get cramped
If this is too cramped
```{r}
#| fig-width: 7
#| fig-height: 5
cont_cat(
id,
y = c("WT", "BMI", "ALB", "CRCL"),
x = "STUDYc"
) %>% pm_grid()
```
Try this
```{r}
#| fig-height: 5
#| fig-width: 7
cont_cat(
id,
y = c("WT", "BMI", "ALB", "CRCL"),
x = "STUDYc"
) %>% map(~.x+coord_flip()) %>% pm_grid()
```
### Standard axis titles
```{r}
pm_axis_time()
pm_axis_tad()
pm_axis_tafd()
pm_axis_res()
pm_axis_wres()
pm_axis_cwres()
pm_axis_cwresi()
pm_axis_npde()
pm_axis_dv()
pm_axis_pred()
pm_axis_ipred()
```
You can `glue()` in information with these functions
```{r}
pm_axis_time("hr")
```
Similar with
```{r, eval = FALSE}
pm_axis_tafd("hr")
pm_axis_tad("hr")
```
And
```{r}
pm_axis_dv("ASA concentration (ng/mL)")
```
Similar with
```{r, eval = FALSE}
pm_axis_pred("ASA concentration (ng/mL)")
pm_axis_ipred("ASA concentration (ng/mL)")
```
### Log breaks
```{r}
logbr3()
```
```{r}
logbr()
```
### Custom breaks
Default breaks
```{r}
dv_time(df)
```
Break every 3 days
```{r}
dv_time(df, xby = 72)
```
Custom breaks and limits
```{r}
a <- list(breaks = seq(0,240,48), limits = c(0,240))
dv_time(df, xs = a)
```
## Replicate look and feel
```{r}
p <- ggplot(df, aes(IPRED,DV)) + geom_point()
p
```
### Theme
```{r}
p + pm_theme()
```
### Plain
```{r}
p + theme_plain()
```
### Smooth
```{r}
p + pm_smooth()
```
### Abline
```{r}
p + pm_abline()
```
### Horizontal reference line
```{r}
ggplot(df, aes(TIME,CWRES)) + geom_point() + pm_hline()
```
## Latex
### Latex in axis title
```{r}
dv_pred(df, x = "PRED//Concentration ($\\mu$g)")
```
### Latex in pairs plot
```{r}
#| fig-width: 6
#| fig-height: 6
data <- data.frame(
m = rnorm(100),
s = rnorm(100),
n = rnorm(100)
)
x <- c("m//$\\mu$", "s//$\\sigma$", "n//$\\nu$")
pairs_plot(data,x)
```
### Latex in wrapped plots
```{r}
#| fig-width: 7
#| fig-height: 3
y <- c("WT//Weight (kg)", "BMI//BMI (kg/m$^2$)", "SCR//SCR (g/dL)")
wrap_cont_time(df, y = y, use_labels=TRUE)
```