sample()
function.
library(dplyr)
fancy_matrix <-
sample(1:1000, 8*8, replace = TRUE) |>
matrix(nrow = 8, ncol = 8)
fancy_matrix
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 752 27 933 578 174 948 974 604
## [2,] 89 462 49 837 574 788 108 829
## [3,] 502 500 863 862 531 875 358 890
## [4,] 960 365 793 841 408 974 539 697
## [5,] 130 541 808 961 981 484 986 143
## [6,] 38 576 110 839 532 294 764 35
## [7,] 784 424 768 718 85 727 838 27
## [8,] 3 216 183 858 849 805 192 929
terra::rast()
function can be fed with matrices to
create a raster layer.
library(terra)
fancy_raster_layer <- terra::rast(fancy_matrix)
terra::plot(fancy_raster_layer)
The terra::rast()
function can not only be used to
create raster data on the fly, which is also quite boring. Instead, we
can use it to import already prepared data.
.tiff
files in the
./data
folder of the workshop directory.
getwd()
. Setting is done with setwd()
.
immigrants_cologne <- terra::rast("./data/immigrants_cologne.tif")
+
, -
, or /
operators.
Z-standardization can be applied using the terra::scale()
function.
# load all layers
immigrants_cologne <-
terra::rast("./data/immigrants_cologne.tif")
inhabitants_cologne <-
terra::rast("./data/inhabitants_cologne.tif")
# create proportation layer
immigrants_proportion <- immigrants_cologne / inhabitants_cologne
# scale data
immigrants_proportion_scaled <- terra::scale(immigrants_proportation)
immigrants_proportion_scaled[immigrants_proportion_scaled < 0] <- 0
immigrants_proportion_scaled[immigrants_proportion_scaled > 0] <- 1
library(tmap)
tm_shape(immigrants_proportion) +
tm_raster(palette = "-viridis")
tm_shape(immigrants_proportion_scaled) +
tm_raster(palette = "-viridis")