From 223feacea7a459857e0093cc4e17f4a4b50be1ac Mon Sep 17 00:00:00 2001 From: elmeraa <eazadpour@usgs.gov> Date: Fri, 31 May 2024 11:16:46 -0700 Subject: [PATCH] edit sizing, hispanic and disabled legend titles, and slight color edit for total maps --- 0_config.R | 4 +- 3_visualize.R | 220 ++++------------------------------- 3_visualize/src/plot_utils.R | 37 ++---- 3 files changed, 30 insertions(+), 231 deletions(-) diff --git a/0_config.R b/0_config.R index d6d2ce0..4afd892 100644 --- a/0_config.R +++ b/0_config.R @@ -3,10 +3,10 @@ p0_targets <- list( # Create a tibble to define color palette, fig width and height, county and conus outline colors, background color, font name, and font size p0_viz_config_df, tibble( - width_desktop = 8, + width_desktop = 6, height_desktop = 6, width_mobile = 6, - height_mobile = 8, + height_mobile = 6, dpi = 300, counties_outline_col = "white", conus_outline_col = 'white', diff --git a/3_visualize.R b/3_visualize.R index 69a89bb..ca1405d 100644 --- a/3_visualize.R +++ b/3_visualize.R @@ -4,7 +4,7 @@ source('3_visualize/src/plot_utils.R') # For case studies approach: select one demographic characteristic (hispanic/latino), p3_targets <- list( -# desktop maps ------------------------------------------------------------ +# desktop & mobile maps ------------------------------------------------------------ tar_target( p3_med_income_png, plot_census_map( @@ -20,8 +20,7 @@ p3_targets <- list( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -40,8 +39,7 @@ p3_targets <- list( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -53,16 +51,15 @@ p3_targets <- list( percent_leg = FALSE, var = 'estimate', conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/tot_latino_census_2022.png", - leg_title = "Total Latino, 2022", + outfile_path = "3_visualize/out/tot_hispanic_census_2022.png", + leg_title = "Total Hispanic, 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, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -74,16 +71,15 @@ p3_targets <- list( percent_leg = TRUE, var = 'percent', conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/perc_latino_census_2022.png", - leg_title = "Percent Latino, 2022", + outfile_path = "3_visualize/out/perc_hispaic_census_2022.png", + leg_title = "Percent Hispanic, 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, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -105,8 +101,7 @@ p3_targets <- list( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -125,8 +120,7 @@ p3_targets <- list( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -145,8 +139,7 @@ p3_targets <- list( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -167,8 +160,7 @@ tar_target( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -188,8 +180,7 @@ tar_target( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -209,8 +200,7 @@ tar_target( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -230,8 +220,7 @@ tar_target( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -251,8 +240,7 @@ tar_target( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -272,8 +260,7 @@ tar_target( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -287,15 +274,14 @@ tar_target( var = 'estimate', conus_sf = p1_conus_sf, outfile_path = "3_visualize/out/perc_disable_census_2022.png", - leg_title = "Percent disable, 2022", + 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, - mobile = FALSE + barheight = 1 ), format = "file" ), @@ -315,172 +301,8 @@ tar_target( height = p0_viz_config_df$height_desktop, font_size = p0_viz_config_df$font_size_desktop, barwidth = 20, - barheight = 1, - mobile = FALSE - ), - format = "file" -), - -# mobile maps ------------------------------------------------------------- - tar_target( - p3_med_income_mobile_png, - plot_census_map( - census_data = p2_perc_census_acs5_layers_sf[[2]], - percent_leg = FALSE, - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/med_income_census_2022_mobile.png", - leg_title = "Median household income, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$socioeconomic_status, - width = p0_viz_config_df$width_mobile, - height = p0_viz_config_df$height_mobile, - font_size = p0_viz_config_df$font_size_mobile, - barwidth = 20, - barheight = 1, - mobile = TRUE - ), - format = "file" - ), - tar_target( - p3_perc_household_income_mobile_png, - plot_census_map( - census_data = p2_census_acs5sub_income_data[[1]], - percent_leg = TRUE, - var = 'percent', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/perc_household_income_2022_mobile.png", - leg_title = "Percent allocated household income\nin the past 12 months, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$socioeconomic_status, - width = p0_viz_config_df$width_mobile, - height = p0_viz_config_df$height_mobile, - font_size = p0_viz_config_df$font_size_mobile, - barwidth = 20, - barheight = 1, - mobile = TRUE + barheight = 1 ), - format = "file" - ), - tar_target( - p3_tot_latino_mobile_png, - plot_census_map( - census_data = p2_perc_census_acs5_layers_sf[[4]], - percent_leg = FALSE, - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/tot_latino_census_2022_mobile.png", - leg_title = "Total Latino, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$demographic_characteristics, - width = p0_viz_config_df$width_mobile, - height = p0_viz_config_df$height_mobile, - font_size = p0_viz_config_df$font_size_mobile, - barwidth = 20, - barheight = 1, - mobile = TRUE - ), - format = "file" - ), - - tar_target( - p3_perc_latino_mobile_png, - plot_census_map( - census_data = p2_perc_census_acs5_layers_sf[[4]], - percent_leg = TRUE, - var = 'percent', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/perc_latino_census_2022_mobile.png", - leg_title = "Percent Latino, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$demographic_characteristics, - width = p0_viz_config_df$width_mobile, - height = p0_viz_config_df$height_mobile, - font_size = p0_viz_config_df$font_size_mobile, - barwidth = 20, - barheight = 1, - mobile = TRUE - ), - format = "file" - ), - tar_target( - p3_total_households_mobile_png, - plot_census_map( - census_data = p2_census_acs5sub_household_data[[1]], - percent_leg = FALSE, - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/tot_households_2022_mobile.png", - leg_title = "Total households, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$demographic_characteristics, - width = p0_viz_config_df$width_mobile, - height = p0_viz_config_df$height_mobile, - font_size = p0_viz_config_df$font_size_mobile, - barwidth = 20, - barheight = 1, - mobile = TRUE - ), - format = "file" - ), - tar_target( - p3_female_households_mobile_png, - plot_census_map( - census_data = p2_census_acs5sub_household_data[[2]], - percent_leg = FALSE, - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/tot_female_households_2022_mobile.png", - leg_title = "Total female households, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$demographic_characteristics, - width = p0_viz_config_df$width_mobile, - height = p0_viz_config_df$height_mobile, - font_size = p0_viz_config_df$font_size_mobile, - barwidth = 20, - barheight = 1, - mobile = TRUE - ), - format = "file" - ), - tar_target( - p3_median_rent_mobile_png, - plot_census_map( - census_data = p2_census_acs5sub_household_data[[3]], - percent_leg = FALSE, - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/median_rent_2022_mobile.png", - leg_title = "Median gross rent, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$socioeconomic_status, - width = p0_viz_config_df$width_mobile, - height = p0_viz_config_df$height_mobile, - font_size = p0_viz_config_df$font_size_mobile, - barwidth = 20, - barheight = 1, - mobile = TRUE - ), - format = "file" - ), -tar_target( - p3_disable_mobile_png, - plot_census_map( - census_data = p2_census_acs5sub_disability_data[[1]], - percent_leg = TRUE, - var = 'estimate', - conus_sf = p1_conus_sf, - outfile_path = "3_visualize/out/perc_disable_2022_mobile.png", - leg_title = "Total disable, 2022", - viz_config_df = p0_viz_config_df, - viz_config_pal = p0_viz_config_pal$demographic_characteristics, - width = p0_viz_config_df$width_mobile, - height = p0_viz_config_df$height_mobile, - font_size = p0_viz_config_df$font_size_mobile, - barwidth = 20, - barheight = 1, - mobile = TRUE - ), 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 8ca35b5..311f876 100644 --- a/3_visualize/src/plot_utils.R +++ b/3_visualize/src/plot_utils.R @@ -12,9 +12,8 @@ #' @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 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){ + barwidth, barheight){ font_legend <- viz_config_df$load_font font_add_google(font_legend) @@ -45,7 +44,7 @@ plot_census_map <- function(census_data, conus_sf, leg_title, outfile_path, var, if (percent_leg == FALSE) { census_map <- census_map + scale_fill_gradientn( - colors = colorRampPalette(c("#F8F9FF", viz_config_pal))(100), + colors = colorRampPalette(c("#eef0ff", viz_config_pal))(100), name = leg_title, limits = c(0, max(census_data[[var]], na.rm = TRUE)), labels = scales::comma, @@ -74,7 +73,6 @@ plot_census_map <- function(census_data, conus_sf, leg_title, outfile_path, var, ) ) - if (mobile == FALSE) { # compose final plot final_map <- ggdraw(ylim = c(0,1), xlim = c(0,1)) + @@ -85,40 +83,19 @@ plot_census_map <- function(census_data, conus_sf, leg_title, outfile_path, var, hjust = 0, vjust = 1) + # Add main plot draw_plot(census_map + theme(legend.position="none"), - x = 0.14, - y = 0.1, + x = -0.01, + y = 0.08, height = 0.98, - width = (1-plot_margin)*0.72) + + width = (1-plot_margin)*1.03) + # Add legend draw_plot(census_legend, x = 0.48, - y = 0.06, + y = 0.02, 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") +ggsave(outfile_path, final_map, width = width, height = height, dpi = viz_config_df$dpi, bg = viz_config_df$bg_col, units = "in") return(outfile_path) } \ No newline at end of file -- GitLab