visualize_nlms.Rmd
As the ever growing R package environment can be a rough terrain to navigate and find the appropriate tools to achieve one’s goals, this vignette is meant to point out some ways to overcome initial problems with visualizing neutral landscape models or more general raster data. This is probably a heavily biased view on packages and functions and I am sure there are other good R packages out there to achieve the same (if so - feel free to point that out to me and I will include it!). However, I am also sure this collection can at least be a kickstart for quickly visualizing your results and help you to communicate them.
landscapetools function show_landscape
was developed to help users to adhere to some standards concerning color scales and typography. This means for example that by default the viridis color scale is applied which makes your plots easier to read by those with colorblindness.
library("NLMR")
library("landscapetools")
landscape <- nlm_mosaictess(200, 200, germs = 444)
# default theme
show_landscape(landscape)
# ... chose another color scale from viridis ("E" = cividis)
show_landscape(landscape, viridis_scale = "E")
# ... chose any other scale:
# show_landscape returns a ggplot2 object, so you can follow your usual ggplot2
# workflow and change the color, axis labels, ...
library(ggplot2)
library(pals)
show_landscape(landscape) +
scale_fill_gradientn(colours=pals::parula(100)) + # parula color scale
theme_void() + # minimal theme
guides(fill = FALSE) # remove legend
rasterVis also offers some convenience functions to plot raster, for example:
library("NLMR")
library("rasterVis")
landscape <- nlm_mosaictess(200, 200, germs = 444)
levelplot(landscape, , margin = FALSE)
Another nice function from rasterVis is gplot()
, a wrapper to use ggplot2 with raster data without reshaping your data as long data.frame:
library("highcharter")
library("magrittr")
library("plotly")
library("NLMR")
# create a NLM to work with
landscape <- nlm_mosaicfield(ncol = 100, nrow = 100, n = 20)
# coerce to matrix
landscape_matrix <- raster::as.matrix(landscape)
# plot interactive graph
hchart(landscape_matrix) %>%
# changing default color
hc_colorAxis(stops = color_stops(colors = viridis::inferno(10))) %>%
hc_exporting(
enabled = TRUE
)
library(rayshader)
library(NLMR)
library(raster)
library(rgl)
set.seed(123)
landscape <- nlm_mpd(1000, 1000, roughness = 0.6, rescale = FALSE) * 500
landscape <- raster::focal(landscape, w=matrix(1, 31, 31), mean, pad = TRUE, padValue=0)
landscape <- raster::as.matrix(landscape)
shadow = ray_shade(landscape,
zscale=1,
lambert=FALSE)
amb = ambient_shade(landscape,
zscale=1,
sunbreaks = 15,
maxsearch = 100)
landscape %>%
sphere_shade(zscale=5,texture = "imhof1") %>%
add_water(detect_water(landscape, min_area = 4000)) %>%
add_shadow(shadow,0.7) %>%
add_shadow(amb) %>%
add_shadow(lamb_shade(landscape)) %>%
plot_3d(landscape,
zscale=5,
fov=0,
theta=-45,
phi=45,
windowsize=c(1200,1200),
zoom=1.2,
water=TRUE,
wateralpha = 0.8,
watercolor = "lightblue",
waterlinecolor = "white",
waterlinealpha = 0.3,
solid = FALSE)
library(rayshader)
library(NLMR)
library(raster)
library(rgl)
set.seed(123)
landscape <- nlm_mpd(1000, 1000, roughness = 0.6, rescale = FALSE) * 500
landscape <- raster::focal(landscape, w=matrix(1, 31, 31), mean, pad = TRUE, padValue=0)
landscape <- raster::as.matrix(landscape)
shadow = ray_shade(landscape,
zscale=1,
lambert=FALSE)
amb = ambient_shade(landscape,
zscale=1,
sunbreaks = 15,
maxsearch = 100)
landscape %>%
sphere_shade(zscale=5,texture = "imhof1") %>%
add_water(detect_water(landscape, min_area = 4000)) %>%
add_shadow(shadow,0.7) %>%
add_shadow(amb) %>%
add_shadow(lamb_shade(landscape)) %>%
plot_3d(landscape,
zscale = 5,
fov = 0,
theta = -45,
phi = 45,
windowsize = c(1200, 1200),
zoom = 1.2,
water = TRUE,
wateralpha = 0.8,
watercolor = "lightblue",
waterlinecolor = "white",
waterlinealpha = 0.3,
solid = TRUE,
solidcolor = "grey75")
render_depth(
focallength = 30,
fstop = 2,
bokehshape = "hex",
bokehintensity = 5,
progbar = FALSE
)