-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy paththemes.R
204 lines (192 loc) · 5.89 KB
/
themes.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
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
#' Quickly scale the size of a ggplot
#'
#' `expand_plot()` is a convenience function that expands the scales of a
#' ggplot, as the large node sizes in a DAG will often get clipped in themes
#' that don't have DAGs in mind.
#'
#' @param expand_x,expand_y Vector of range expansion constants used to add some
#' padding around the data, to ensure that they are placed some distance away
#' from the axes. Use the convenience function `ggplot2::expansion()` to
#' generate the values for the expand argument.
#' @export
expand_plot <- function(
expand_x = expansion(c(.10, .10)),
expand_y = expansion(c(.10, .10))) {
list(
ggplot2::scale_x_continuous(expand = expand_x),
ggplot2::scale_y_continuous(expand = expand_y)
)
}
#' Minimalist DAG themes
#'
#' @inheritParams ggplot2::theme_minimal
#' @param ... additional arguments passed to `theme()`
#'
#' @export
#'
#' @examples
#' ggdag(m_bias()) + theme_dag_blank() # the default
#'
#' @rdname theme_dag_blank
#'
#' @importFrom ggplot2 %+replace%
theme_dag_blank <- function(base_size = 12, base_family = "", ...) {
ggplot2::theme_minimal(base_size = base_size, base_family = base_family) %+replace%
ggplot2::theme(
strip.text = ggplot2::element_text(face = "bold"),
axis.text = ggplot2::element_blank(),
axis.title = ggplot2::element_blank(),
panel.grid = ggplot2::element_blank(),
...,
complete = TRUE
)
}
#' @rdname theme_dag_blank
#' @export
theme_dag <- theme_dag_blank
#' @rdname theme_dag_blank
#' @export
#' @importFrom ggplot2 %+replace%
theme_dag_grid <- function(base_size = 12, base_family = "", ...) {
ggplot2::theme_minimal(base_size = base_size, base_family = base_family) %+replace%
ggplot2::theme(
axis.text = ggplot2::element_blank(),
axis.title = ggplot2::element_blank(),
...,
complete = TRUE
)
}
#' Simple grey themes for DAGs
#'
#' @inheritParams ggplot2::theme_grey
#' @param ... additional arguments passed to `theme()`
#'
#' @export
#'
#' @rdname theme_dag_grey
#'
#' @examples
#'
#' ggdag(m_bias()) + theme_dag_grey()
#'
#' @importFrom ggplot2 %+replace%
theme_dag_grey <- function(base_size = 12, base_family = "", ...) {
ggplot2::theme_grey(base_size = base_size, base_family = base_family) %+replace%
ggplot2::theme(
axis.text = ggplot2::element_blank(),
axis.title = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
panel.grid.major = ggplot2::element_line(colour = "grey92"),
panel.grid.minor = ggplot2::element_line(colour = "grey92"),
...,
complete = TRUE
)
}
#' @rdname theme_dag_grey
#' @export
theme_dag_gray <- theme_dag_grey
#' @rdname theme_dag_grey
#' @export
theme_dag_grey_grid <- function(base_size = 12, base_family = "", ...) {
ggplot2::theme_grey(base_size = base_size, base_family = base_family) %+replace%
ggplot2::theme(
axis.text = ggplot2::element_blank(),
axis.title = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
...,
complete = TRUE
)
}
#' @rdname theme_dag_grey
#' @export
theme_dag_gray_grid <- theme_dag_grey_grid
#' Common scale adjustments for DAGs
#'
#' `scale_adjusted()` is a convenience function that implements ways of
#' visualizing adjustment for a variable. By convention, a square shape is used
#' to indicate adjustment and a circle when not adjusted. Arrows out of adjusted
#' variables are often eliminated or de-emphasized, and `scale_adjusted()` uses
#' a lower `alpha` for these arrows. When adjusting a collider, a dashed line is
#' sometimes used to demarcate opened pathways, and `scale_adjusted()` does this
#' whenever [geom_dag_collider_edges()] is used. `scale_dag()` is deprecated in
#' favor of `scale_adjusted()`.
#'
#' @param include_alpha Logical. Include alpha-related scales?
#' @param breaks One of:
#'
#' - NULL for no breaks
#'
#' - waiver() for the default breaks computed by the transformation object
#'
#' - A numeric vector of positions
#'
#' - A function that takes the limits as input and returns breaks as output
#'
#'
#' @export
#' @rdname scale_adjusted
scale_adjusted <- function(include_alpha = FALSE) {
list(
ggplot2::scale_linetype_manual(name = NULL, values = "dashed"),
ggplot2::scale_shape_manual(
values = c("adjusted" = 15, "unadjusted" = 19),
limits = c("adjusted", "unadjusted")
),
ggplot2::scale_color_discrete(limits = c("adjusted", "unadjusted")),
if (include_alpha) {
ggplot2::scale_alpha_manual(
values = c("adjusted" = .30, "unadjusted" = 1),
limits = c("adjusted", "unadjusted")
)
},
if (include_alpha) {
ggraph::scale_edge_alpha_manual(
name = NULL,
values = c("adjusted" = .30, "unadjusted" = 1),
limits = c("adjusted", "unadjusted")
)
}
)
}
breaks <- function(breaks = ggplot2::waiver(), name = ggplot2::waiver(), drop = TRUE) {
list(
ggplot2::scale_color_discrete(name = name, breaks = breaks, drop = drop),
ggplot2::scale_fill_discrete(name = name, breaks = breaks, drop = drop)
)
}
#' @rdname scale_adjusted
#' @export
scale_dag <- function(breaks = ggplot2::waiver()) {
.Deprecated("scale_adjusted")
list(
scale_adjusted(),
breaks(breaks = breaks)
)
}
#' Quickly remove plot axes and grids
#'
#' `remove_axes()` and `remove_grid()` are convenience functions that removes
#' the axes and grids from a ggplot, respectively. This is useful when you want
#' to use an existing theme, e.g. those included in `ggplot2`, for a DAG.
#'
#' @export
#'
#' @examples
#' library(ggplot2)
#' ggdag(confounder_triangle()) +
#' theme_bw() +
#' remove_axes()
#'
#' @rdname remove_axes
remove_axes <- function() {
ggplot2::theme(
axis.text = ggplot2::element_blank(),
axis.title = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank()
)
}
#' @rdname remove_axes
#' @export
remove_grid <- function() {
ggplot2::theme(panel.grid = ggplot2::element_blank())
}