-
Notifications
You must be signed in to change notification settings - Fork 13
/
.Rprofile
236 lines (195 loc) · 6.95 KB
/
.Rprofile
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
library(reticulate)
use_condaenv("book-of-models")
source("load_packages.R")
source("functions/utils.R")
filter = dplyr::filter
# create a theme
# sysfonts::font_add_google(name = "Roboto Condensed", family = "roboto-condensed")
# nonsense required for ggplot + latex to figure things out, still doesn't work;
# NOTE: it seems the font will render correctly via a browser viewer (sometimes), but not in acrobat reader or acrobat, which is
# ridiculous but at least we can save the browser pdf for correct rendering.
# probably the only way to get plots to behave for latex/pdf will likely be to save them out and use conditional
# includegraphics; this is also partly ggplot as it will give warnings for pdf but not html
# library(extrafont)
extrafont::loadfonts(quiet = FALSE)
# extrafont::font_import(pattern = "RobotoCondensed", prompt = FALSE)
# extrafont::font_import(pattern = "Arial Narrow", prompt = FALSE)
# showtext::showtext_auto()
# check for narrow fonts, if not available, use default
# plot_fonts = c('roboto condensed', 'arial narrow')
# check_fonts = which(plot_fonts %in% tolower(unique(systemfonts::system_fonts()$family)))
# plot_ff = stringr::str_to_title(ifelse(length(check_fonts) == 0, '', plot_fonts[which.min(check_fonts)]))
plot_ff = 'Roboto Condensed'
# plot_ff = 'Arial Narrow'
# plot_ff = 'Roboto'
theme_clean = function(
font_size = 12,
font_family = plot_ff,
center_axis_labels = FALSE
) {
if (center_axis_labels) {
haxis_just_x = 0.5
vaxis_just_y = 0.5
v_rotation_x = 0
v_rotation_y = 0
} else {
haxis_just_x = 0
vaxis_just_y = 1
v_rotation_x = 0
v_rotation_y = 0
}
ggplot2::theme(
text = ggplot2::element_text(
family = font_family,
face = 'plain',
color = 'gray30',
size = font_size,
hjust = 0.5,
vjust = 0.5,
angle = 0,
lineheight = 0.9,
margin = ggplot2::margin(),
debug = FALSE
),
axis.title.x = ggplot2::element_text(
hjust = haxis_just_x,
angle = v_rotation_x,
size = 1.2 * font_size,
face = 'bold'
),
axis.title.y = ggplot2::element_text(
vjust = vaxis_just_y,
hjust = 0,
angle = v_rotation_y,
size = 1.2 * font_size,
family = font_family,
face = 'bold',
),
axis.ticks = ggplot2::element_line(color = 'gray30'),
title = ggplot2::element_text(
color = 'gray30',
size = font_size * 1.25,
margin = margin(b = font_size * 0.3),
family = font_family,
face = 'bold',
),
plot.subtitle = ggplot2::element_text(
color = 'gray30',
size = font_size * 1,
hjust = 0,
family = font_family,
face = 'bold',
),
plot.caption = ggplot2::element_text(
color = 'gray30',
size = font_size * .8,
hjust = 0,
family = font_family,
face = 'bold',
),
legend.position = 'bottom',
legend.key = ggplot2::element_rect(fill = 'transparent', color = NA),
legend.background = ggplot2::element_rect(fill = 'transparent', color = NA),
legend.title = ggplot2::element_blank(),
panel.background = ggplot2::element_blank(),
panel.grid.major = ggplot2::element_line(color = 'gray95'),
strip.background = ggplot2::element_blank(),
plot.background = ggplot2::element_rect(fill = 'transparent', color = NA),
)
}
# theme_other = theme_clean = function (
# ) {
# }
# set the theme as default
theme_set(theme_clean())
# set other point/line default colors; in most cases, we can use the color from
# default discrete scale for more consistency across plots.
# paletteer::palettes_d$colorblindr$OkabeIto
update_geom_defaults('vline', list(color = 'gray25', alpha = .25)) # vlines and hlines are typically not attention grabbers so set alpha
update_geom_defaults('hline', list(color = 'gray25', alpha = .25)) # usually a zero marker
update_geom_defaults('point', list(color = '#E69F00', alpha = .5)) # alpha as usually there are many points
update_geom_defaults('pointrange', list(color = '#0072B2'))
update_geom_defaults('smooth', list(color = '#0072B2', alpha = .15))
update_geom_defaults('line', list(color = '#0072B2', alpha = .75))
update_geom_defaults('path', list(color = '#0072B2', alpha = .75))
update_geom_defaults('abline', list(color = '#0072B2', alpha = 1))
update_geom_defaults('ribbon', list(color = NA, fill = '#E69F00'))
update_geom_defaults('bar', list(color = '#E69F00', fill = '#E69F00'))
update_geom_defaults('col', list(color = '#E69F00', fill = '#E69F00'))
update_geom_defaults('dotplot', list(color = '#E69F00', fill = '#E69F00'))
# use colorblind safe colors for categories; if you supply a continuous value to
# color you'll get an error, but you just have to use `myplot +
# scale_color_continous()` or whatever to override this; likewise you can always
# override this scale for categorical schemes if desired also. Note that this
# will apply for both color and fill, which is usually what we want.
okabe_ito = c(
orange = '#E69F00',
blue = '#56B4E9',
green = '#009E73',
yellow = '#F0E442',
darkblue = '#0072B2',
red = '#D55E00',
pink = '#CC79A7',
gray = '#999999'
)
# Use the following to overwrite basic ggplot to use color scheme
# ggplot = function(...) ggplot2::ggplot(...) +
# # okabe ito colorblind safe scheme
# scale_color_manual(
# values = okabe_ito,
# drop = FALSE,
# aesthetics = c('color', 'fill')
# )
# Tables ------------------------------------------------------------------
gt = function(..., decimals = 2, title = NULL, subtitle = NULL) {
gt::gt(...) %>%
gt::fmt_number(
columns = where(is.numeric),
decimals = decimals
) %>%
gt::tab_style(
style = gt::cell_text(color = 'gray25'),
locations = gt::cells_body(
columns = c( # TODO: update to c() or just drop vars
where(is.numeric)
)
)
) %>%
gt::tab_header(title = title, subtitle = subtitle) %>%
gtExtras::gt_theme_nytimes() |>
tab_options(quarto.disable_processing = TRUE) # May have unintended consequences see https: //github.com/quarto-dev/quarto-cli/issues/6945
}
gt_theme =
list(
# report median (IQR) and n (percent) as default stats in `tbl_summary()`
'tbl_summary-str:continuous_stat' = '{mean} ({sd})',
'tbl_summary-str:categorical_stat' = '{n} ({p})'
)
gtsummary::set_gtsummary_theme(gt_theme)
tbl_summary = function(..., title = '', butcher = TRUE) {
tbl_out = gtsummary::tbl_summary(
...,
digits = list(
all_continuous() ~ c(1, 1),
all_categorical() ~ c(0, 1)
)
) %>%
gtsummary::modify_caption(caption = title)
#
# # trim dataset etc from table; may lose other functionality
if (butcher) {
tbl_out = tbl_out %>%
gtsummary::tbl_butcher()
}
#
tbl_out
}
ggsave = function(filename, width = 8, height = 6, ...) {
ggplot2::ggsave(
filename = filename,
width = width,
height = height,
...
)
}
options(digits = 4) # number of digits of precision for floating point output