-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_area_bcps_programs.R
131 lines (127 loc) 路 3.21 KB
/
map_area_bcps_programs.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
#' Map BCPS programs and attendance zones for a local area
#'
#' Map showing BCPS school zones that overlap with a provided area or areas. If
#' the area sf tibble includes multiple areas, a separate map is created for
#' each area provided.
#'
#' @param area sf object
#' @examples
#' \dontrun{
#' ## Map school attendance boundary zones for the Madison Park neighborhood
#' madisonpark <- get_area(
#' area_type = "neighborhood",
#' area_name = "Madison Park"
#' )
#' map_area_bcps_programs(area = madisonpark)
#' }
#'
#' \dontrun{
#' ## Map school attendance boundary zones for City Council District 2
#' district9 <- get_area(
#' type = "council district",
#' area_name = "9"
#' )
#' map_area_bcps_programs(area = district9)
#' }
#' @export
#' @importFrom dplyr nest_by
#' @importFrom purrr map map2 pluck
map_area_bcps_programs <- function(area) {
check_installed(c("ggplot2", "ggrepel"))
area_nested <- dplyr::nest_by(area,
name,
.key = "area",
.keep = TRUE
)
area_nested$bcps_programs <- purrr::map(
area_nested$area,
~ get_area_bcps_programs(.x)
)
bcps_program_maps <- purrr::map2(
area_nested$area,
area_nested$bcps_programs,
~ ggplot2::ggplot() +
# Map BCPS attendance zones
ggplot2::geom_sf(
data = .y$zones,
ggplot2::aes(fill = program_name),
color = NA
) +
# Map BCPS school locations matched to zones
ggplot2::geom_sf(
data = .y$zoned_programs,
ggplot2::aes(fill = program_name),
shape = 21,
size = 4,
color = "white",
stroke = 1.5
) +
# Map BCPS locations in area (not zoned)
ggplot2::geom_sf(
data = .y$other_programs,
shape = 21,
size = 4,
fill = "gray60",
color = "white"
) +
# Map neighborhood boundary
ggplot2::geom_sf(
data = .x,
color = "white",
fill = NA,
alpha = 0.6,
size = 1.8
) +
# Map neighborhood boundary
ggplot2::geom_sf(
data = .x,
color = "gray20",
fill = NA,
linetype = 5
) +
# Label zoned schools
ggrepel::geom_label_repel(
data = .y$zoned_programs,
ggplot2::aes(
label = program_name,
fill = program_name,
geometry = geometry
),
stat = "sf_coordinates",
color = "white",
size = 4,
point.padding = 20,
min.segment.length = 0.25,
segment.color = "white",
label.size = 0.5,
label.padding = 0.5,
label.r = 0.05
) +
ggrepel::geom_label_repel(
data = .y$other_programs,
ggplot2::aes(
label = program_name,
geometry = geometry
),
stat = "sf_coordinates",
fill = "white",
color = "gray30",
size = 4,
point.padding = 20,
min.segment.length = 0.25,
segment.color = "white",
label.size = 0.5,
label.padding = 0.5,
label.r = 0.05
) +
ggplot2::guides(
fill = "none",
color = "none"
)
)
if (length(bcps_program_maps) == 1) {
return(purrr::pluck(bcps_program_maps, 1))
} else {
return(bcps_program_maps)
}
}