R/choroMap.R

Defines functions choroMap

Documented in choroMap

#' Basic interactive choropleth
#' 
#' This function can be use to quickly generate a choropleth
#' map from a \code{SpatialPolygonsDataFrame}.
#' 
#' @param x a \code{SpatialPolygonsDataFrame}.
#' @param col.by a column name of x giving values to color polygons.
#' @param breaks  a numeric vector of cut points or a single number of intervals.
#' @param alpha a numeric to control the transparency.
#' @param popup a character vector providing popup content.
#' @param col a vector of color usually generated by color palettes functions.
#' @param ... additional arguments which can be passed to \code{\link[rleafmap]{writeMap}}.
#' 
#' @export
choroMap <- function(x, col.by, breaks = 5, alpha = 1, col = NULL, popup = NULL, ...){
  
  if(!class(x) == "SpatialPolygonsDataFrame"){
    stop("x must be a SpatialPolygonsDataFrame")
  }
  
  z <- x[[col.by]]
  znum <- as.numeric(z)
  
  if (length(breaks) > 1){
    n <- length(breaks) - 1
  } else {
    n <- breaks
  }
  
  if(is.null(col)){
    gcol <- rev(heat.colors(n))
  } else {
    gcol <- col
  }
  gcut <- cut(znum, breaks = breaks)
  col <- gcol[as.numeric(gcut)]

  if(is.null(popup)){
    popup <- paste(col.by, znum, sep=": ")
  }
  
  bbm <- basemap("stamen.toner.lite")
  x.map <- spLayer(x, fill.col = col, fill.alpha = alpha, popup = popup)
  
  writeMap(bbm, x.map, ...)

}
Hackout2/epimap documentation built on May 6, 2019, 9:47 p.m.