-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy-func.r
More file actions
55 lines (51 loc) · 1.46 KB
/
my-func.r
File metadata and controls
55 lines (51 loc) · 1.46 KB
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
#' function to plot sf object as map
#'
#' @param data sf dataset.
#' @param fill_col name of primary fill column; must be factor.
#' @return ggview object.
#' @examples
#' myPlotFun(ky_county_poly_join, life_expectancy_bin)
myPlotFun <- function(data, fill_col) {
gg <- ggplot(data) +
geom_sf(
aes(fill = {{fill_col}}),
color = "grey50",
linewidth = 0.75
) +
ggrepel::geom_label_repel(
data = myLabels,
aes(label = name2, geometry = geometry),
size = 2,
segment.color = "black",
stat = "sf_coordinates",
min.segment.length = 0
) +
scale_fill_brewer(
name = "Life Expectancy by County (Kentucky)",
palette = "GnBu",
direction = -1,
guide = guide_legend(
keyheight = unit(3, units = "mm"),
keywidth = unit(12, units = "mm"),
label.position = "bottom",
nrow = 1
)
) +
theme_void() +
theme(
plot.margin = margin(0, 1, 0, 1, "cm"),
plot.background = element_rect(fill = "white"),
legend.position = "inside",
legend.position.inside = c(0.075,0.85),
legend.box = "vertical",
legend.justification = "left",
legend.box.just = "left",
legend.box.spacing = unit(0.5, "lines"),
legend.spacing = unit(0.1, "lines")
) +
labs(caption = "Source: County Health Rankings & Roadmaps (CHR&R) 2025")
return(gg)
}
if (sys.nframe() == 0) {
myPlotFun(ky_county_poly_join, life_expectancy_pretty)
}