Newer
Older
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
# Map census data
#'
#' @param census_data, dataframe of census data for specified variable of interest
#' @param conus_sf, sf of conus states outline
#' @param pal, assign map palette
#' @param leg_title, character string for legend title
#' @param outfile_path, outfile path for pngs
#' @param width, set png width
#' @param height, set png height
#' @param dpi, set png dots per inches
#' @param counties_outline_col, assign color to counties outline
#' @param conus_outline_col, assign color to conus outline
#' @param bg_col, assign background color for maps
#' @param load_font, assign font name
plot_census_map <- function(census_data, conus_sf, pal, leg_title, outfile_path, width, height, dpi,
counties_outline_col, conus_outline_col, bg_col, load_font, font_size, var){
font_legend <- load_font
font_add_google(font_legend)
showtext_opts(dpi = 300, regular.wt = 200, bold.wt = 700)
showtext_auto(enable = TRUE)
census_map <- census_data |>
ggplot(aes(fill = .data[[var]])) +
geom_sf(color = counties_outline_col,
linewidth = 0.05) +
geom_sf(data = conus_sf,
fill = NA,
color = conus_outline_col,
linewidth = 0.2,
linetype = "solid") +
theme_void() +
scale_fill_distiller(
palette = pal,
direction = 1,
name = leg_title,
limits = c(0, max(census_data[[var]], na.rm = TRUE)),
labels = scales::comma) +
theme(text = element_text(family = font_legend, size = font_size),
legend.margin = margin(r = 10))
ggsave(outfile_path, census_map, width = width, height = height, dpi = dpi, bg = bg_col)
return(outfile_path)
}