library(devtools) # runs devtools library library(spatinz) library(ggplot2)
This vignette for the spatinz
package provides an overview of the included functions and some examples of how to use them.
If you are reading this vignette you possibly already have the spatinz
package installed, but if not, simply run the following code to install:
install.packages('devtools') # installs devtools package library(devtools) # runs devtools library install_github('isaacbain/spatinz') # installs spatinz package from github library(spatinz)
The devtools
package is required because spatinz
is currently hosted on github. You may be used to installing packages using the install.package()
function, but this only works for packages hosted on the CRAN mirror (which is more restrictive than github).
spatinz
includes various datasets sourced from Ministry for the Environment's data service. These are included in the package because it simplifies using the datasets by not having to read in data formats like shapefile each time (which requires the rgdal
package to convert to a sp object). These datasets can be used inside the spatinz
or anywhere in your global R
environment (for example for use in another package like ggplot2
).
Currently nutrient trend data for 1989-2013 is included for the following nutrient types: - tp (total phosphorus) - drp (dissolved reactive phosphorus) - tn (total nitrogen) - nn (nitrate nitrogen) - an (ammoniacal nitrogen)
Once spatinz
is installed the datasets can be accessed by simply typing their abbreviation (as above), or by assigning them to another name:
tp
nutrient_of_interest_data <- tp
It is sufficient to just have spatinz
installed to access the datasets, even without running the package (using the library()
command):
spatinz::tp nutrient_of_interest_data <- spatinz::tp
plot.trends
functionThis function allows for the easy production of a ggplot map of a nutrient trend of choice. This can be for either the included nutrient trend datasets, or you can supply your own in sp format.
To plot a map:
plot.trends(tp) # total phosphorus plot.trends(nn) # total nitrogen
These maps utilise the ggplot2
package as their plotting engine, so the usual list of geoms
and aesthetics
can be easily added to the maps:
p <- plot.trends(tp) # assign map to a variable name p + labs(title = "Total phosphorus trends, 1989-2013") # add a title to the map
More functions are included in spatinz
that make it easier or more efficient to work with New Zealand spatial data and/or data sourced from MFE's data service.
proj4web
Spatial data in R or GIS software inherently includes spatial metadata, including the spatial projection. It is common to switch between projections (reproject). New Zealand spatial data by is often by convention supplied in NZTM format, but certain uses of spatial data requires reprojection to a different format. For example, most webmaps (like leaflet.js) require data to be in the popular WGS84 projection.
This function quickly reprojects any projection into WGS84 (for web):
new_projection <- proj4web(old_projection)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.