-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_area_highlighted.R
62 lines (57 loc) 路 1.96 KB
/
map_area_highlighted.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
#' Maps a highlighted area within the context of multiple areas
#'
#' Map highlighting the location of an area the context of multiple areas.
#'
#' @param area Required sf object with a 'name' column.
#' @param highlight_name Character vector. Required. Use "all" to create a grid
#' of maps highlighting each area in the provided sf object or provide the
#' name of one or more areas to highlight.
#' @export
#' @importFrom dplyr filter
#' @importFrom sf st_union
map_area_highlighted <- function(area,
highlight_name = "all") {
check_installed("ggplot2")
if (length(area$geometry) == 1) {
warning("map_area_highlighted is designed to work with multiple areas")
}
area_map_highlighted <- ggplot2::ggplot()
if (highlight_name == "all") {
area_map_highlighted <- area_map_highlighted +
ggplot2::geom_sf(
data = area,
ggplot2::aes(fill = name),
color = NA
) +
ggplot2::facet_wrap(~name) +
ggplot2::guides(fill = "none")
} else if (is.character(highlight_name) && (length(highlight_name) == 1)) {
area_map_highlighted <- area_map_highlighted +
ggplot2::geom_sf(
data = dplyr::filter(area, name == highlight_name),
ggplot2::aes(fill = name),
color = NA
) +
ggplot2::guides(fill = "none") +
ggplot2::labs(title = highlight_name)
} else if (is.character(highlight_name) && (length(highlight_name) > 1)) {
area_map_highlighted <- area_map_highlighted +
ggplot2::geom_sf(
data = dplyr::filter(area, name %in% highlight_name),
ggplot2::aes(fill = name),
color = NA
) +
ggplot2::facet_wrap(
~name
) +
ggplot2::theme(strip.text.x = ggplot2::element_text(size = 10)) +
ggplot2::guides(fill = "none")
}
area_map_highlighted <- area_map_highlighted +
ggplot2::geom_sf(
data = sf::st_union(area),
color = "gray30",
fill = NA
)
return(area_map_highlighted)
}