R/nola_map.R

#' @importFrom magrittr %>%



#' @export
nola_basemap <- function(bounding_layer, source = 'stamen', maptype = 'toner') {

  box <- bounding_layer %>%
    sf::st_transform(4326) %>%
    sf::st_bbox()

  names(box) <- c('left', 'bottom', 'right', 'top')

  ggmap::get_map(location = box, source = source, maptype = maptype) %>%
    ggmap::ggmap()

}



#' @export
nola_heatmap <- function(point_layer, bounding_layer = point_layer,
                         source = 'stamen', maptype = 'toner', ...) {

  nolar::nola_basemap(bounding_layer, source, maptype) +
    ggplot2::stat_density_2d(data = sf::st_transform(point_layer, 4326),
                             mapping = ggplot2::aes(x = purrr::map_dbl(geometry, ~.[1]),
                                                    y = purrr::map_dbl(geometry, ~.[2]),
                                                    fill = stat(level)),
                             geom = 'polygon',
                             contour = TRUE,
                             alpha = 0.5,
                             ...) +
    ggplot2::scale_fill_viridis_c()

}



#' @export
nola_hexmap <- function(point_layer, bounding_layer = point_layer,
                        source = 'stamen', maptype = 'toner', ...) {

  nolar::nola_basemap(bounding_layer, source, maptype) +
    ggplot2::coord_cartesian() +
    ggplot2::geom_hex(data = sf::st_transform(point_layer, 4326),
                      mapping = ggplot2::aes(x = purrr::map_dbl(geometry, ~.[1]),
                                             y = purrr::map_dbl(geometry, ~.[2]),
                                             fill = stat(density)),
                      alpha = 0.5,
                      ...) +
    ggplot2::scale_fill_viridis_c()

}



#' @export
nola_points <- function(point_layer, ...) {

  ggplot2::geom_point(data = sf::st_transform(point_layer, 4326),
                      mapping = ggplot2::aes(x = purrr::map_dbl(geometry, ~.[1]),
                                             y = purrr::map_dbl(geometry, ~.[2])),
                      ...)

}
nola-r/nolar documentation built on May 9, 2019, 5:56 p.m.