Trajectories of a binary varible

Thomas Mumuni Bilintoh

2022-07-16

binaryTimeSeries

This tutorial illustrates the concepts of the trajectories of a binary variable using an example data.

Data

The example data is a time series of five binary maps at five time points: 2000, 2001, 2002, 2003, and 2005. The tutorial analyzes the trajectory of variable 1, which in this instance indicates presence, whiles 0 represents absence.

The time series maps are available in GeoTIFF and CSV formats and included in the package. The first step is to load the raster files.

# Read the raster files from "externa"l folder as terra raster format.
rasstackY <- terra::rast(system.file("external/Example_raster_Y.tif",package="binaryTimeSeries"))

#NB: The package comes with "Example_Data_Y.tif", "Example_Data_Y.cvs", "Example_raster_X.tif", and "Example_Data_X.cvs."

# Set the spatial resolution of the data. 
datares <- c( 1000, 1000)

# Set the crs of the data.
datprj <-"+proj=utm +zone=32 +datum=WGS84 +ellps=GRS80  +units=m +no_defs"

Calling the changeData function

Next, use the changeData function to create a list containing the raster data sets concerning the number of times the category of interest is present and the number of times the category of interest changes during the time series. Output from the changeData function serves as input data for the presencePlot function. Type “?changeData” in your R console to see the help file.

# Create input data for the presencePlot function and assign the results to a variable, e.g., "a."
a <- changeData (rasstackY,
                 nodata = 2,
                 category = 1,
                 spres = datares,
                 datacrs = datprj)
a
#> $`Data for number of presence`
#> class       : SpatRaster 
#> dimensions  : 12, 1, 1  (nrow, ncol, nlyr)
#> resolution  : 1000, 1000  (x, y)
#> extent      : 681400, 682400, 772500, 784500  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / UTM zone 32N (EPSG:32632) 
#> source      : memory 
#> name        : sum 
#> min value   :   0 
#> max value   :   5 
#> 
#> $`Data for unique number of presence`
#>   sum
#> 1   0
#> 2   1
#> 3   2
#> 4   3
#> 5   4
#> 6   5
#> 
#> $`Data for number of changes`
#> class       : SpatRaster 
#> dimensions  : 12, 1, 1  (nrow, ncol, nlyr)
#> resolution  : 1000, 1000  (x, y)
#> extent      : 681400, 682400, 772500, 784500  (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / UTM zone 32N (EPSG:32632) 
#> source      : memory 
#> name        : sum 
#> min value   :   0 
#> max value   :   4 
#> 
#> $`Data for unique number of changes`
#>   sum
#> 1   0
#> 2   1
#> 3   2
#> 4   3
#> 5   4

Calling the presencePlot function

Use the presencePlot function to create the number of presences and change maps. Type “?presencePlot” in your R console to see the help file.

presencePlot (input = a,
              pltunit = "m",
              dataEpsg = 32632,
              scalePos = "bottomleft",
              narrowPos = "topright",
              narrowSize = 1,
              categoryName = "marsh",
              xAxis = "Longitude (m)",
              yAxis = "Latitude (m)",
              axisText = 1.2,
              axisLabel = 1.2,
              plotTitle = 1)

Calling the trajData function

The trajData function creates the data that serves as input for the trajPlot function. Type “?trajData” in your R console to see the help file.

# Create input data for the trajPlot function and assign the results to a variable, e.g., "b."
b <- trajData(x = rasstackY,
             nodata = 2,
             category = 1,
             spres = datares,
             datacrs = datprj,
             unified = "yes")# if "no", the analysis considers the entire study region.

b
#> $`Raster data for trajectory plot`
#> class      : RasterLayer 
#> dimensions : 11, 1, 11  (nrow, ncol, ncell)
#> resolution : 1000, 1000  (x, y)
#> extent     : 681400, 682400, 773500, 784500  (xmin, xmax, ymin, ymax)
#> crs        : +proj=utm +zone=32 +datum=WGS84 +ellps=GRS80 +units=m +no_defs +towgs84=0,0,0 
#> source     : memory
#> names      : change 
#> values     : 1, 8  (min, max)
#> 
#> 
#> $`Attribute data for trajectory plot`
#>   ID   myCol                                                     cl
#> 1  1 #941004                    Presence<U+2192>Loss<U+2192>Absence
#> 2  2 #FF6666 Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence
#> 3  3 #020e7a                    Absence<U+2192>Gain<U+2192>Presence
#> 4  4 #14a5e3 Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence
#> 5  5 #a8a803            Presence<U+2192>Alternation<U+2192>Presence
#> 6  6 #E6E600              Absence<U+2192>Alternation<U+2192>Absence
#> 7  7 #666666                 Presence<U+2192>Stable<U+2192>Presence
#> 8  8 #c4c3c0                   Absence<U+2192>Stable<U+2192>Absence
#> 
#> $`Data for trajectory pie chart`
#>   ID   myCol                                                     cl value
#> 1  1 #941004                    Presence<U+2192>Loss<U+2192>Absence     3
#> 2  2 #FF6666 Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence     1
#> 3  5 #a8a803            Presence<U+2192>Alternation<U+2192>Presence     1
#> 4  6 #E6E600              Absence<U+2192>Alternation<U+2192>Absence     1
#> 5  3 #020e7a                    Absence<U+2192>Gain<U+2192>Presence     2
#> 6  4 #14a5e3 Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence     1
#> 7  7 #666666                 Presence<U+2192>Stable<U+2192>Presence     1
#> 
#> $`Number of time points`
#> [1] 5
#> 
#> $`Dataframe for trajectory`
#>         x      y change
#> 1  681900 784000      1
#> 2  681900 783000      1
#> 3  681900 782000      1
#> 4  681900 779000      2
#> 5  681900 781000      3
#> 6  681900 780000      3
#> 7  681900 778000      4
#> 8  681900 777000      5
#> 9  681900 776000      6
#> 10 681900 775000      7
#> 11 681900 774000      8

