Newer
Older
# Map census data
#'
#' @param census_data, dataframe of census data for specified variable of interest
#' @param conus_sf, sf of conus states outline
#' @param leg_title, character string for legend title
#' @param outfile_path, outfile path for pngs
#' @param viz_config_df `data.frame` width, height, counties outline color, conus outline color, background color, font nam, and font size
#' @param viz_config_pal `data.frame` assign colors for postively and negatively correlated dimensions for census maps

Azadpour, Elmera
committed
#' @param width, set figure width dimension
#' @param height, set figure height dimension

Azadpour, Elmera
committed
#' @param percent_leg if else statement where if TRUE, apply 0-100 legend, otherwise retain 0 - max of variable name

Azadpour, Elmera
committed
#' @param font_size, set font size
#' @param barheight, set colorbar bar height
#' @param barwidth, set colorbar bar width
#' @param mobile, if else statement where if TRUE, apply mobile cowplotting and sizing parameters

Azadpour, Elmera
committed
plot_census_map <- function(census_data, conus_sf, leg_title, outfile_path, var, percent_leg, viz_config_df, viz_config_pal, width, height, font_size,
barwidth, barheight, mobile){
font_legend <- viz_config_df$load_font
font_add_google(font_legend)
showtext_opts(dpi = 300, regular.wt = 200, bold.wt = 700)
showtext_auto(enable = TRUE)

Azadpour, Elmera
committed
census_map <- census_data |>
ggplot(aes(fill = .data[[var]])) +
geom_sf(color = viz_config_df$counties_outline_col,
linewidth = 0.05) +
geom_sf(data = conus_sf,
fill = NA,
color = viz_config_df$conus_outline_col,

Azadpour, Elmera
committed
theme(text = element_text(family = viz_config_df$font_legend, size = font_size),
legend.margin = margin(t = 5, b = 5), legend.position = 'bottom', legend.title.align = 0.5) +

Azadpour, Elmera
committed
title.theme = element_text(face = 'bold', family = viz_config_df$font_legend, size = font_size),
barwidth = barwidth,
barheight = barheight

Azadpour, Elmera
committed
))

Azadpour, Elmera
committed
if (percent_leg == FALSE) {
census_map <- census_map +
scale_fill_gradientn(
colors = colorRampPalette(c("#F8F9FF", viz_config_pal))(100),

Azadpour, Elmera
committed
name = leg_title,
limits = c(0, max(census_data[[var]], na.rm = TRUE)),

Azadpour, Elmera
committed
labels = scales::comma,

Azadpour, Elmera
committed
} else {
census_map <- census_map +
scale_fill_gradientn(
colors = colorRampPalette(c("#F8F9FF", viz_config_pal))(100),

Azadpour, Elmera
committed
name = leg_title,
limits = c(0, 100),

Azadpour, Elmera
committed
breaks = c(0, 25, 50, 75, 100),

Azadpour, Elmera
committed
)
}
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
census_legend <- get_legend(census_map)
background_color = "white"
plot_margin = 0.025
# cowplot to get map sizes larger
canvas <- grid::rectGrob(
x = 0, y = 0,
width = width, height = height,
gp = grid::gpar(fill = background_color, alpha = 1, col = background_color
)
)
if (mobile == FALSE) {
# compose final plot
final_map <- ggdraw(ylim = c(0,1),
xlim = c(0,1)) +
# White background
draw_grob(canvas,
x = 0, y = 1,
height = height, width = width,
hjust = 0, vjust = 1) +
# Add main plot
draw_plot(census_map + theme(legend.position="none"),
x = 0.14,
y = 0.1,
height = 0.98,
width = (1-plot_margin)*0.72) +
# Add legend
draw_plot(census_legend,
x = 0.48,
y = 0.06,
height = 0.09 ,
width = 0.1 - plot_margin)
} else {
final_map <- ggdraw(ylim = c(0,1),
xlim = c(0,1)) +
# White background
draw_grob(canvas,
x = 0, y = 1,
height = height, width = width,
hjust = 0, vjust = 1) +
# Add main plot
draw_plot(census_map + theme(legend.position="none"),
x = 0.01,
y = -0.01,
height = 1.2,
width = (1-plot_margin)) +
# Add legend
draw_plot(census_legend,
x = 0.48,
y = 0.06,
height = 0.09 ,
width = 0.1 - plot_margin)
}
ggsave(outfile_path, final_map, width = width, height = height, dpi = viz_config_df$dpi, bg = viz_config_df$bg_col, units = "in")