-
Notifications
You must be signed in to change notification settings - Fork 4
/
si_themes.R
206 lines (186 loc) · 6 KB
/
si_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
#' @title SI style plot with only y-axis gridlines
#' @description Incorporates the default si_style graph and modifies it to include on y-axis gridlines. Useful for when you are highlighting a horizontal comparison.
#' @param ... inherits parameters from [si_style()]
#' @export
#' @examples
#' \dontrun{
#' library(systemfonts)
#' library(ggplot2)
#' ggplot(iris, aes(Sepal.Length, y = Sepal.Width, colour = Species)) +
#' geom_point() + si_style_ygrid()
#'}
# Show only y axis lines
si_style_ygrid <- function(...) {
si_style(...) %+replace%
theme(
panel.grid.major.x = ggplot2::element_blank()
)
}
#' @title SI style plot with only x-axis gridlines
#' @description Incorporates the default si_style graph and modifies it to include on x-axis gridlines. Useful for when you are highlighting a vertical comparison, such as rotated bar graphs
#'
#' @export
#' @importFrom ggplot2 %+replace% aes geom_point theme
#' @param ... inherits parameters from [si_style()]
#' @examples
#' \dontrun{
#' library(systemfonts)
#' library(ggplot2)
#' ggplot(iris, aes(Sepal.Length, y = Sepal.Width, colour = Species)) +
#' geom_point() + si_style_xgrid()
#'}
# Show only x axis lines
si_style_xgrid <- function(...) {
si_style(...) %+replace%
theme(
panel.grid.major.y = ggplot2::element_blank()
)
}
#' @title SI style plot with blank canvass
#' @description Incorporates the default si_style graph and strips all gridlines. Useful for when you want to focus attention on a few parts of a plot -- such as a bar graph with fewer than four categories.
#'
#' @export
#' @importFrom ggplot2 %+replace% aes geom_point theme
#' @param ... inherits parameters from [si_style()]
#' @examples
#' \dontrun{
#' library(systemfonts)
#' library(ggplot2)
#' ggplot(iris, aes(Sepal.Length, y = Sepal.Width, colour = Species)) +
#' geom_point() + si_style_nolines()
#'}
# Show no axis lines
si_style_nolines <- function(...) {
si_style(...) %+replace%
theme(
panel.grid.major.y = ggplot2::element_blank(),
panel.grid.major.x = ggplot2::element_blank(),
)
}
#' @title SI style plot with x-axis line drawn
#' @description May be used in place of si_style_blank if you would like to anchor the x-axis.
#'
#' @export
#' @importFrom ggplot2 %+replace% aes geom_point theme element_line
#' @param ... inherits parameters from [si_style()]
#' @examples
#' \dontrun{
#' library(systemfonts)
#' library(ggplot2)
#' ggplot(iris, aes(Sepal.Length, y = Sepal.Width, colour = Species)) +
#' geom_point() + si_style_xline()
#'}
si_style_xline <- function(...) {
si_style_nolines(...) %+replace%
theme(
axis.line.x = element_line(colour = color_gridline)
)
}
#' @title SI style plot with y-axis line drawn
#' @description May be used in place of si_style_blank if you would like to anchor the y-axis.
#'
#' @export
#' @importFrom ggplot2 %+replace% aes geom_point theme element_line
#' @param ... inherits parameters from [si_style()]
#' @examples
#' \dontrun{
#' library(systemfonts)
#' library(ggplot2)
#' ggplot(iris, aes(Sepal.Length, y = Sepal.Width, colour = Species)) +
#' geom_point() + si_style_yline()
#'}
si_style_yline <- function(...) {
si_style_nolines(...) %+replace%
theme(
axis.line.y = element_line(colour = color_gridline)
)
}
#' @title Create Standard SI Plots showing only both x and y axis lines
#'
#' @export
#' @importFrom ggplot2 %+replace% aes geom_point theme element_line
#' @param ... inherits parameters from [si_style()]
#' @examples
#' \dontrun{
#' library(systemfonts)
#' library(ggplot2)
#' ggplot(iris, aes(Sepal.Length, y = Sepal.Width, colour = Species)) +
#' geom_point() + si_style_xyline()
#'}
#'
si_style_xyline <- function(...) {
si_style(...) %+replace%
theme(
axis.line = element_line(colour = color_gridline),
panel.grid.major.y = ggplot2::element_blank(),
panel.grid.major.x = ggplot2::element_blank(),
)
}
#' @title Create Standard SI Map
#'
#' @export
#' @importFrom ggplot2 %+replace%
#' @param ... inherits parameters from [si_style()]
#' @examples
#' \dontrun{
#'
#' library(systemfonts)
#' library(tidyverse)
#' library(glitr)
#' library(gisr)
#' library(sf)
#'
#' sfdf <- gisr::get_admin1("Nigeria") %>%
#' select(name) %>%
#' mutate(value = runif(nrow(.), 0, 1))
#'
#' ggplot() +
#' geom_sf(data = sfdf,
#' aes(fill = value),
#' color = grey10k,
#' size = .1) +
#' scale_fill_si(palette = "genoas",
#' discrete = FALSE,
#' limits = c(0, 1),
#' labels = scales::percent) +
#' labs(title = "NIGERIA - % OF PLHIV BY STATE",
#' subtitle = "States from XYZ Region are the most hit by HIV/AIDS",
#' caption = base::paste0("Produced by OHA/SIEI/SI, ON ", base::Sys.Date())) +
#' si_style_map() +
#' si_legend_fill()
#'}
#'
si_style_map <- function(...) {
ggplot2::theme_void() %+replace%
si_style(...) %+replace%
theme(
line = ggplot2::element_blank(),
rect = ggplot2::element_blank(),
axis.text.x = ggplot2::element_blank(),
axis.text.y = ggplot2::element_blank(),
)
}
#' @title Create ggplot with no axes or no grid lines
#'
#' @param ... inherits parameters from [si_style()]
#' @export
#' @importFrom ggplot2 %+replace% aes geom_point theme element_line
#' @examples
#' \dontrun{
#' library(systemfonts)
#' library(ggplot2)
#' ggplot(iris, aes(Sepal.Length, y = Sepal.Width, colour = Species)) +
#' geom_point() + si_style_void()
#'}
si_style_void <- function(...) {
si_style(...) %+replace%
theme(
axis.line = ggplot2::element_blank(),
panel.grid.major.y = ggplot2::element_blank(),
panel.grid.major.x = ggplot2::element_blank(),
axis.text.x = ggplot2::element_blank(),
axis.text.y = ggplot2::element_blank(),
axis.title = ggplot2::element_blank(),
legend.position = "none"
)
}