R/obtn_county_map.R

Defines functions obtn_county_map

#' Title
#'
#' @param county_to_filter
#' @param year_to_filter
#'
#' @return
#' @export
#'
#' @examples
obtn_county_map <- function(year_to_filter = 2022, county_to_filter) {

  set.seed(year_to_filter)

  obtn_notable_features_filtered <- obtn_notable_features %>%
    tidyr::drop_na(long) %>%
    dplyr::filter(geography == county_to_filter)

  obtn_county_seats_filtered <- obtn_county_seats %>%
    dplyr::filter(geography == county_to_filter) %>%
    dplyr::filter(year == year_to_filter)

  obtn_boundaries_oregon_counties_filtered <- obtn_boundaries_oregon_counties %>%
    dplyr::filter(geography == county_to_filter)

  obtn_largest_community_filtered <- obtn_largest_community %>%
    dplyr::filter(geography == county_to_filter) %>%
    dplyr::filter(year == year_to_filter)

  get_text_data <- function(df, text_label_var, font_weight) {
    df %>%
      dplyr::select({{ text_label_var }}, long, lat) %>%
      purrr::set_names("text_label", "long", "lat") %>%
      dplyr::mutate(font_weight = font_weight) %>%
      dplyr::mutate(point_type = text_label_var) %>%
      dplyr::mutate(star_type = dplyr::case_when(
        point_type == "largest_community" ~ 15,
        point_type == "county_seat" ~ 1,
        TRUE ~ 15
      ))
  }


  point_data <- get_text_data(obtn_largest_community_filtered, "largest_community", "bold") %>%
    dplyr::bind_rows(get_text_data(obtn_county_seats_filtered, "county_seat", "bold")) %>%
    dplyr::bind_rows(get_text_data(obtn_notable_features_filtered, "notable_feature", "plain")) %>%
    dplyr::mutate()

  # print(point_data)

  text_labels <- point_data %>%
    dplyr::add_count(text_label) %>%
    dplyr::mutate(keep_row = dplyr::case_when(
      n == 2 & point_type == "largest_community" ~ NA_character_,
      TRUE ~ "Y"
    )) %>%
    tidyr::drop_na(keep_row)


  plot_dimensions <- tibble::tribble(
    ~geography, ~width, ~height,
    "Baker",  3.627,  2.4764,
    "Benton",  3.627,  2.4764,
    "Clackamas",  3.627,  2.4764,
    "Clatsop", 3.9897,   2.724,
    "Columbia", 3.9897,   2.724,
    "Coos", 4.3887,  2.9964,
    "Crook", 3.5464,  2.4213,
    "Curry", 1.9058,  4.3903,
    "Deschutes", 3.5464,  1.7794,
    "Douglas", 3.5464,  2.4213,
    "Gilliam", 3.8688,  2.6415,
    "Grant", 3.8688,  2.6415,
    "Harney",    1.8,  4.1467,
    "Hood River", 3.8688,  2.6415,
    "Jackson",   4.03,  2.7515,
    "Jefferson", 3.5464,  1.7794,
    "Josephine", 3.8688,  2.6415,
    "Klamath",   1.98,  4.5613,
    "Lake",  3.901,  2.6635,
    "Lane",  3.224,  1.6177,
    "Lincoln",  1.575,  3.6283,
    "Linn",  3.224,  2.2012,
    "Malheur",    1.5,  3.4556,
    "Marion",  3.224,  2.2012,
    "Morrow",    1.8,  4.1467,
    "Multnomah", 3.1667,  1.5889,
    "Polk", 3.1667,  2.1621,
    "Sherman",  2.145,  4.9414,
    "Tillamook", 2.0625,  4.7514,
    "Umatilla", 3.5444,    2.42,
    "Union", 3.8667,    2.64,
    "Wallowa", 3.8667,    2.64,
    "Wasco", 3.8667,    2.64,
    "Washington", 3.5444,    2.42,
    "Wheeler", 3.8667,    2.64,
    "Yamhill", 3.5444,  1.7784
  ) %>%
    dplyr::filter(geography == county_to_filter)

  if (plot_dimensions$width < 2.5) {
    text_wrap_width <- 12
  } else {
    text_wrap_width <- 16
  }

  plot <- ggplot2::ggplot() +
    ggplot2::geom_sf(data = obtn_boundaries_oregon_counties_filtered,
                     fill = tfff_light_green,
                     color = "transparent") +
    ggstar::geom_star(data = point_data,
                      ggplot2::aes(x = long, y = lat,
                                   starshape = star_type,
                                   size = point_type,
                                   fill = point_type,
                                   color = point_type),
                      # position = ggplot2::position_jitter(width = 0.05, height = 0.05)
    ) +
    ggrepel::geom_text_repel(data = text_labels,
                             ggplot2::aes(x = long, y = lat,
                                          fontface = font_weight,
                                          label = stringr::str_wrap(text_label, text_wrap_width)),
                             size = 3.5,
                             lineheight = 0.75,
                             box.padding = 0.5,
                             segment.colour = "transparent",
                             # color = tfff_dark_gray,
                             # fontface = "bold",
                             family = "Calibri") +
    ggplot2::scale_fill_manual(values = c(
      "largest_community" = "white",
      "county_seat" = tfff_yellow,
      "notable_feature" = tfff_dark_green
    )) +
    ggplot2::scale_color_manual(values = c(
      "largest_community" = tfff_dark_green,
      "county_seat" = tfff_dark_green,
      "notable_feature" = "white"
    )) +
    ggplot2::scale_size_manual(values = c(
      "largest_community" = 4,
      "county_seat" = 3,
      "notable_feature" = 3
    )) +
    ggplot2::theme_void() +
    ggplot2::theme(legend.position = "none")

  plot

  obtn_save_plot(2022, "County Map", county_to_filter, plot_dimensions$width, plot_dimensions$height)

  plot

}


# purrr::pwalk(list(2022, obtn_oregon_counties), obtn_county_map)
rfortherestofus/obtn documentation built on Nov. 13, 2022, 7:25 p.m.