#' An R6 Class to download, view, and save a map based on desired coordinates.
#' @docType class
#' @name MapStamen
#' @usage MapStamen(left, bottom, right, top, mapType, Zoom)
#'
#' @description MapStamen is an object generator as R6 class. The returnd object is a map
#' which has been downloaded from mapStamen API.
#'
#' @details MapStamen generates an object as R6 class. The returnd object is a map
#' which has been downloaded from mapStamen API. The object
#' will be constructed by passing the lower left and upperright coordinates of the
#' area of interest. Type of map and the requiered zoom can
#' also be specified as the arguments.#'
#'
#'
#' @section Public Methods:
#' \describe{
#' \item{\code{initializ(left, bottom, right, top =, mapType, Zoom)}}{This method will be initialized the class generator as the costructor is called. A new object will be generated.}
#' \item{\code{showMap()}}{This method displays the downloaded map.}
#' \item{\code{saveMap()}}{This method saves a png file in the working directory.}
#' \item{\code{ClGoodday()}}{This method display the No. of healthy days in California in 2019.}
#' \item{\code{ClModday()}}{This method display the No. of moderate days in California in 2019.}
#' \item{\code{ClUnhealthyDay()}}{This method display the No. of unhealthy days in California in 2019.}
#' }
#'
#' @section Private Methods:
#' \describe{
#' \item{\code{getMap()}}{This method dowloads the map from Stamen API and will be implemented in initialize method.}
#'
#' }
#'
#' @seealso http://maps.stamen.com
#'
#' @return A MapStamen class object generated by R6 class "MapStamen".
#' @format \code{\link{R6Class}} object.
#' @examples myMap = MapStamen$new(left = 86.05, bottom = 27.21,
#' right = 87.81, top = 28.76, mapType = "toner-lite", Zoom = 5 )
#'
#' @import R6
#' @import ggmap
#' @import ggplot2
#' @export
#'
MapStamen <- R6Class("MapStamen",
public = list(
#' @param left The lowerleft longitude of the area-box to be downloaded.
#' @param bottom The lowerleft latitude of the area-box to be downloaded.
#' @param right The upperright longitude of the area-box to be downloaded.
#' @param top The upperright latitude of the area-box to be downloaded.
#' @param mapType A string indicating the type of map("terrain", "toner",...)
#' @param Zoom zoom level
initialize = function(left = -124.409591, bottom = 32.534156,
right = -114.131211, top = 42.009518,
mapType = "terrain", Zoom = 9){
#Returning error and stop executing as the arguments are not the correct type.
stopifnot(is.numeric(left), length(left) == 1)
stopifnot(is.numeric(bottom), length(bottom) == 1)
stopifnot(is.numeric(right), length(right) == 1)
stopifnot(is.numeric(top), length(top) == 1)
stopifnot(is.numeric(Zoom), length(Zoom) == 1)
stopifnot(is.character(mapType))
# Assigning the arguments to the private fields
private$.left <- left
private$.bottom <- bottom
private$.right <- right
private$.top <- top
private$.mapType <- mapType
private$.zoom <- Zoom
# Downloading the map
private$getMap()
},
# Method to display the downloaded map
showMap = function(){
longitude = c(private$.left, private$.right)
latitude = c(private$.bottom, private$.top)
sites = data.frame(Longitude = longitude, Latitude = latitude)
ggmap(private$.myMap, extent = "device") +
geom_point(data = sites, aes(x = Longitude, y = Latitude),
size = 1, shape=19)
},
#Method to save the downloaded map as a png file.
saveMap = function(){
self$showMap()
ggsave(filename="myMap.png",
width=6, height=6, units = "in")
},
ClGoodday = function() {
data("Ca_2019")
ggmap(private$.myMap, extent = "device") +
geom_point(aes(x = longi, y = latit, size = GoodDays),
data = Ca_2019, color = "gray", fill = "Green", stroke = 1, alpha = 0.75, shape = 21) +
scale_size(range = c(2,18)) +
theme(legend.position="right")
},
ClModday = function(){
data("Ca_2019")
ggmap(private$.myMap, extent = "device") +
geom_point(aes(x = longi, y = latit, size = ModerateDays),
data = Ca_2019, color = "gray", fill = "Orange", stroke = 1, alpha = 0.75, shape = 21) +
scale_size(range = c(2,18)) +
theme(legend.position="right")
},
ClUnhealthyDay = function(){
data("Ca_2019")
ggmap(private$.myMap, extent = "device") +
geom_point(aes(x = longi, y = latit, size = UnhealthyDays),
data = Ca_2019, color = "gray", fill = "Red", stroke = 1, alpha = 0.75, shape = 21) +
scale_size(range = c(2,18)) +
theme(legend.position="right")
}
),
private = list(
.left = NA,
.bottom = NA,
.right = NA,
.top = NA,
.zoom = NA,
.mapType = NA,
.myMap = NA,
# Get method to download the map from mapStamen API.
getMap = function(){
private$.myMap <- get_stamenmap(c(private$.left, private$.bottom,
private$.right , private$.top ),
maptype = private$.mapType,
crop = TRUE, zoom = private$.zoom)
self
}
)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.