R/fars_map_state.R

#'Plot State map
#'
#' Function for plotting an states accidentes map for given a state & year
#'
#' @param state.num an integer value, describing the the stateid of the data set which is needed. The valid states which can be used are between \code{1 - 56}.
#' However,there no data about stats id's: \code{3, 7, 14, 43 & 52.}
#'
#' @param year an integer value, describing the year of the data set which is needed. The valid years which can be used are:\code{ 2013, 2014 & 2015}.
#'
#' @details This function using the packages: \bold{readr}, \bold{dplyr}, \bold{maps}.
#'
#' If an invalid year will be supplied to the function then show an error message.
#'
#' If an invalid state id will be supplid the the function show tn error message.
#'
#' The bounderies of longitud & latitude are below 900 & 90 respectivly.
#'
#' @return a ggplot object
#'
#' @importFrom dplyr filter
#' @importFrom maps map
#' @importFrom graphics points
#'
#' @examples
#' \dontrun{fars_map_state(1,2013)}
#
#'@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)
  })
}
AlphaIgor/Test documentation built on May 19, 2019, 10:47 p.m.