# Print a numeric vector
c(3.14, 2.718, -10, 100.5)
[1] 3.140 2.718 -10.000 100.500
Vector and raster packages, spatial operations, and cartography.
Each programming language has specific ways of doing things, and R is no different. The following section contains a general introduction to R classes and data types.
Depending on which operations you are performing, the data class will make a huge difference. These classes determine how R treats and operates on the data.
Numeric data class represents continuous numerical values. E.g., temperature
Integer data class represents whole numbers without decimal points. E.g., counts
Character data class represents text strings. E..g., identification
Factor data class represents categorical variables with a fixed set of possible values. E.g., land cover
Logical data class represents binary values indicating true or false. E.g., presence/abscence
One-dimensional arrays that can hold numeric, character, or logical values.
Two-dimensional arrays with rows and columns of the same data type.
Tabular data structures, similar to spreadsheets, consisting of rows and columns.
Collections of objects, which can be of different data types.
list(
numeric_vector = c(1, 2, 3),
character_vector = c("a", "b", "c"),
matrix_data = matrix(1:4, nrow = 2),
data_frame = data.frame(
Name = c("John", "Alice"),
Age = c(25, 30),
stringsAsFactors = FALSE
)
)
$numeric_vector
[1] 1 2 3
$character_vector
[1] "a" "b" "c"
$matrix_data
[,1] [,2]
[1,] 1 3
[2,] 2 4
$data_frame
Name Age
1 John 25
2 Alice 30
These run mathematical operations on two or more elements.
These assess relationships between two or more elements.
These assign values or groups of values to an object that is stored in memory.
These pass a series of arguments to a pre-defined process, called a function.
Packages in R are collections of functions curated by developers to make life easier. They extend the functionality by providing additional tools, common workflows, and accessible datasets.
R packages can generally be downloaded/installed via two primary methods. (1) from the CRAN - Comprehensive R Archive Network, or (2) from a GitHub repository containing the package. While many popular packages are on CRAN, specific application packages might only be available on GitHub.
# Installing packages from CRAN
install.packages("package_name")
# Installing packages from GitHub
## Install and load the remotes package (if not already installed)
if (!requireNamespace("remotes", quietly = TRUE)) {
install.packages("remotes")}
library(remotes)
## Install the package from the GitHub repository
install_github("username/repository")
# Loading any installed package
library(package_name)
The tidyverse is a collection of R packages designed for data science and statistical analysis. It provides a cohesive framework for working with data by emphasizing consistency, readability, and efficiency.
The core philosophy of the tidyverse centers around the principles outlined in the “tidy data” concept, where datasets are organized in a structured format with each variable forming a column, each observation forming a row, and each type of observational unit forming a table.
The tidyverse also introduced the pipe (%>%
) which allows the chaining of functions. Pipes enable expressive code where the output of a previous function becomes the first argument of the next function, enabling chaining. RStudio now has a native pipe (|>
) that works with a variety of non-tidyverse packages. Note: ctrl+shift+M = shortcut
There are likely over 100 R packages that can handle some aspect of spatial data, these are the most popular ones that we will explore some in the workshop. A more comprehensive list can be found here: https://cran.r-project.org/web/views/Spatial.html
Spatial packages in R are currently in a transition period, with many historically common packages being replaced by newer, more performant varieties. E.g., sp
, rgdal
, rgeos
, and raster
are deprecated along with their spatial object types, but are often still dependencies.
Note: indicates packages utilized in this workshop.
cartography
Several R packages can load spatial data into the R environment, but we will focus on sf
and terra
for this workshop. Note that creating an R spatial object does not save it to a file directory automatically, it simply loads the spatial information into your current R session (in memory). For large files, it can be useful to subset to only include a specific area of interest (AOI).
Remember that the vector data model represents points, lines, and polygons. We can load these spatial features into our R environment with the sf
package using the st_read()
function. But first we use the st_layers
function to identify all the layers we have available to us in our geopackage.
## Reference spatial layers from geopackage (n = 25)
gpkg_dsn <- "BeaverHabitatSelection.gpkg"
gpkg_layers <- sf::st_layers(dsn = gpkg_dsn)
## Load in beaver-absent vegetation layer
absent_veg_sf <- sf::st_read(dsn = gpkg_dsn,
layer = gpkg_layers$name[3])
If we need to reference a shapefile in our directory, we can still use st_read()
but need to specify the .shp extension in our function.
# Set the file path to your shapefile (.shp)
path <- "path/to/your/shapefile.shp"
# Read the shapefile using sf::st_read()
shapefile <- sf::st_read(path)
The “sf” in sf
stands for simple features, which is an open data standard by the Open Geospatial Consortium (OGC). This standard is used across many software systems (e.g., QGIS, PostGIS) and contains seven core geometry types that are supported by sf
.
st_point()
st_linestring()
st_polygon()
st_multipoint()
st_multilinestring()
st_multipolygon()
st_geometrycollection()
Remember that the raster data model represents the world with a continuous grid of cells, called pixels. We can load raster files (e.g., .tif) into our R environment using the rast()
function in the terra
package.
# Set tje file path to your raster, in this case a .tif
raster_filepath = system.file("raster/srtm.tif")
# Read the raster as a SpatRaster
my_rast = rast(raster_filepath)
Every raster object has a header that can be viewed by calling the object in the console. This contains information like the dimensions and CRS of the raster.
# Call the raster object to view its header
my_rast
#> class : SpatRaster
#> dimensions : 457, 465, 1 (nrow, ncol, nlyr)
#> resolution : 0.000833, 0.000833 (x, y)
#> extent : -113, -113, 37.1, 37.5 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source : srtm.tif
#> name : srtm
#> min value : 1024
#> max value : 2892
It is important to note that several other R packages still require raster data to be in the older format handled by the raster
package. In this case, we can coerce our SpatRaster into the raster
format.
Here are some example spatial options in R using sf
and the tidyverse
.
As we saw before, several packages can create maps in R so we have several to pick from. Which one you use will depend on your familiarity with the package and the purpose of the map you are creating.
In general, there are two types of maps we will create in R: (1) static, such as those for publications, and (2) interactive/dynamic, such as those we can view on our computer screens and allow us to zoom in and drag to navigate around the map.