From 18a0461fb7e4006429004b3b1da8a97002a4821b Mon Sep 17 00:00:00 2001 From: Kaysa Vaarre-Lamoureux <kvaarre-lamoureux@usgs.gov> Date: Tue, 8 Oct 2024 10:42:21 -0400 Subject: [PATCH] reduce repeat code and clean functions --- 2_process.R | 17 +- 2_process/src/data_utils.R | 32 ++-- 3_visualize.R | 335 ++++++++++++++++++----------------- 3_visualize/src/plot_utils.R | 208 +++++++++------------- 4 files changed, 276 insertions(+), 316 deletions(-) diff --git a/2_process.R b/2_process.R index af6c3b3..9a25092 100644 --- a/2_process.R +++ b/2_process.R @@ -243,26 +243,23 @@ tar_target(p2_census_acs5sub_disability_data, percent_rename = FALSE), pattern = map(p2_census_acs5_disability_layers), iteration = "list"), -# process population density raster data +# prep for raster data processing and plotting tar_target(p2_conus_sf, - fetch_conus_sf()), + fetch_conus_sf(states = p1_census_states)), tar_target(p2_conus_sf_proj, p2_conus_sf |> st_transform(p1_proj)), tar_target(p2_conus_inner, rmapshaper::ms_innerlines(p2_conus_sf_proj)), tar_target(p2_conus_counties_sf, - counties_sf <- tigris::counties(cb = TRUE, state = c('Washington', 'Oregon', 'California', 'Idaho', 'Nevada', - 'Utah', 'Arizona', 'Montana', 'Wyoming', 'Colorado', - 'New Mexico', 'North Dakota', 'South Dakota', 'Nebraska', 'Kansas', - 'Oklahoma', 'Texas', 'Minnesota', 'Iowa', 'Missouri', - 'Arkansas', 'Louisiana')) |> + counties_sf <- tigris::counties(cb = TRUE, state = p1_census_states) |> rmapshaper::ms_simplify(keep = 0.2) |> st_transform('EPSG:5070')), +# process population density raster data tar_target(p2_pop_density_processed, - process_pop_dens_raster(in_raster = p1_pop_density_raster_tif, #proj = p1_proj, - conus = p2_conus_sf, conus_proj = p2_conus_sf_proj, - outfile_path = "2_process/out/pop_density.tif"), + process_pop_dens(in_raster = p1_pop_density_raster_tif, + conus_sf = p2_conus_sf, conus_proj = p2_conus_sf_proj, + outfile_path = "2_process/out/pop_density.tif"), format = "file"), # process impervious surfaces raster data tar_target(p2_imp_surf_processed, diff --git a/2_process/src/data_utils.R b/2_process/src/data_utils.R index b3c5af4..e7aeed9 100644 --- a/2_process/src/data_utils.R +++ b/2_process/src/data_utils.R @@ -50,27 +50,27 @@ prep_tree_data <- function(data) { } ## data processing for population density raster map - -fetch_conus_sf <- function(){ +#' @title Fetch conus for raster processing +#' @param states, chr string with long name of states of interest to filter by +fetch_conus_sf <- function(states){ conus_sf <- tigris::states(cb = TRUE) |> - dplyr::filter(NAME %in% c('Washington', 'Oregon', 'California', 'Idaho', 'Nevada', - 'Utah', 'Arizona', 'Montana', 'Wyoming', 'Colorado', - 'New Mexico', 'North Dakota', 'South Dakota', 'Nebraska', 'Kansas', - 'Oklahoma', 'Texas', 'Minnesota', 'Iowa', 'Missouri', - 'Arkansas', 'Louisiana') + dplyr::filter(NAME %in% states ) |> rmapshaper::ms_simplify(keep = 0.2) } -#' @title Process raster data for ploting -#' @param -process_pop_dens_raster <- function(in_raster, conus, conus_proj, outfile_path){ - # in_raster = p1_pop_density_raster, proj = p1_proj, conus = p2_conus_sf, conus_proj = p2_conus_sf_proj +#' @title Process population density raster data for plotting +#' @param in_raster character string - .tif file path of raster data for specified variable of interest +#' @param conus_sf, sf of conus states outline +#' @param conus_proj, projected sf of conus states outline +#' @param outfile_path, outfile path for processed tifs +process_pop_dens <- function(in_raster, conus_sf, conus_proj, outfile_path){ + raw_data <- rast(in_raster) - # crop population data to area of interest - terra::window(raw_data) <- terra::ext(conus) + # crop population data to area of interest + terra::window(raw_data) <- terra::ext(conus_sf) # project population data conus_sf_rast <- rast(conus_proj, resolution = c(1000, 1000)) @@ -94,8 +94,12 @@ process_pop_dens_raster <- function(in_raster, conus, conus_proj, outfile_path){ return(outfile_path) } + +#' @title Process impervous surfaces raster data for plotting +#' @param in_raster character string - .tif file path of raster data for specified variable of interest +#' @param conus_proj, projected sf of conus states outline +#' @param outfile_path, outfile path for processed tifs process_imp_surf <- function(in_raster, conus_proj, outfile_path){ - # in_raster = p1_imp_surf_tip, conus_proj = p2_conus_sf_proj imp_surf_raw <- rast(in_raster) diff --git a/3_visualize.R b/3_visualize.R index d33ff93..15ce7ed 100644 --- a/3_visualize.R +++ b/3_visualize.R @@ -93,174 +93,183 @@ p3_targets <- list( format = "file" ), tar_target( - p3_perc_disable_png_en, - plot_census_map( - census_data = p2_census_acs5sub_disability_data[[1]], - percent_leg = TRUE, - dollar_leg = FALSE, - lim_vals = c(0, 50), - break_vals = c(0, 10, 20, 30, 40, 50), - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/perc_disabled_census_2022_en.png", - leg_title = "Percent disabled, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$demographic_characteristics, - width = p0_viz_config_df$width_desktop, - height = p0_viz_config_df$height_desktop, - font_size = p0_viz_config_df$font_size_desktop, - barwidth = 20, - barheight = 1, - low_ramp_col = "#eef0ff" + p3_perc_disable_png_en, + plot_census_map( + census_data = p2_census_acs5sub_disability_data[[1]], + percent_leg = TRUE, + dollar_leg = FALSE, + lim_vals = c(0, 50), + break_vals = c(0, 10, 20, 30, 40, 50), + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_disabled_census_2022_en.png", + leg_title = "Percent disabled, 2022", + viz_config_df = p0_viz_config_df, + viz_config_pal = p0_viz_config_pal$demographic_characteristics, + width = p0_viz_config_df$width_desktop, + height = p0_viz_config_df$height_desktop, + font_size = p0_viz_config_df$font_size_desktop, + barwidth = 20, + barheight = 1, + low_ramp_col = "#eef0ff" ), - format = "file" -), -# population density raster plot + format = "file" + ), + # population density raster plot tar_target( - p3_pop_density_plot, - plot_raster(in_raster = p2_pop_density_processed, - conus_sf = p2_conus_sf_proj, - viz_config_df = p0_viz_config_df, - conus_inner = p2_conus_inner, - viz_config_pal = "#38837a", - width = p0_viz_config_df$width_desktop, - height = p0_viz_config_df$height_desktop, - font_size = p0_viz_config_df$font_size_desktop, - barwidth = 20, - barheight = 1, - counties_sf = p2_conus_counties_sf, - outfile_path = "3_visualize/out/pop_density_rast_2020_en.png", - low_ramp_col = "#E8F4F4"), - format = "file"), -# impervious surfaces raster plot + p3_pop_density_png, + plot_raster(in_raster = p2_pop_density_processed, + conus_sf = p2_conus_sf_proj, + conus_inner = p2_conus_inner, + counties_sf = p2_conus_counties_sf, + viz_config_df = p0_viz_config_df, + viz_config_pal = "#38837a", + rast_type = "pop_density", + low_ramp_col = "#E8F4F4", + base_conus_fill = "#E8F4F4", + base_conus_color = "#E8F4F4", + border_color = p0_viz_config_df$counties_outline_col, + leg_title = expression(paste(bold("Population per 1 "), bold(km^2), bold(", 2020"))), + font_size = p0_viz_config_df$font_size_desktop, + barheight = 1, + barwidth = 20, + outfile_path = "3_visualize/out/pop_density_rast_2020_en.png", + width = p0_viz_config_df$width_desktop, + height = p0_viz_config_df$height_desktop), + format = "file"), + # impervious surfaces raster plot tar_target( - p3_imp_surf_plot, - plot_imp_surf_raster(in_raster = p2_imp_surf_processed, - conus_sf = p2_conus_sf_proj, - viz_config_df = p0_viz_config_df, - conus_col = "gray90", - conus_inner = p2_conus_inner, - viz_config_pal = "#38837a", - width = p0_viz_config_df$width_desktop, - height = p0_viz_config_df$height_desktop, - font_size = p0_viz_config_df$font_size_desktop, - barwidth = 20, - barheight = 1, - outfile_path = "3_visualize/out/imp_surface_rast_2022_en.png", - counties_sf = p2_conus_counties_sf, - low_ramp_col = "white"), - format = "file"), -# Spanish version's of maps ----------------------------------------------- -tar_target( - p3_med_income_png_es, - plot_census_map( - census_data = p2_perc_census_acs5_layers_sf[[2]], - lim_vals = c(0, 155000), - percent_leg = FALSE, - dollar_leg = TRUE, - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/med_income_census_2022_es.png", - leg_title = "Media de ingresos por hogar, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$socioeconomic_status, - width = p0_viz_config_df$width_desktop, - height = p0_viz_config_df$height_desktop, - font_size = p0_viz_config_df$font_size_desktop, - barwidth = 20, - barheight = 1, - low_ramp_col = "#fef1f1" - ), - format = "file" -), -tar_target( - p3_perc_latino_png_es, - plot_census_map( - census_data = p2_perc_census_acs5_layers_sf[[4]], - percent_leg = TRUE, - dollar_leg = FALSE, - lim_vals = c(0, 100), - break_vals = c(0, 25, 50, 75, 100), - var = 'percent', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/perc_hispanic_census_2022_es.png", - leg_title = "Porcentaje de Hispanos, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$demographic_characteristics, - width = p0_viz_config_df$width_desktop, - height = p0_viz_config_df$height_desktop, - font_size = p0_viz_config_df$font_size_desktop, - barwidth = 20, - barheight = 1, - low_ramp_col = "#eef0ff" + p3_imp_surf_png, + plot_raster(in_raster = p2_imp_surf_processed, + conus_sf = p2_conus_sf_proj, + conus_inner = p2_conus_inner, + counties_sf = p2_conus_counties_sf, + viz_config_df = p0_viz_config_df, + viz_config_pal = "#38837a", + rast_type = "imp_surfaces", + low_ramp_col = "white", + base_conus_fill = NA, + base_conus_color = "#E8F4F4", + border_color = "gray90", + leg_title = "Percent impervious surface, 2022", + font_size = p0_viz_config_df$font_size_desktop, + barheight = 1, + barwidth = 20, + outfile_path = "3_visualize/out/imp_surface_rast_2022_en.png", + width = p0_viz_config_df$width_desktop, + height = p0_viz_config_df$height_desktop), + format = "file"), + # Spanish version's of maps ----------------------------------------------- + tar_target( + p3_med_income_png_es, + plot_census_map( + census_data = p2_perc_census_acs5_layers_sf[[2]], + lim_vals = c(0, 155000), + percent_leg = FALSE, + dollar_leg = TRUE, + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/med_income_census_2022_es.png", + leg_title = "Media de ingresos por hogar, 2022", + viz_config_df = p0_viz_config_df, + viz_config_pal = p0_viz_config_pal$socioeconomic_status, + width = p0_viz_config_df$width_desktop, + height = p0_viz_config_df$height_desktop, + font_size = p0_viz_config_df$font_size_desktop, + barwidth = 20, + barheight = 1, + low_ramp_col = "#fef1f1" + ), + format = "file" ), - format = "file" -), -tar_target( - p3_avg_household_size_png_es, - plot_census_map( - census_data = p2_census_acs5sub_household_data[[1]], - percent_leg = FALSE, - dollar_leg = FALSE, - lim_vals = c(1, 5), - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/avg_household_size_2022_es.png", - leg_title = "Tamaño promedio de los hogares, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$demographic_characteristics, - width = p0_viz_config_df$width_desktop, - height = p0_viz_config_df$height_desktop, - font_size = p0_viz_config_df$font_size_desktop, - barwidth = 20, - barheight = 1, - low_ramp_col = "#eef0ff" + tar_target( + p3_perc_latino_png_es, + plot_census_map( + census_data = p2_perc_census_acs5_layers_sf[[4]], + percent_leg = TRUE, + dollar_leg = FALSE, + lim_vals = c(0, 100), + break_vals = c(0, 25, 50, 75, 100), + var = 'percent', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_hispanic_census_2022_es.png", + leg_title = "Porcentaje de Hispanos, 2022", + viz_config_df = p0_viz_config_df, + viz_config_pal = p0_viz_config_pal$demographic_characteristics, + width = p0_viz_config_df$width_desktop, + height = p0_viz_config_df$height_desktop, + font_size = p0_viz_config_df$font_size_desktop, + barwidth = 20, + barheight = 1, + low_ramp_col = "#eef0ff" + ), + format = "file" ), - format = "file" -), -tar_target( - p3_median_rent_png_es, - plot_census_map( - census_data = p2_census_acs5sub_household_data[[2]], - percent_leg = FALSE, - dollar_leg = TRUE, - lim_vals = c(0, 3000), - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/median_rent_2022_es.png", - leg_title = "Alquiler bruto medio, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$land_tenure, - width = p0_viz_config_df$width_desktop, - height = p0_viz_config_df$height_desktop, - font_size = p0_viz_config_df$font_size_desktop, - barwidth = 20, - barheight = 1, - low_ramp_col = "#ebfaf8" + tar_target( + p3_avg_household_size_png_es, + plot_census_map( + census_data = p2_census_acs5sub_household_data[[1]], + percent_leg = FALSE, + dollar_leg = FALSE, + lim_vals = c(1, 5), + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/avg_household_size_2022_es.png", + leg_title = "Tamaño promedio de los hogares, 2022", + viz_config_df = p0_viz_config_df, + viz_config_pal = p0_viz_config_pal$demographic_characteristics, + width = p0_viz_config_df$width_desktop, + height = p0_viz_config_df$height_desktop, + font_size = p0_viz_config_df$font_size_desktop, + barwidth = 20, + barheight = 1, + low_ramp_col = "#eef0ff" + ), + format = "file" ), - format = "file" -), -tar_target( - p3_perc_disable_png_es, - plot_census_map( - census_data = p2_census_acs5sub_disability_data[[1]], - percent_leg = TRUE, - dollar_leg = FALSE, - lim_vals = c(0, 50), - break_vals = c(0, 10, 20, 30, 40, 50), - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/perc_disabled_census_2022_es.png", - leg_title = "Porcentaje de discapacitados, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$demographic_characteristics, - width = p0_viz_config_df$width_desktop, - height = p0_viz_config_df$height_desktop, - font_size = p0_viz_config_df$font_size_desktop, - barwidth = 20, - barheight = 1, - low_ramp_col = "#eef0ff" + tar_target( + p3_median_rent_png_es, + plot_census_map( + census_data = p2_census_acs5sub_household_data[[2]], + percent_leg = FALSE, + dollar_leg = TRUE, + lim_vals = c(0, 3000), + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/median_rent_2022_es.png", + leg_title = "Alquiler bruto medio, 2022", + viz_config_df = p0_viz_config_df, + viz_config_pal = p0_viz_config_pal$land_tenure, + width = p0_viz_config_df$width_desktop, + height = p0_viz_config_df$height_desktop, + font_size = p0_viz_config_df$font_size_desktop, + barwidth = 20, + barheight = 1, + low_ramp_col = "#ebfaf8" + ), + format = "file" ), - format = "file" -) + tar_target( + p3_perc_disable_png_es, + plot_census_map( + census_data = p2_census_acs5sub_disability_data[[1]], + percent_leg = TRUE, + dollar_leg = FALSE, + lim_vals = c(0, 50), + break_vals = c(0, 10, 20, 30, 40, 50), + var = 'estimate', + conus_sf = p1_conus_sf, + outfile_path = "3_visualize/out/perc_disabled_census_2022_es.png", + leg_title = "Porcentaje de discapacitados, 2022", + viz_config_df = p0_viz_config_df, + viz_config_pal = p0_viz_config_pal$demographic_characteristics, + width = p0_viz_config_df$width_desktop, + height = p0_viz_config_df$height_desktop, + font_size = p0_viz_config_df$font_size_desktop, + barwidth = 20, + barheight = 1, + low_ramp_col = "#eef0ff" + ), + format = "file" + ) ) \ No newline at end of file diff --git a/3_visualize/src/plot_utils.R b/3_visualize/src/plot_utils.R index e534ea1..fb311d1 100644 --- a/3_visualize/src/plot_utils.R +++ b/3_visualize/src/plot_utils.R @@ -110,16 +110,30 @@ ggsave(outfile_path, final_map, width = width, height = height, dpi = viz_config # Map raster data #' -#' @param raster_data .tif file of raster data for specified variable of interest -#' # notes: update colorpalette according to Elmera's request - -plot_raster <- function(in_raster, conus_sf, conus_inner, outfile_path, - viz_config_df, viz_config_pal, width, height, - font_size, barwidth, barheight, counties_sf, - low_ramp_col){ - # in_raster = p2_pop_density_processed, conus_sf = p2_conus_sf_proj, conus_inner = p2_conus_inner, viz_config_pal = p0_viz_config_pal$living_conditions - # width = p0_viz_config_df$width_desktop, height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, barheight = 1, - #outfile_path = "3_visualize/out/pop_density_rast_2020_en.png", low_ramp_col = "#eef0ff", +#' @param in_raster character string - .tif file path of raster data for specified variable of interest +#' @param conus_sf, sf of conus states outline +#' @param conus_inner, sf of conus states outline not including US borders +#' @param counties_sf, sf of counties outline +#' @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 +#' @param rast_type character string - choice of "pop_density" or "imp_surfaces" to indicate which map is being made +#' @param low_ramp_col, color for low end of color ramp +#' @param base_conus_fill, base fill used for conus background +#' @param base_conus_color, color used for state outlines +#' @param border_color, color used for inner state outlines and county outlines +#' @param leg_title, title to be used for the legend +#' @param font_size, set font size +#' @param barheight, set colorbar bar height +#' @param barwidth, set colorbar bar width +#' @param outfile_path, outfile path for pngs +#' @param width, set figure width dimension +#' @param height, set figure height dimension +plot_raster <- function(in_raster, conus_sf, conus_inner, counties_sf, + viz_config_df, viz_config_pal, rast_type, low_ramp_col, + base_conus_fill, base_conus_color, border_color, + leg_title, font_size, barheight, barwidth, outfile_path, + width, height){ + raster_data <- rast(in_raster) # plot @@ -128,127 +142,53 @@ plot_raster <- function(in_raster, conus_sf, conus_inner, outfile_path, showtext_opts(dpi = 300, regular.wt = 200, bold.wt = 700) showtext_auto(enable = TRUE) - # doesn't work with cowplot::get_plot_component() - legend_label <- expression(paste(bold("Population per 1 "), bold(km^2), bold(", 2020"))) - - (pop_map <- ggplot() + - #geom_spatraster(data = pop_usa_proj)+ - geom_sf(data = conus_sf, fill = low_ramp_col, color = low_ramp_col, linewidth = 0.5) + - geom_spatraster(data = raster_data)+ #x = x, y = y, - geom_sf(data = conus_inner, fill = NA, color = viz_config_df$counties_outline_col, linewidth = 0.5) + - geom_sf(data = counties_sf, fill = NA, color = viz_config_df$counties_outline_col, linewidth = 0.1) + + rast_map <- ggplot() + + geom_sf(data = conus_sf, fill = base_conus_fill, color = base_conus_color, linewidth = 0.9) + + geom_spatraster(data = raster_data)+ + geom_sf(data = conus_inner, fill = NA, color = border_color, linewidth = 0.5) + + geom_sf(data = counties_sf, fill = NA, color = border_color, linewidth = 0.1) + + labs(fill = leg_title) + + theme(plot.background = element_rect(fill = "white", color = "white"), + panel.background = element_rect(fill = "white", color = "white"), + text = element_text(family = viz_config_df$font_legend, size = font_size), + axis.title = element_blank(), + axis.text = element_blank(), + axis.ticks = element_blank(), + panel.grid = element_blank(), + legend.background = element_blank(), + legend.direction = "horizontal", + legend.margin = margin(t = 5, b = 5), + legend.position = "bottom", + legend.title.align = 0.5) + + guides(fill = guide_colorbar( + title.position = "top", + title.theme = element_text(face = 'bold', family = font_legend, size = font_size), + direction = "horizontal", + position = "bottom", + barwidth = barwidth, + barheight = barheight + )) + + if(rast_type == "pop_density"){ + rast_map <- rast_map + scale_fill_gradientn( colors = colorRampPalette(c(low_ramp_col, viz_config_pal))(100), - name = legend_label, + name = leg_title, labels = c(1, 10, 100, '1k', '10k', '100k'), na.value=NA - )+ - labs(fill = legend_label) + - theme(plot.background = element_rect(fill = "white", color = "white"), - panel.background = element_rect(fill = "white", color = "white"), - axis.title = element_blank(), - axis.text = element_blank(), - axis.ticks = element_blank(), - panel.grid = element_blank(), - legend.background = element_blank(), - legend.direction = "horizontal", - legend.position = "bottom", - legend.title.align = 0.5) + - guides(fill = guide_colorbar( - title.position = "top", - title.theme = element_text(face = 'bold', family = font_legend, size = font_size), - direction = "horizontal", - position = "bottom", - barwidth = barwidth, - barheight = barheight - )) - ) - - background_color = "white" - plot_margin = 0.025 - - # cowplot to get map sizes larger - canvas <- grid::rectGrob( - x = 0, y = 0, - width = 6, height = 6, - gp = grid::gpar(fill = background_color, alpha = 1, col = background_color - ) - ) - pop_map_legend <- get_plot_component(pop_map, 'guide-box', return_all = TRUE) - # compose final plot - final_map <- ggdraw(ylim = c(0,1), - xlim = c(0,1)) + - # White background - draw_grob(canvas, - x = 0, y = 1, - height = 6, width = 6, - hjust = 0, vjust = 1) + - # Add main plot - draw_plot(pop_map + theme(legend.position="none"), - x = -0.01, - y = 0.08, - height = 0.98, - width = (1-plot_margin)*1.03) + - # Add legend - draw_plot(pop_map_legend[[3]], - x = 0.48, - y = 0.02, - 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") - -} - - -plot_imp_surf_raster <- function(in_raster, conus_sf, conus_inner, conus_col, outfile_path, - viz_config_df, viz_config_pal, width, height, - font_size, barwidth, barheight, counties_sf, - low_ramp_col){ - # in_raster = p2_imp_surf_processed, conus_sf = p2_conus_sf_proj, conus_col = "gray90", viz_config_pal = p0_viz_config_pal$living_conditions - # width = p0_viz_config_df$width_desktop, height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, barheight = 1, - #outfile_path = "3_visualize/out/imp_surface_rast_2020_en.png", low_ramp_col = "white" - raster_data <- rast(in_raster) - - # plot - 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) - - (pop_map <- ggplot() + - geom_sf(data = conus_sf, fill = NA, color = conus_col, linewidth = 0.9) + - geom_spatraster(data = raster_data)+ - geom_sf(data = conus_inner, fill = NA, color = conus_col, linewidth = 0.5) + - geom_sf(data = counties_sf, fill = NA, color = conus_col, linewidth = 0.1) + + ) + + } else if(rast_type == "imp_surfaces") { + rast_map <- rast_map + scale_fill_gradientn( colors = colorRampPalette(c(low_ramp_col, viz_config_pal))(100), - name = "Percent impervious surface, 2022", + name = leg_title, limits = c(0, 100), breaks = c(0, 25, 50, 75, 100), labels = c("0%", "25%", "50%", "75%", "100%"), na.value=NA - )+ - labs(fill = "% Impervious Surface, 2022") + - theme(plot.background = element_rect(fill = "white", color = "white"), - panel.background = element_rect(fill = "white", color = "white"), - axis.title = element_blank(), - axis.text = element_blank(), - axis.ticks = element_blank(), - panel.grid = element_blank(), - legend.background = element_blank(), - legend.direction = "horizontal", - legend.position = "bottom", - legend.title.align = 0.5) + - guides(fill = guide_colorbar( - title.position = "top", - title.theme = element_text(face = 'bold', family = font_legend, size = font_size), - direction = "horizontal", - position = "bottom", - barwidth = barwidth, - barheight = barheight - )) - ) + ) + } background_color = "white" plot_margin = 0.025 @@ -260,7 +200,7 @@ plot_imp_surf_raster <- function(in_raster, conus_sf, conus_inner, conus_col, ou gp = grid::gpar(fill = background_color, alpha = 1, col = background_color ) ) - pop_map_legend <- get_plot_component(pop_map, 'guide-box', return_all = TRUE) + rast_map_legend <- get_plot_component(rast_map, 'guide-box', return_all = TRUE) # compose final plot final_map <- ggdraw(ylim = c(0,1), xlim = c(0,1)) + @@ -270,17 +210,27 @@ plot_imp_surf_raster <- function(in_raster, conus_sf, conus_inner, conus_col, ou height = 6, width = 6, hjust = 0, vjust = 1) + # Add main plot - draw_plot(pop_map + theme(legend.position="none"), + draw_plot(rast_map + theme(legend.position="none"), x = -0.01, y = 0.08, height = 0.98, - width = (1-plot_margin)*1.03) + + width = (1-plot_margin)*1.03) # Add legend - draw_plot(pop_map_legend[[3]], - x = 0.48, - y = 0.02, - height = 0.09 , - width = 0.1 - plot_margin) + + final_map <- final_map + + if(rast_type == "pop_density"){ + draw_plot(rast_map_legend[[3]], + x = 0.48, + y = 0.03, + height = 0.09 , + width = 0.1 - plot_margin) + } else if(rast_type == "imp_surfaces") { + draw_plot(rast_map_legend[[3]], + x = 0.48, + y = 0.02, + 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") } \ No newline at end of file -- GitLab