-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_map_theme.R
54 lines (51 loc) 路 1.43 KB
/
set_map_theme.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
#' Set default map theme
#'
#' Set a map theme using [ggplot2::theme_set()] and default for `geom_label`
#' using [ggplot2::update_geom_defaults()].
#' Optionally hides axis text and labels.
#'
#' @param map_theme ggplot2 theme. Optional. Defaults to [ggplot2::theme_minimal()]
#' @param show_axis Logical. If TRUE, keep theme axis formatting. If FALSE, hide the panel grid, axis title, and axis text.
#' @export
set_map_theme <- function(map_theme = NULL,
show_axis = FALSE) {
check_installed("ggplot2")
if (is.null(map_theme)) {
# Set minimal theme
ggplot2::theme_set(
ggplot2::theme_minimal(base_size = 14)
)
} else {
(
ggplot2::theme_set(
map_theme
)
)
}
if (!show_axis) {
ggplot2::theme_update(
# Remove lat/lon grid
panel.grid.major = ggplot2::element_line(color = "transparent"),
# Remove lat/lon axis text
axis.title = ggplot2::element_text(color = "transparent"),
# Remove numeric labels on lat/lon axis ticks
axis.text = ggplot2::element_text(color = "transparent")
)
}
# Match font family for label and label_repel to theme font family
ggplot2::update_geom_defaults(
"label",
list(
color = "grey20",
family = ggplot2::theme_get()$text$family
)
)
# Set fill and color for geom_sf to NA by default
ggplot2::update_geom_defaults(
"sf",
list(
color = NA,
fill = NA
)
)
}