R/ggswissmaps_00funs.R

Defines functions theme_white_f maps2_

Documented in maps2_ theme_white_f

#' theme_white_f is a 'ggplot2' theme function that can be added to a 'ggplot2'
#' object to eliminate axes, ticks and put white background
#' 
#' @param base_size base font size
#' @param base_family base font family
#' @export
theme_white_f <- function(base_size = 12, base_family = ""){
  ggplot2::theme_bw(base_size = base_size, base_family = base_family) %+replace%
    ggplot2::theme(
      legend.background = ggplot2::element_blank(),
      legend.key = ggplot2::element_blank(),
      panel.background = ggplot2::element_blank(),
      panel.border = ggplot2::element_blank(),
      #  strip.background = element_blank(),
      plot.background = ggplot2::element_blank(),
      axis.title = ggplot2::element_blank(),
      axis.text = ggplot2::element_blank(),
      axis.ticks = ggplot2::element_blank(),
      panel.grid = ggplot2::element_blank()
    )
}



#' Prepares the base of a map, starting from a data frame with longitude (long)
#' and latitude (lat) coordinates, as a 'ggplot2' object
#' 
#' @param data data frame with longitude, latitude and group coordinates of a territory (polygons)
#' @param mapping Aesthetic mappings, as character strings (\code{link{ggplot2::aes_string}}). Defaults are \code{x = "long"}, \code{y = "lat"} and \code{group = "group"} (these work with every element of the list \code{shp_df} of ggswissmaps)
#' @param caption text to include in the lower right corner of the map (default: "Boundaries: BFS GEOSTAT / swisstopo")
#' 
#'  
#' @examples 
#' data(shp_df)
#' maps2_(data = shp_df[[1]])
#' @export
maps2_ <- 
  function(data,
           mapping = 
             ggplot2::aes_string(x = "long",
                                 y = "lat",
                                 group = "group"),
           caption = "Boundaries: BFS GEOSTAT / swisstopo"){
    ggplot2::ggplot(data = data, mapping = mapping) +
    ggplot2::geom_path() +
    ggplot2::coord_equal() +
      theme_white_f() +
      ggplot2::labs(x = NULL, y = NULL, 
                    caption = caption)
}

# data(shp_df)
# maps2_(data = shp_df[[1]])
# maps2 <- lapply(shp_df, maps2_)
gibonet/ggswissmaps documentation built on Feb. 2, 2020, 2:21 a.m.