-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.qmd
191 lines (179 loc) · 6.88 KB
/
theme.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
# Theme {#sec-theme}
```{r}
#| label: setup
#| message: false
#| warning: false
#| include: false
library(tidyverse)
library(lubridate)
library(scales)
library(knitr)
library(kableExtra)
library(colorblindr)
library(downlit)
# _common.R ----
source("_common.R")
# use font
showtext::showtext_auto()
# set theme
ggplot2::theme_set(theme_ggp2g(
base_size = 15))
```
```{r}
#| label: co_box_dev
#| echo: false
#| results: asis
#| eval: true
co_box(
color = "o", look = "minimal",
header = "Caution",
contents = "This section is still being developed. The contents are subject to change.",
fold = FALSE
)
library(dplyr)
```
The theme used in the graphs is custom and uses combined elements from `ggplot2::theme_minimal()` and `ggplot2::theme_void()`. View it [here](https://github.com/mjfrigaard/fm-ggp2/blob/main/_assets/R/theme_ggp2g.R).
```{r}
#| code-fold: show
#| code-summary: 'show/hide theme_ggp2g.R'
# pkgs --------------------------------------------------------------------
require(ggplot2)
require(extrafont)
require(showtext)
require(showtextdb)
require(sysfonts)
library(extrafont)
library(palmerpenguins)
library(fs)
library(janitor)
# theme_ggp2g -------------------------------------------------------------
# this is a custom theme for the ggplot2 gallery
# import fonts like the function in hrbrthemes
# https://github.com/hrbrmstr/hrbrthemes/blob/3e8d9494a9e0026a3127f6a0df88208511cd0715/R/roboto-condensed.r#L208
# First call the font_import() is called inside each setup chunk
# extrafont::font_import(
# paths = "assets/Ubuntu/",
# prompt = FALSE)
# we then add the font using font_add()
# sysfonts::font_add(
# family = "Ubuntu",
# regular = "assets/Ubuntu/Ubuntu-Regular.ttf")
# finally we use showtext_auto() to use the font in the graph
# showtext::showtext_auto()
# "If turned on, any newly opened graphics devices will use showtext to
# draw text"
theme_ggp2g <- function(base_size = 11, base_family = "Ubuntu",
base_line_size = base_size / 22,
base_rect_size = base_size / 22) {
half_line <- base_size / 2
# most of this is borrowed from theme_minimal()/theme_void(), but with some
# adjustments to panel, margins, and legend see the original here:
# https://github.com/tidyverse/ggplot2/blob/d9f179b038f020158773fac54af9a84cf961b54b/R/theme-defaults.r#L459
thm <- theme(
# RECTANGLE ----------------------------------------------------
rect = element_blank(),
# TEXT ---------------------------------------------------------
text = element_text(
family = "Ubuntu",
# face = "plain",
colour = "black",
size = base_size,
lineheight = 0.9,
hjust = 0.5, vjust = 0.5,
angle = 0,
margin = margin(),
debug = FALSE),
## AXES --------------------------------------------------------------
axis.text = element_text(
family = base_family, size = rel(0.65)),
axis.title.x = element_text(vjust = -4.75),
axis.title.y = element_text(vjust = 4.25, angle = 90),
axis.title = element_text(size = rel(0.8)),
### AXIS TICKS ----
## remove all axis ticks
## https://ggplot2-book.org/polishing.html#theme-axis
axis.ticks.length = unit(0, "pt"),
axis.ticks.length.x = NULL,
axis.ticks.length.x.top = NULL,
axis.ticks.length.x.bottom = NULL,
axis.ticks.length.y = NULL,
axis.ticks.length.y.left = NULL,
axis.ticks.length.y.right = NULL,
## STRIP ----
##
strip.clip = "inherit",
strip.text.x = element_text(size = rel(0.7)),
# strip.text.x affects both facet_wrap() or facet_grid()
strip.text.y = element_text(size = rel(0.7)),
# strip.text.y only affects facet_grid()
strip.switch.pad.grid = unit(half_line / 2, "pt"),
strip.switch.pad.wrap = unit(half_line / 2, "pt"),
strip.background = element_rect(fill = "#d0d0d0"),
## PANEL ----
## here we introduce thin, light gray lines for the graph area (with
## vertical lines slightly larger than horizontal lines)
## https://ggplot2-book.org/polishing.html#panel-elements
panel.ontop = FALSE,
panel.spacing = unit(half_line, "pt"),
panel.grid.major.x = element_line(color = "#d0d0d0", linewidth = 0.3),
panel.grid.major.y = element_line(color = "#d0d0d0", linewidth = 0.3),
panel.grid.minor.x = element_line(color = "#d0d0d0", linewidth = 0.1),
panel.grid.minor.y = element_line(color = "#d0d0d0", linewidth = 0.1),
## TITLES ----
### Title -----
# location of title
plot.title.position = "panel",
## slightly larger text
plot.title = element_text(
size = rel(1.1),
hjust = 0, vjust = 2,
margin = margin(b = half_line)
),
### subtitle -----
# slightly smaller text, italic
plot.subtitle = element_text(
size = rel(0.9),
hjust = 0, vjust = 2,
margin = margin(b = half_line)
),
### caption -----
plot.caption = element_text(
size = rel(0.8),
hjust = 1, vjust = 1,
margin = margin(b = half_line)
),
plot.caption.position = "panel",
### tag -----
plot.tag = element_text(
size = rel(1.2),
hjust = 0.5, vjust = 0.5
),
plot.tag.position = 'topleft',
## LEGEND ----
## https://ggplot2-book.org/polishing.html#legend-elements
# legend.direction = "",
# legend.box.just = "",
legend.box = NULL,
legend.key.size = unit(1.2, "lines"),
# the positioning is = c(horizontal, vertical)
# c(1, 0) = bottom right
# c(0.5, 0.5) = dead center
# c(0, 1) = top left
# https://ggplot2-book.org/scale-colour.html#legend-layout
# legend.position = c(1, 0.5), # c(horizontal, vertical)
# legend.justification = c(-0.015, 0.0), # c(horizontal, vertical)
legend.text = element_text(
size = rel(0.60)),
legend.title = element_text(
size = rel(0.70),
hjust = 0),
# legend.margin = unit(c(1.5, 1.5, 1.5, 1.5), "mm"),
legend.margin = margin(t = 2.5, r = 2.5, b = 2.5, l = 2.5),
## PLOT MARGIN ----
## https://ggplot2-book.org/polishing.html#plot-elements
plot.margin = unit(x = c(1.5, 5.0, 1.5, 1.5), "lines"),
complete = TRUE
)
return(thm)
}
```