-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0d8fc59
Showing
30 changed files
with
768 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
^README\.Rmd$ | ||
^README-.*\.png$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.Rproj.user | ||
.Rhistory | ||
.RData | ||
.Ruserdata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Package: brazilmaps | ||
Type: Package | ||
Title: Brazilian maps from diverse geographic levels | ||
Version: 0.1.0 | ||
Authors@R: person("Renato", "Prado Siqueira", email = "[email protected]", role = c("aut", "cre")) | ||
Maintainer: Renato Prado Siqueira <[email protected]> | ||
Description: Obtain Brazilian map spatial objects of varying region types (e.g. cities, | ||
states, microregions, mesoregions). Convenience functions for plotting | ||
choropleths and working with the geographic codes are also provided. | ||
LazyData: true | ||
License: GPL-3 | ||
Encoding: UTF-8 | ||
Imports: | ||
dplyr, | ||
ggplot2, | ||
magrittr, | ||
methods, | ||
sf, | ||
sp, | ||
RoxygenNote: 6.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(filter) | ||
export(filter.sf) | ||
export(get_brmap) | ||
export(join_data) | ||
export(plot_brmap) | ||
export(plot_sf) | ||
importFrom(dplyr,filter) | ||
importFrom(magrittr,"%>%") | ||
importFrom(methods,as) | ||
importFrom(sf,filter.sf) | ||
importFrom(sf,plot_sf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#' Population estimates (2017), city level | ||
#' | ||
#' @description IBGE's population estimates by municipality for 2017. \cr\cr | ||
#' The data is formatted for easy merging with output from \code{\link[brazilmaps]{get_brmap}}. | ||
#' | ||
#' @usage data(pop2017) | ||
#' | ||
#' @details | ||
#' \itemize{ | ||
#' \item \code{mun} The 7-digit code corresponding to the city. | ||
#' \item \code{nome_mun} The city name. | ||
#' \item \code{pop2017} The 2017 population estimate (in number of people) | ||
#' for the corresponding city | ||
#' } | ||
#' | ||
#' @name pop2017 | ||
#' @format A data frame with 5570 rows and 3 variables. | ||
#' @docType data | ||
#' @references | ||
#' \itemize{ | ||
#' \item \url{http://www.ibge.gov.br/home/estatistica/populacao/estimativa2017/default.shtm} | ||
#' \item \url{http://www.ibge.gov.br/home/estatistica/populacao/estimativa2017/estimativa_dou.shtm} | ||
#' } | ||
#' @keywords data | ||
"pop2017" | ||
|
||
|
||
#' Gini index (2015), state level | ||
#' | ||
#' @description IBGE's Gini index of the monthly income distribution of persons 15 years of age | ||
#' or over, with income for 2015. \cr\cr | ||
#' The data is formatted for easy merging with output from \code{\link[brazilmaps]{get_brmap}}. | ||
#' | ||
#' @usage data(gini2015) | ||
#' | ||
#' @details | ||
#' \itemize{ | ||
#' \item \code{cod} The 2-digit code corresponding to the state. | ||
#' \item \code{uf} The state name. | ||
#' \item \code{gini} The 2015 Gini Index | ||
#' } | ||
#' | ||
#' @name gini2015 | ||
#' @format A data frame with 27 rows and 3 variables. | ||
#' @docType data | ||
#' @references | ||
#' \itemize{ | ||
#' \item \url{http://www.ibge.gov.br/home/estatistica/pesquisas/pesquisa_resultados.php?id_pesquisa=40} | ||
#' } | ||
#' @keywords data | ||
"gini2015" | ||
|
||
|
||
#' Number of deaths (2015), microregion level | ||
#' | ||
#' @description DATASUS' registry of deaths from IBGE's brazilian microregions for 2015. \cr\cr | ||
#' The data is formatted for easy merging with output from \code{\link[brazilmaps]{get_brmap}}. | ||
#' | ||
#' @usage data(deaths) | ||
#' | ||
#' @details | ||
#' \itemize{ | ||
#' \item \code{cod} The 5-digit code corresponding to the microregion. | ||
#' \item \code{micro} The microregion name. | ||
#' \item \code{ndeaths} The 2015 absolut number of deaths | ||
#' } | ||
#' | ||
#' @name deaths | ||
#' @format A data frame with 580 rows and 3 variables. | ||
#' @docType data | ||
#' @references | ||
#' \itemize{ | ||
#' \item \url{http://datasus.saude.gov.br/informacoes-de-saude/tabnet} | ||
#' } | ||
#' @keywords data | ||
"deaths" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#' Get Brazilian maps from different geographic levels | ||
#' | ||
#' Turn available to manipulation | ||
#' brazilian maps in various type | ||
#' of geographic levels. The maps | ||
#' are from IBGE (Instituto Brasileiro | ||
#' de Geografia e Estatística) and | ||
#' refers to the administrative | ||
#' configurations of 2016. | ||
#' | ||
#' @usage get_brmap(geo = c("Brazil","Region","State","MesoRegion","MicroRegion","City"), | ||
#' geo.filter = NULL, | ||
#' class = c("sf", "SpatialPolygonsDataFrame", "data.frame")) | ||
#' @param geo A string value with geographic levels of interest | ||
#' @param geo.filter A (named) list object with the specific item of the | ||
#' geographic level or all itens of a determined higher geografic level | ||
#' @param class The class of the object to be returned | ||
#' @return The function returns a 'sf', 'SpatialPolygonsDataFrame' or 'data.frame' | ||
#' object depending of the 'class' argument informed | ||
#' @author Renato Prado Siqueira \email{<rpradosiqueira@@gmail.com>} | ||
#' @seealso \code{\link{join_data}} | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Retrieving the map from the State of Rio de Janeiro | ||
#' rio_map <- get_brmap(geo = "State", | ||
#' geo.filter = list(State = 33), | ||
#' class = "sf") | ||
#' } | ||
#' | ||
#' @keywords IBGE shapefile geographic levels spatial | ||
#' @importFrom methods as | ||
#' @importFrom magrittr %>% | ||
#' @export | ||
|
||
get_brmap <- function(geo = c("Brazil","Region","State","MesoRegion","MicroRegion","City"), | ||
geo.filter = NULL, | ||
class = c("sf", "SpatialPolygonsDataFrame", "data.frame")) { | ||
|
||
geo <- match.arg(geo) | ||
class <- match.arg(class) | ||
|
||
# geo.filter | ||
geo.filter.df <- data.frame(description = as.character(c("Brazil","Region","State","MesoRegion","MicroRegion","City")), | ||
level = c(0, 1, 2, 4, 5, 7)) | ||
|
||
geo1 <- merge(geo.filter.df, data.frame(description = names(geo.filter))) | ||
geo2 <- geo.filter.df[which(geo.filter.df$description == geo), ] | ||
|
||
if ( any(geo1$level > geo2$level) ) stop("The 'geo.filter' argument is misspecified") | ||
|
||
brmap <- base::readRDS(system.file("maps", paste0(geo, ".rds"), package = "brazilmaps")) | ||
|
||
if ( (!is.null(geo.filter)) & (geo %in% c("Region", "Brazil")) ) { | ||
|
||
warning("'geo.filter' argument will be assign to NULL when geo is 'Region' or 'Brazil'") | ||
geo.filter <- NULL | ||
|
||
} else if (!is.null(geo.filter)) { | ||
|
||
brmap_list <- list() | ||
|
||
for (i in 1:nrow(geo1)) { | ||
|
||
brmap_list[[i]] <- brmap %>% | ||
dplyr::filter( brmap[[(names(geo.filter)[i])]] %in% geo.filter[[i]] ) %>% | ||
sf::st_as_sf() | ||
|
||
} | ||
|
||
brmap <- do.call("rbind", brmap_list) | ||
|
||
} | ||
|
||
if (class == "SpatialPolygonsDataFrame") brmap <- as(brmap, "Spatial") | ||
if (class == "data.frame") brmap <- as(brmap, "Spatial") %>% ggplot2::fortify(region = geo) | ||
|
||
brmap | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#' @importFrom dplyr filter | ||
#' @export | ||
dplyr::filter | ||
#' @importFrom sf filter.sf | ||
#' @export | ||
sf::filter.sf | ||
#' @importFrom sf plot_sf | ||
#' @export | ||
sf::plot_sf | ||
#' @importClassesFrom sp SpatialPolygons | ||
#' @exportClass sp SpatialPolygons | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#' Join external data | ||
#' | ||
#' A wrapper around dplyr's join | ||
#' in order to facilitate the analysis | ||
#' on the maps from this package | ||
#' | ||
#' @usage join_data(map, data, by = NULL) | ||
#' @param map An object of class 'sf', 'SpatialPolygonsDataFrame' or 'data.frame' | ||
#' @param data A data.frame object with the data to join | ||
#' @param by A character vector of variables to join by. If NULL, the default, will do | ||
#' a natural join, using all variables with common names across the two tables. See | ||
#' dplyr's join to more information. | ||
#' @return The function returns a 'sf', 'SpatialPolygonsDataFrame' or 'data.frame' | ||
#' object depending of the class of the map argument informed | ||
#' @author Renato Prado Siqueira \email{<rpradosiqueira@@gmail.com>} | ||
#' @seealso \code{\link{get_brmap}} | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Joining population estimates data to the year of 2017 | ||
#' data("pop2017") | ||
#' municipios <- get_brmap(geo = "City", geo.filter = list(Region = 5), | ||
#' class = "SpatialPolygonsDataFrame") | ||
#' municipios <- join_data(municipios, pop2017, by = c("City" = "mun")) | ||
#' plot(municipios) | ||
#' } | ||
#' | ||
#' @keywords IBGE shapefile geographic levels spatial | ||
#' @importFrom magrittr %>% | ||
#' @export | ||
|
||
join_data <- function(map, data, by = NULL) { | ||
|
||
if (any(class(map) == "SpatialPolygonsDataFrame")) { | ||
|
||
map@data <- dplyr::left_join(map@data, data, by = by) | ||
|
||
map | ||
|
||
} else { | ||
|
||
data <- dplyr::left_join(map, data, by = by) %>% | ||
sf::st_as_sf() | ||
|
||
data | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#' Facilitated plot of brazilian maps | ||
#' | ||
#' A wrapper in order to facilitate the plot | ||
#' of the maps from this package. The function | ||
#' returns a ggplot object so it can be edited | ||
#' easily. | ||
#' | ||
#' @usage plot_brmap(map, data_to_join = data.frame(), join_by = NULL, | ||
#' var = "values", theme = theme_map()) | ||
#' @param map An object of class 'sf', 'SpatialPolygonsDataFrame' or 'data.frame' | ||
#' @param data_to_join A data frame containing values to plot on the map. | ||
#' @param join_by A character vector of variables to join by. | ||
#' @param var The name of the column that contains the values of the field to | ||
#' be plotted. The default is \code{"value"}. | ||
#' @param theme The theme that should be used for plotting the map. The default | ||
#' is \code{\link[ggthemes]{theme_map}}. | ||
#' @return A \code{\link[ggplot2]{ggplot}} object that contains a basic | ||
#' brazilian map with the described parameters. Since the result is a \code{ggplot} | ||
#' object, it can be extended with more \code{geom} layers, scales, labels, | ||
#' themes, etc. | ||
#' | ||
#' @seealso \code{\link{get_brmap}}, \code{\link[ggplot2]{theme}} | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' data("pop2017") | ||
#' map_sul <- get_brmap(geo = "City", geo.filter = list(Region = 4)) | ||
#' mapa1 <- plot_brmap(map_sul, | ||
#' data_to_join = pop2017, | ||
#' join_by = c("City" = "mun"), | ||
#' var = "values) | ||
#' mapa1 | ||
#' | ||
#' # Output is ggplot object so it can be extended | ||
#' # with any number of ggplot layers | ||
#' library(ggplot2) | ||
#' mapa1 + | ||
#' labs(title = "População Municipal 2017 - Região Sul") | ||
#' } | ||
#' | ||
#' @export | ||
|
||
plot_brmap <- function(map, | ||
data_to_join = data.frame(), | ||
join_by = NULL, | ||
var = "values", | ||
theme = theme_map()) { | ||
|
||
|
||
if (nrow(data_to_join) != 0) { | ||
|
||
map <- join_data(map = map, data = data_to_join, by = join_by) | ||
|
||
if (any(class(map) == "sf")) { | ||
|
||
map_ggplot <- as(map, "Spatial") | ||
map_ggplot@data$id = row.names(map_ggplot@data) | ||
|
||
map_ggplot_df <- suppressMessages( ggplot2::fortify(map_ggplot) ) | ||
map_ggplot_df <- dplyr::left_join(map_ggplot_df, map_ggplot@data, by = "id") | ||
|
||
} else if (class(map) == "SpatialPolygonsDataFrame") { | ||
|
||
map_ggplot <- map | ||
map_ggplot@data$id = row.names(map_ggplot@data) | ||
|
||
map_ggplot_df <- suppressMessages( ggplot2::fortify(map_ggplot) ) | ||
map_ggplot_df <- dplyr::left_join(map_ggplot_df, map_ggplot@data, by = "id") | ||
|
||
} | ||
|
||
ggplot2::ggplot() + | ||
ggplot2::geom_polygon(ggplot2::aes(fill = map_ggplot_df[[var]], x = map_ggplot_df$long, y = map_ggplot_df$lat, group = map_ggplot_df$group), | ||
data = map_ggplot_df, | ||
color = "black", | ||
size = 0.2) + | ||
ggplot2::coord_fixed() + | ||
ggplot2::labs(fill = var) + | ||
theme_map() | ||
|
||
} else { | ||
|
||
if (any(class(map) == "sf")) map <- as(map, "Spatial") %>% ggplot2::fortify() | ||
if (class(map) == "SpatialPolygonsDataFrame") map <- map %>% ggplot2::fortify() | ||
|
||
ggplot2::ggplot() + | ||
ggplot2::geom_polygon(ggplot2::aes(x = map$long, y = map$lat, group = map$group), | ||
data = map, | ||
color = "black", | ||
size = 0.2, | ||
fill = "white") + | ||
ggplot2::coord_fixed() + | ||
theme_map() | ||
|
||
} | ||
|
||
} | ||
|
||
#' This creates a nice map theme for use in plot_usmap. | ||
#' It is borrowed from the usmap package that borrowed from the | ||
#' ggthemes package located at this repository: | ||
#' https://github.com/jrnold/ggthemes | ||
#' | ||
#' This function was manually rewritten here to avoid the need for | ||
#' another package import. | ||
#' | ||
#' All theme functions (i.e. theme_bw, theme, element_blank, %+replace%) | ||
#' come from ggplot2. | ||
#' | ||
#' @keywords internal | ||
theme_map <- function(base_size = 9, base_family = "") { | ||
elementBlank = ggplot2::element_blank() | ||
`%+replace%` <- ggplot2::`%+replace%` | ||
unit <- ggplot2::unit | ||
|
||
ggplot2::theme_bw(base_size = base_size, base_family = base_family) %+replace% | ||
ggplot2::theme(axis.line = elementBlank, | ||
axis.text = elementBlank, | ||
axis.ticks = elementBlank, | ||
axis.title = elementBlank, | ||
panel.background = elementBlank, | ||
panel.border = elementBlank, | ||
panel.grid = elementBlank, | ||
panel.spacing = unit(0, "lines"), | ||
plot.background = elementBlank, | ||
legend.justification = c(0, 0), | ||
legend.position = c(0, 0)) | ||
} | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.