Calling the trajPlot function

The trajPlot function creates a map and pie chart of the trajectories of the category of interest. Type “?trajPlot” in your R console to see the help file.

Calling the dataStack function

The dataStack function create the data which serves as input for the “stackbarPlots” function. Type “?dataStack” in your R console to see the help file.

# Create a vector variable containing the time points
timepoints <- c(2000,2001,2002,2003,2005)

# Use the dataStack function to create input for the stackbarPlots function.
c <- dataStack(x = rasstackY,
               category = 1,
               nodata = 2,
               unified = "yes",
               timePoints = timepoints,
               categoryName = "marsh",
               regionName = "extent")
c
#> $`Factor dataframe for trajectory stacke bar plot`
#>                                                      Var1      Var2 value size
#> 1             Presence<U+2192>Alternation<U+2192>Presence 2000-2001     0    1
#> 2               Absence<U+2192>Alternation<U+2192>Absence 2000-2001    10    1
#> 3  Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence 2000-2001    10    1
#> 4  Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence 2000-2001     0    1
#> 5                     Absence<U+2192>Gain<U+2192>Presence 2000-2001     0    1
#> 6                     Presence<U+2192>Loss<U+2192>Absence 2000-2001     0    1
#> 7             Presence<U+2192>Alternation<U+2192>Presence 2001-2002     0    1
#> 8               Absence<U+2192>Alternation<U+2192>Absence 2001-2002     0    1
#> 9  Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence 2001-2002     0    1
#> 10 Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence 2001-2002     0    1
#> 11                    Absence<U+2192>Gain<U+2192>Presence 2001-2002    10    1
#> 12                    Presence<U+2192>Loss<U+2192>Absence 2001-2002     0    1
#> 13            Presence<U+2192>Alternation<U+2192>Presence 2002-2003    10    1
#> 14              Absence<U+2192>Alternation<U+2192>Absence 2002-2003    10    1
#> 15 Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence 2002-2003    10    1
#> 16 Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence 2002-2003    10    1
#> 17                    Absence<U+2192>Gain<U+2192>Presence 2002-2003    10    1
#> 18                    Presence<U+2192>Loss<U+2192>Absence 2002-2003     0    1
#> 19            Presence<U+2192>Alternation<U+2192>Presence 2003-2005     0    2
#> 20              Absence<U+2192>Alternation<U+2192>Absence 2003-2005     0    2
#> 21 Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence 2003-2005     0    2
#> 22 Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence 2003-2005     0    2
#> 23                    Absence<U+2192>Gain<U+2192>Presence 2003-2005     0    2
#> 24                    Presence<U+2192>Loss<U+2192>Absence 2003-2005     0    2
#> 25            Presence<U+2192>Alternation<U+2192>Presence 2000-2001     0    1
#> 26              Absence<U+2192>Alternation<U+2192>Absence 2000-2001     0    1
#> 27 Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence 2000-2001     0    1
#> 28 Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence 2000-2001     0    1
#> 29                    Absence<U+2192>Gain<U+2192>Presence 2000-2001     0    1
#> 30                    Presence<U+2192>Loss<U+2192>Absence 2000-2001   -10    1
#> 31            Presence<U+2192>Alternation<U+2192>Presence 2001-2002   -10    1
#> 32              Absence<U+2192>Alternation<U+2192>Absence 2001-2002   -10    1
#> 33 Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence 2001-2002   -10    1
#> 34 Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence 2001-2002   -10    1
#> 35                    Absence<U+2192>Gain<U+2192>Presence 2001-2002     0    1
#> 36                    Presence<U+2192>Loss<U+2192>Absence 2001-2002   -10    1
#> 37            Presence<U+2192>Alternation<U+2192>Presence 2002-2003     0    1
#> 38              Absence<U+2192>Alternation<U+2192>Absence 2002-2003     0    1
#> 39 Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence 2002-2003     0    1
#> 40 Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence 2002-2003     0    1
#> 41                    Absence<U+2192>Gain<U+2192>Presence 2002-2003     0    1
#> 42                    Presence<U+2192>Loss<U+2192>Absence 2002-2003   -10    1
#> 43            Presence<U+2192>Alternation<U+2192>Presence 2003-2005     0    2
#> 44              Absence<U+2192>Alternation<U+2192>Absence 2003-2005    -5    2
#> 45 Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence 2003-2005     0    2
#> 46 Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence 2003-2005    -5    2
#> 47                    Absence<U+2192>Gain<U+2192>Presence 2003-2005     0    2
#> 48                    Presence<U+2192>Loss<U+2192>Absence 2003-2005     0    2
#> 
#> $`Value of gain line`
#> [1] 16
#> 
#> $`Value of loss line`
#> [1] -18
#> 
#> $`Dataframe for components of change`
#>       compNames variable value
#> 1 Quantity Loss compVals     2
#> 2    Allocation compVals    12
#> 3   Alternation compVals    20
#> 
#> $`Title of stackbar plot`
#> [1] "Change in extent where any time point is marsh category in extent"
#> 
#> $`Size of net component`
#> [1] "Quantity Loss"
#> 
#> $`Name of category of ineterst`
#> [1] "marsh"
#> 
#> $`Dataframe for stackbar plot`
#>   Presence<U+2192>Alternation<U+2192>Presence
#> 1                                           0
#> 2                                           0
#> 3                                          10
#> 4                                           0
#> 5                                           0
#> 6                                         -10
#> 7                                           0
#> 8                                           0
#>   Absence<U+2192>Alternation<U+2192>Absence
#> 1                                        10
#> 2                                         0
#> 3                                        10
#> 4                                         0
#> 5                                         0
#> 6                                       -10
#> 7                                         0
#> 8                                        -5
#>   Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence
#> 1                                                     10
#> 2                                                      0
#> 3                                                     10
#> 4                                                      0
#> 5                                                      0
#> 6                                                    -10
#> 7                                                      0
#> 8                                                      0
#>   Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence
#> 1                                                      0
#> 2                                                      0
#> 3                                                     10
#> 4                                                      0
#> 5                                                      0
#> 6                                                    -10
#> 7                                                      0
#> 8                                                     -5
#>   Absence<U+2192>Gain<U+2192>Presence Presence<U+2192>Loss<U+2192>Absence
#> 1                                   0                                   0
#> 2                                  10                                   0
#> 3                                  10                                   0
#> 4                                   0                                   0
#> 5                                   0                                 -10
#> 6                                   0                                 -10
#> 7                                   0                                 -10
#> 8                                   0                                   0
#>   Time_intervals interval_2
#> 1              1  2000-2001
#> 2              1  2001-2002
#> 3              1  2002-2003
#> 4              2  2003-2005
#> 5              1  2000-2001
#> 6              1  2001-2002
#> 7              1  2002-2003
#> 8              2  2003-2005
#> 
#> $`Colors and trajectories for stacked bars`
#>                                               trajNames2 trajCol
#> 1            Presence<U+2192>Alternation<U+2192>Presence #a8a803
#> 2              Absence<U+2192>Alternation<U+2192>Absence #e6e600
#> 3 Absence<U+2192>Alternation<U+2192>Gain<U+2192>Presence #14a5e3
#> 4 Presence<U+2192>Alternation<U+2192>Loss<U+2192>Absence #ff6666
#> 5                    Absence<U+2192>Gain<U+2192>Presence #020e7a
#> 6                    Presence<U+2192>Loss<U+2192>Absence #941004

Calling the stackbarPlots function

The stackbarPlots function creates stacked bar plots showing the sizes of a category’s trajectory during each time interval and the three Change Components. Type “?stackbarPlots” in your R console to see the help file.

#> [[1]]

#> 
#> [[2]]

References

Pontius Jr, R. G. (2022). Metrics That Make a Difference: How to Analyze Change and Error. Springer Nature Switzerland AG.

Bilintoh, T.M., (2022). Intensity Analysis to Study the Dynamics of reforestation in the Rio Doce Water Basin, Brazil. Frontiers in Remote Sensing, 3 (873341), 13.

Pontius Jr, R. G. (2019). Component Intensities to relate difference by category with difference overall.International Journal of Applied Earth Observation and Geoinformation, 77: 94-99.

Pontius Jr, R. G., Krithivasan, R., Sauls, L., Yan, Y., & Zhang, Y. (2017). Methods to summarize change among land categories across time intervals. Journal of Land Use Science, 12(4), 218–230.

Pontius Jr, R. G., & Santacruz, A. (2014). Quantity, exchange, and shift components of difference in a square contingency table. International Journal of Remote Sensing, 35(21), 7543–7554.