-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathggplot_themes.R
258 lines (250 loc) · 9.51 KB
/
ggplot_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
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
#' Customized pretty ggplot theme
#'
#' @param font Font family for ggplot text.
#' @param background_color Color for plot background.
#' @param strip_background_color Color for strip background (for
#' \code{ggplot2::facet_grid()} and \code{ggplot2::facet_wrap()}).
#' @param grid_color Color of panel grid major axes or \code{NULL} if want no
#' major grid lines.
#' @param axis_line_width Width of x and y axes lines.
#' @param show_ticks Logical; whether or not to show axes tick marks.
#' @param x_text_angle Logical; whether or not to angle x text at 45 degrees.
#' @param size_theme One of "small", "normal", "large", "xlarge"; default sizes
#' for plot text and titles. If \code{NULL}, defaults to values specified by
#' \code{axis_title_size}, \code{axis_text_size}, \code{legend_title_size},
#' \code{legend_text_size}, \code{strip_text_size}, \code{title_size}.
#' @param axis_title_size Font size of axis title. Ignored if \code{size_theme}
#' not \code{NULL}.
#' @param axis_text_size Font size of axis text. Ignored if \code{size_theme}
#' not \code{NULL}.
#' @param legend_title_size Font size of legend title. Ignored if
#' \code{size_theme} not \code{NULL}.
#' @param legend_text_size Font size of legend text/key. Ignored if
#' \code{size_theme} not \code{NULL}.
#' @param strip_text_size Font size of strip text. Ignored if \code{size_theme}
#' not \code{NULL}.
#' @param title_size Font size of plot title. Ignored if \code{size_theme}
#' not \code{NULL}.
#' @param ... = other arguments to pass to \code{ggplot2::theme()}
#'
#' @return A \code{ggplot2::theme()} object.
#'
#' @examples
#' require(ggplot2)
#' ggplot(iris) +
#' aes(x = Sepal.Length, y = Sepal.Width) +
#' geom_point() +
#' prettyGGplotTheme()
#'
prettyGGplotTheme <- function(font = "Helvetica",
background_color = "grey98",
strip_background_color = "#2c3e50",
grid_color = "grey90",
axis_line_width = 1,
show_ticks = TRUE,
x_text_angle = FALSE,
size_theme = NULL,
axis_title_size = 10, axis_text_size = 7,
legend_title_size = 10, legend_text_size = 8,
strip_text_size = 9, title_size = 12,
...) {
if (!is.null(size_theme)) {
if (size_theme == "small") {
axis_title_size <- 10
axis_text_size <- 7
legend_title_size <- 10
legend_text_size <- 8
strip_text_size <- 9
title_size <- 12
} else if (size_theme == "medium") {
axis_title_size <- 14
axis_text_size <- 10
legend_title_size <- 14
legend_text_size <- 10
strip_text_size <- 12
title_size <- 16
} else if (str_detect(size_theme, "large")) {
num_x <- str_count(size_theme, "x")
axis_title_size <- 18 + num_x * 2
axis_text_size <- 14 + num_x * 2
legend_title_size <- 18 + num_x * 2
legend_text_size <- 14 + num_x * 2
strip_text_size <- 16 + num_x * 2
title_size <- 20 + num_x * 2
} else {
stop("size_theme must be one of 'small', 'medium', 'large', 'xlarge', or NULL.")
}
}
custom_theme <- ggplot2::theme(
axis.title = ggplot2::element_text(family = font,
size = axis_title_size,
face = "bold"),
axis.text = ggplot2::element_text(family = font, size = axis_text_size),
axis.line = ggplot2::element_line(size = axis_line_width, color = "black"),
axis.ticks = ggplot2::element_line(size = ifelse(show_ticks,
ggplot2::rel(1), 0),
colour = "black"),
axis.text.x = ggplot2::element_text(angle = ifelse(x_text_angle, 45, 0),
hjust = ifelse(x_text_angle, 1, 0.5)),
panel.grid.major = ggplot2::element_line(colour = grid_color,
size = ggplot2::rel(0.5)),
panel.grid.minor = ggplot2::element_blank(),
panel.background = ggplot2::element_rect(fill = background_color),
strip.background = ggplot2::element_rect(fill = strip_background_color,
color = strip_background_color),
strip.text = ggplot2::element_text(color = "white", face = "bold",
size = strip_text_size),
legend.key = ggplot2::element_rect(fill = "grey98"),
legend.text = ggplot2::element_text(family = font, size = legend_text_size),
legend.title = ggplot2::element_text(family = font, face = "bold",
size = legend_title_size),
plot.title = ggplot2::element_text(family = font, face = "bold",
size = title_size),
...
)
return(custom_theme)
}
#' Customized pretty ggplot color theme
#'
#' @param color Vector used for color aesthetic. Should match
#' \code{ggplot2::aes(color = ...)} argument.
#' @param viridis Logical. Whether or not to use viridis scheme if using
#' discrete color scheme
#' @param option Argument indicating viridis palette name
#' @param drop Logical; whether or not to drop factors with no observations.
#' @param ... Other arguments to pass to scale_color_manual() or
#' scale_colour_viridis()
#'
#' @return A ggplot color theme object.
#'
#' @examples
#' require(ggplot2)
#' ggplot(iris) +
#' aes(x = Sepal.Length, y = Sepal.Width, color = Species) +
#' geom_point() +
#' prettyGGplotTheme() +
#' prettyGGplotColor(color = iris$Species)
#'
prettyGGplotColor <- function(color, viridis = F, option = "plasma",
drop = T, ...) {
discrete <- is.factor(color)
if (discrete) {
if (nlevels(color) <= 8 & viridis == FALSE) {
custom_palette <- RColorBrewer::brewer.pal(n = 8, name = "Dark2")
custom_palette[2] <- custom_palette[1]
custom_palette[1] <- "#FF9300"
custom_color <- ggplot2::scale_color_manual(values = custom_palette,
drop = drop, ...)
} else {
custom_color <- viridis::scale_colour_viridis(
discrete = discrete, option = option,
begin = 0, end = 0.95, drop = drop, ...
)
}
} else {
custom_color <- viridis::scale_colour_viridis(
discrete = discrete, option = option,
begin = 0, end = 0.95, ...
)
}
return(custom_color)
}
#' Customized pretty ggplot fill theme
#'
#' @param fill Vector used for fill aesthetic. Should match
#' \code{ggplot2::aes(fill = ...)} argument.
#' @param viridis Logical. Whether or not to use viridis scheme if using
#' discrete fill scheme
#' @param option Argument indicating viridis palette name
#' @param drop Logical; whether or not to drop factors with no observations.
#' @param ... Other arguments to pass to scale_fill_manual() or
#' scale_fill_viridis()
#'
#' @return A ggplot color theme object.
#'
#' @examples
#' require(ggplot2)
#' ggplot(iris) +
#' aes(x = Sepal.Length, fill = Species) +
#' geom_density() +
#' prettyGGplotTheme() +
#' prettyGGplotFill(fill = iris$Species)
#'
prettyGGplotFill <- function(fill, viridis = F, option = "plasma",
drop = T, ...) {
discrete <- is.factor(fill)
if (discrete) {
if (nlevels(fill) <= 8 & viridis == FALSE) {
custom_palette <- RColorBrewer::brewer.pal(n = 8, name = "Dark2")
custom_palette[2] <- custom_palette[1]
custom_palette[1] <- "#FF9300"
custom_fill <- ggplot2::scale_fill_manual(values = custom_palette,
drop = drop, ...)
} else {
custom_fill <- viridis::scale_fill_viridis(
discrete = discrete, option = option,
begin = 0, end = 0.95, drop = drop, ...
)
}
} else {
custom_fill <- viridis::scale_fill_viridis(
discrete = discrete, option = option,
begin = 0, end = 0.95, ...
)
}
return(custom_fill)
}
#' Blank x-axis theme for ggplot objects.
#'
#' @return A ggplot theme object.
#'
#' @examples
#' require(ggplot2)
#' ggplot(iris) +
#' aes(x = Sepal.Length, fill = Species) +
#' geom_density() +
#' blank_x_theme()
#'
blank_x_theme <- function() {
theme(axis.line.x = ggplot2::element_blank(),
axis.title.x = ggplot2::element_blank(),
axis.text.x = ggplot2::element_blank(),
axis.ticks.x = ggplot2::element_blank(),
panel.grid.major.x = ggplot2::element_blank())
}
#' Blank y-axis theme for ggplot objects.
#'
#' @return A ggplot theme object.
#'
#' @examples
#' require(ggplot2)
#' ggplot(iris) +
#' aes(x = Sepal.Length, fill = Species) +
#' geom_density() +
#' blank_y_theme()
#'
blank_y_theme <- function() {
theme(axis.line.y = ggplot2::element_blank(),
axis.title.y = ggplot2::element_blank(),
axis.text.y = ggplot2::element_blank(),
axis.ticks.y = ggplot2::element_blank(),
panel.grid.major.y = ggplot2::element_blank())
}
#' Blank x- and y-axis theme for ggplot objects.
#'
#' @return A ggplot theme object.
#'
#' @examples
#' require(ggplot2)
#' ggplot(iris) +
#' aes(x = Sepal.Length, fill = Species) +
#' geom_density() +
#' blank_xy_theme()
#'
blank_xy_theme <- function() {
theme(axis.line = ggplot2::element_blank(),
axis.title = ggplot2::element_blank(),
axis.text = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
panel.grid.major = ggplot2::element_blank())
}