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("NLMR")
library("rasterVis")
landscape <- nlm_mosaictess(200, 200, germs = 444)
gplot(landscape) +
geom_tile(aes(fill = value)) +
coord_equal()
If you want to start from scratch with ggplot2:
library("NLMR")
library("raster")
library("ggplot2")
landscape <- nlm_mosaictess(200, 200, germs = 444)
# transform to long format for ggplot2
landscape_long <- as.data.frame(landscape, xy = TRUE)
# plot with ggplot2
ggplot(landscape_long, aes(x,y)) +
geom_tile(aes(fill = layer)) +
coord_equal()
… if you are in a lot of hurry, raster itself also has a plot method for raster:
library("NLMR")
library("raster")
landscape <- nlm_mosaictess(200, 200, germs = 444)
plot(landscape)