Skip to content

Commit

Permalink
finalizing cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosci committed Jan 22, 2019
1 parent 097de34 commit 5b272ca
Show file tree
Hide file tree
Showing 99 changed files with 1,832 additions and 415 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
inst/doc
.Rproj.user
.Rhistory
.RData
7 changes: 4 additions & 3 deletions DESCRIPTION
Expand Up @@ -41,13 +41,14 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 6.1.1
Imports:
classInt,
gridExtra,
dplyr,
ggplot2,
purrr,
raster,
tibble,
tidyr
Suggests:
testthat,
covr
covr,
knitr,
rmarkdown
VignetteBuilder: knitr
6 changes: 3 additions & 3 deletions R/data.R
Expand Up @@ -5,7 +5,7 @@
#'
#' @format A raster layer object.
#' @source Simulated neutral landscape models with R. \url{https://github.com/ropensci/NLMR/}
"fbmmap"
"fractal_landscape"

#' Example map (planar gradient).
#'
Expand All @@ -14,7 +14,7 @@
#'
#' @format A raster layer object.
#' @source Simulated neutral landscape models with R. \url{https://github.com/ropensci/NLMR/}
"grdmap"
"gradient_landscape"

#' Example map (random).
#'
Expand All @@ -23,4 +23,4 @@
#'
#' @format A raster layer object.
#' @source Simulated neutral landscape models with R. \url{https://github.com/ropensci/NLMR/}
"rndmap"
"random_landscape"
115 changes: 37 additions & 78 deletions R/show_landscape.R
Expand Up @@ -16,18 +16,18 @@
#'
#' @examples
#' \dontrun{
#' x <- grdmap
#' x <- gradient_landscape
#'
#' # classify
#' y <- util_classify(grdmap, n = 3, level_names = c("Land Use 1", "Land Use 2", "Land Use 3"))
#' y <- util_classify(gradient_landscape, n = 3, level_names = c("Land Use 1", "Land Use 2", "Land Use 3"))
#'
#' show_landscape(x)
#' show_landscape(y, discrete = TRUE)
#'
#' show_landscape(list(grdmap, rndmap))
#' show_landscape(raster::stack(grdmap, rndmap))
#' show_landscape(list(gradient_landscape, random_landscape))
#' show_landscape(raster::stack(gradient_landscape, random_landscape))
#'
#' show_landscape(list(grdmap, y), unique_scales = TRUE)
#' show_landscape(list(gradient_landscape, y), unique_scales = TRUE)
#'
#' }
#'
Expand Down Expand Up @@ -98,83 +98,42 @@ show_landscape.list <- function(x,
n_row = NULL,
...) {

if (!unique_scales){
x_tibble <- tibble::enframe(x, "id", "maps")
x_tibble <- dplyr::mutate(x_tibble,
maps = lapply(x_tibble$maps, util_raster2tibble))
x_tibble <- tidyr::unnest(x_tibble)

if(!discrete){
p <- ggplot2::ggplot(x_tibble, ggplot2::aes_string("x", "y")) +
ggplot2::coord_fixed() +
ggplot2::geom_raster(ggplot2::aes_string(fill = "z")) +
ggplot2::facet_wrap(~id, nrow = n_row, ncol = n_col) +
ggplot2::scale_x_continuous(expand = c(0, 0), limits = c(0,max(x_tibble$x))) +
ggplot2::scale_y_continuous(expand = c(0, 0), limits = c(0,max(x_tibble$y))) +
ggplot2::guides(fill = FALSE) +
ggplot2::labs(titel = NULL, x = NULL, y = NULL) +
theme_facetplot()
}

if(discrete){
p <- ggplot2::ggplot(x_tibble, ggplot2::aes_string("x", "y")) +
ggplot2::coord_fixed() +
ggplot2::geom_raster(ggplot2::aes(fill = factor(z))) +
ggplot2::facet_wrap(~id, nrow = n_row, ncol = n_col) +
ggplot2::scale_x_continuous(expand = c(0, 0), limits = c(0,max(x_tibble$x))) +
ggplot2::scale_y_continuous(expand = c(0, 0), limits = c(0,max(x_tibble$y))) +
ggplot2::guides(fill = FALSE) +
ggplot2::labs(titel = NULL, x = NULL, y = NULL) +
theme_facetplot_discrete()
}

x_tibble <- tibble::enframe(x, "id", "maps")
x_tibble <- dplyr::mutate(x_tibble,
maps = lapply(x_tibble$maps, function(x){

if(unique_scales) x <- util_rescale(x)
util_raster2tibble(x)

})
)
x_tibble <- tidyr::unnest(x_tibble)

if (!discrete) {
p <- ggplot2::ggplot(x_tibble, ggplot2::aes_string("x", "y")) +
ggplot2::coord_fixed() +
ggplot2::geom_raster(ggplot2::aes_string(fill = "z")) +
ggplot2::facet_wrap( ~ id, nrow = n_row, ncol = n_col) +
ggplot2::scale_x_continuous(expand = c(0, 0), limits = c(0, max(x_tibble$x))) +
ggplot2::scale_y_continuous(expand = c(0, 0), limits = c(0, max(x_tibble$y))) +
ggplot2::guides(fill = FALSE) +
ggplot2::labs(titel = NULL, x = NULL, y = NULL) +
theme_facetplot()
}

if (unique_scales){

landscape_plots <- lapply(seq_along(x), function(id){

x_tibble <- raster::as.data.frame(x[[id]], xy = TRUE)
names(x_tibble)[3] <- "z"
x_tibble$id <- names(x[[id]])


p <- tryCatch({
p <- ggplot2::ggplot(x_tibble, ggplot2::aes_string("x", "y")) +
ggplot2::coord_fixed() +
ggplot2::geom_raster(ggplot2::aes_string(fill = "z")) +
ggplot2::facet_wrap(~id, nrow = 1, ncol = 1) +
ggplot2::scale_x_continuous(expand = c(0, 0), limits = c(0,max(x_tibble$x))) +
ggplot2::scale_y_continuous(expand = c(0, 0), limits = c(0,max(x_tibble$y))) +
ggplot2::guides(fill = FALSE) +
ggplot2::labs(titel = NULL, x = NULL, y = NULL) +
theme_facetplot() +
ggplot2::theme(plot.margin = ggplot2::unit(c(0, 0, 0, 0), "cm")) +
theme_facetplot()
print(p)
},
error = function(e) {
ggplot2::ggplot(x_tibble, ggplot2::aes_string("x", "y")) +
ggplot2::coord_fixed() +
ggplot2::geom_raster(ggplot2::aes_string(fill = "z")) +
ggplot2::facet_wrap(~id, nrow = 1, ncol = 1) +
ggplot2::scale_x_continuous(expand = c(0, 0), limits = c(0,max(x_tibble$x))) +
ggplot2::scale_y_continuous(expand = c(0, 0), limits = c(0,max(x_tibble$y))) +
ggplot2::guides(fill = FALSE) +
ggplot2::labs(titel = NULL, x = NULL, y = NULL) +
theme_facetplot() +
ggplot2::theme(plot.margin = ggplot2::unit(c(0, 0, 0, 0), "cm")) +
theme_facetplot_discrete()
})

p

})

p <- gridExtra::grid.arrange(grobs = landscape_plots, nrow=n_row, ncol=n_col)

if (discrete) {
p <- ggplot2::ggplot(x_tibble, ggplot2::aes_string("x", "y")) +
ggplot2::coord_fixed() +
ggplot2::geom_raster(ggplot2::aes(fill = factor(z))) +
ggplot2::facet_wrap( ~ id, nrow = n_row, ncol = n_col) +
ggplot2::scale_x_continuous(expand = c(0, 0), limits = c(0, max(x_tibble$x))) +
ggplot2::scale_y_continuous(expand = c(0, 0), limits = c(0, max(x_tibble$y))) +
ggplot2::guides(fill = FALSE) +
ggplot2::labs(titel = NULL, x = NULL, y = NULL) +
theme_facetplot_discrete()
}


return(p)

}
Expand Down
2 changes: 1 addition & 1 deletion R/util_binarize.R
Expand Up @@ -13,7 +13,7 @@
#'
#' @examples
#' breaks <- c(0.3, 0.5)
#' binary_maps <- util_binarize(grdmap, breaks)
#' binary_maps <- util_binarize(gradient_landscape, breaks)
#'
#' @aliases util_binarize
#' @rdname util_binarize
Expand Down
20 changes: 10 additions & 10 deletions R/util_classify.R
Expand Up @@ -19,7 +19,7 @@
#'
#' @param x raster
#' @param n Number of classes
#' @param style Style of breaks (see classInt::classInvervals() for more details)
#' @param style chosen style: one of "fixed", "sd", "equal", "pretty", "quantile", "kmeans", "hclust", "bclust", "fisher", or "jenks" (see classInt::classInvervals() for more details)
#' @param weighting Vector of numeric values that are considered to be habitat percentages (see details)
#' @param level_names Vector of names for the factor levels.
#' @param real_land Raster with real landscape (see details)
Expand All @@ -29,33 +29,33 @@
#'
#' @examples
#' # Mode 1
#' util_classify(fbmmap,
#' util_classify(fractal_landscape,
#' n = 3,
#' style = "fisher",
#' level_names = c("Land Use 1", "Land Use 2", "Land Use 3"))
#'
#' # Mode 2
#' util_classify(fbmmap,
#' util_classify(fractal_landscape,
#' weighting = c(0.5, 0.25, 0.25),
#' level_names = c("Land Use 1", "Land Use 2", "Land Use 3"))
#'
#' # Mode 3
#' real_land <- util_classify(grdmap,
#' real_land <- util_classify(gradient_landscape,
#' n = 3,
#' level_names = c("Land Use 1", "Land Use 2", "Land Use 3"))
#'
#' fbmmap_real <- util_classify(fbmmap, real_land = real_land)
#' fbmmap_mask <- util_classify(fbmmap, real_land = real_land, mask_val = 1)
#' fractal_landscape_real <- util_classify(fractal_landscape, real_land = real_land)
#' fractal_landscape_mask <- util_classify(fractal_landscape, real_land = real_land, mask_val = 1)
#'
#' \dontrun{
#' landscapes <- list(
#' '1 nlm' = fbmmap,
#' '1 nlm' = fractal_landscape,
#' '2 real' = real_land,
#' '3 result' = fbmmap_real,
#' '4 result with mask' = fbmmap_mask
#' '3 result' = fractal_landscape_real,
#' '4 result with mask' = fractal_landscape_mask
#' )
#'
#' util_facetplot(landscapes, div_scales = TRUE, nrow = 1)
#' show_landscape(landscapes, unique_scales = TRUE, nrow = 1)
#' }
#'
#' @aliases util_classify
Expand Down
2 changes: 1 addition & 1 deletion R/util_merge.R
Expand Up @@ -10,7 +10,7 @@
#' @return Rectangular matrix with values ranging from 0-1
#'
#' @examples
#' x <- util_merge(grdmap, rndmap)
#' x <- util_merge(gradient_landscape, random_landscape)
#' show_landscape(x)
#'
#' @aliases util_merge
Expand Down
2 changes: 1 addition & 1 deletion R/util_raster2tibble.R
Expand Up @@ -10,7 +10,7 @@
#' @return a tibble
#'
#' @examples
#' maptib <- util_raster2tibble(fbmmap)
#' maptib <- util_raster2tibble(fractal_landscape)
#' \dontrun{
#' library(ggplot2)
#' ggplot(maptib, aes(x,y)) +
Expand Down
6 changes: 4 additions & 2 deletions R/util_rescale.R
Expand Up @@ -9,8 +9,8 @@
#' @details Rasters generated by \code{nlm_} functions are scaled between 0 and 1 as default, this option can be set to \code{FALSE} if needed.
#'
#' @examples
#' unscmap <- util_merge(grdmap, rndmap, rescale = FALSE)
#' util_rescale(unscmap)
#' unscaled_landscape <- gradient_landscape + fractal_landscape
#' util_rescale(unscaled_landscape)
#'
#' @aliases util_rescale
#' @rdname util_rescale
Expand All @@ -28,3 +28,5 @@ util_rescale <- function(x) {

return(rescaled_NLM)
}


4 changes: 2 additions & 2 deletions R/util_tibble2raster.R
Expand Up @@ -15,9 +15,9 @@
#' @return Raster* object
#'
#' @examples
#' maptib <- util_raster2tibble(rndmap)
#' maptib <- util_raster2tibble(random_landscape)
#' mapras <- util_tibble2raster(maptib)
#' all.equal(rndmap, mapras)
#' all.equal(random_landscape, mapras)
#'
#' @aliases util_tibble2raster
#' @rdname util_tibble2raster
Expand Down
2 changes: 1 addition & 1 deletion R/util_writeESRI.R
Expand Up @@ -21,7 +21,7 @@
#'
#' @examples
#' \dontrun{
#' util_writeESRI(grdmap, "grdmap.asc")
#' util_writeESRI(gradient_landscape, "gradient_landscape.asc")
#' }
#'
#' @aliases util_writeESRI
Expand Down
27 changes: 14 additions & 13 deletions README.Rmd
Expand Up @@ -30,21 +30,22 @@ Objects).

#### Utilities:

`util_binarize`: Binarize continuous raster values, if > 1 breaks are given, return a RasterBrick.<br/>
`util_classify`: Classify a raster into proportions based upon a vector of class weightings.<br/>
`util_merge`: Merge a primary raster with other rasters weighted by scaling factors.<br/>
`util_raster2tibble`, `util_tibble2raster`: Coerce raster* objects to tibbles and vice versa.<br/>
`util_rescale`: Linearly rescale element values in a raster to a range between 0 and 1.<br/>
- `util_binarize`: Binarize continuous raster values, if > 1 breaks are given, return a RasterBrick.
- `util_classify`: Classify a raster into proportions based upon a vector of class weightings.
- `util_merge`: Merge a primary raster with other rasters weighted by scaling factors.
- `util_raster2tibble`, `util_tibble2raster`: Coerce raster* objects to tibbles and vice versa.
- `util_rescale`: Linearly rescale element values in a raster to a range between 0 and 1.
- `util_writeESRI`: Export raster objects as ESRI asciis (with Windows linebreaks).

#### Visualization

`show_landscape`: Plot a Raster* object with the landscapetools default theme (as ggplot) or multiple raster (RasterStack, -brick or list of raster) side by side as facets.<br/>
- `show_landscape`: Plot a Raster* object with the landscapetools default theme (as ggplot) or multiple raster (RasterStack, -brick or list of raster) side by side as facets.

#### Themes:

`theme_nlm`, `theme_nlm_grey`: Opinionated ggplot2 theme to visualize raster (continuous data).<br/>
`theme_nlm_discrete`, `theme_nlm_grey_discrete`: Opinionated ggplot2 theme to visualize raster (discrete data).<br/>
`theme_faceplot`: Opinionated ggplot2 theme to visualize raster in a facet wrap.<br/>
- `theme_nlm`, `theme_nlm_grey`: Opinionated ggplot2 theme to visualize raster (continuous data).
- `theme_nlm_discrete`, `theme_nlm_grey_discrete`: Opinionated ggplot2 theme to visualize raster (discrete data).
- `theme_faceplot`: Opinionated ggplot2 theme to visualize raster in a facet wrap.

## Installation

Expand All @@ -71,15 +72,15 @@ show_landscape(nlm_raster)
### Binarize

```{r fig.retina=2}
# Binarize the map into habitat and matrix
# Binarize the landscape into habitat and matrix
binarized_raster <- util_binarize(nlm_raster, breaks = 0.31415)
show_landscape(binarized_raster)
```

### Classify

```{r fig.retina=2}
# Classify the map into land uses
# Classify the landscape into land uses
classified_raster <- util_classify(nlm_raster,
n = 3,
level_names = c("Land Use 1", "Land Use 2", "Land Use 3"))
Expand All @@ -89,15 +90,15 @@ show_landscape(classified_raster, discrete = TRUE)
### Merge

```{r fig.retina=2}
# Create a primary and two secondary maps
# Create a primary and two secondary landscapes
prim <- nlm_edgegradient(ncol = 100, nrow = 100)
sec1 <- nlm_distancegradient(ncol = 100, nrow = 100,
origin = c(10, 10, 10, 10))
sec2 <- nlm_random(ncol = 100, nrow = 100)
# Merge all maps into one
# Merge all landscapes into one
merg <- util_merge(prim, c(sec1, sec2), scalingfactor = 1)
# Plot an overview
Expand Down

0 comments on commit 5b272ca

Please sign in to comment.