R/fars_map_state.R

#' Create a FARS map by state and year
#'
#' This function will create a map of fatal accidents based on the \code{state.num}
#' and \code{year} arguments. An error will be thrown if an invalid state number
#' is provided. A message will also be printed if there are zero fatalities to
#' map.
#'
#' @param state.num An integer providing the function what state to use.
#' @param year An integer providing the function what year to use in the filename.
#'
#' @return This function returns a graphics map object.
#'
#' @examples
#' \dontrun{fars_map_state(1,2015)}
#'
#' @export
fars_map_state <- function(state.num, year) {
  filename <- make_filename(year)
  data <- fars_read(filename)
  state.num <- as.integer(state.num)

  if(!(state.num %in% unique(data$STATE)))
    stop("invalid STATE number: ", state.num)
  data.sub <- dplyr::filter(data, STATE == state.num)
  if(nrow(data.sub) == 0L) {
    message("no accidents to plot")
    return(invisible(NULL))
  }
  is.na(data.sub$LONGITUD) <- data.sub$LONGITUD > 900
  is.na(data.sub$LATITUDE) <- data.sub$LATITUDE > 90
  with(data.sub, {
    maps::map("state", ylim = range(LATITUDE, na.rm = TRUE),
              xlim = range(LONGITUD, na.rm = TRUE))
    graphics::points(LONGITUD, LATITUDE, pch = 46)
  })
}
ilikefishes/farspackage documentation built on May 13, 2019, 4:04 a.m.