MovementVis with R

We will learn how to visualise movements by using appropriate R packages in this blog.

Published

July 3, 2021

DOI

Installing and loading the required libraries

Use the below code to install and load packages.

packages = c('raster','sf','clock',
             'tmap','tidyverse','rgdal')
for (p in packages){
  if(!require(p,character.only = T)){
    install.packages(p)
  }
  library(p,character.only = T)
}

Importing Raster file

ap <- raster("data/Geospatial/MC2-tourist.tif")
ap
class      : RasterLayer 
band       : 1  (of  3  bands)
dimensions : 1595, 2706, 4316070  (nrow, ncol, ncell)
resolution : 3.16216e-05, 3.16216e-05  (x, y)
extent     : 24.82419, 24.90976, 36.04499, 36.09543  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
source     : MC2-tourist.tif 
names      : MC2.tourist 
values     : 0, 255  (min, max)
bgmap <- raster("data/Geospatial/MC2-tourist.tif")
bgmap
class      : RasterLayer 
band       : 1  (of  3  bands)
dimensions : 1595, 2706, 4316070  (nrow, ncol, ncell)
resolution : 3.16216e-05, 3.16216e-05  (x, y)
extent     : 24.82419, 24.90976, 36.04499, 36.09543  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
source     : MC2-tourist.tif 
names      : MC2.tourist 
values     : 0, 255  (min, max)

Plotting Raster Layer

tmap_mode("plot") #plot: static version
tm_shape(bgmap) +
  tm_raster(bgmap,
            legend.show = FALSE)

tm_shape(bgmap) + #data
tm_rgb(bgmap,r=1,g=2,b=3,
        alpha = NA,
        saturation = 1,
        interpolate = TRUE,
         max.value = 255)

Abila_st <- st_read(dsn="data/Geospatial",
                    layer="Abila")
Reading layer `Abila' from data source `/Users/zhangying/Alicia-zy/DataViz_Blog/_posts/2021-07-03-movementvis-with-r/data/Geospatial' using driver `ESRI Shapefile'
Simple feature collection with 3290 features and 9 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: 24.82401 ymin: 36.04502 xmax: 24.90997 ymax: 36.09492
CRS:           4326

Importing Aspatial Data

gps <- read_csv("data/aspatial/gps.csv")
glimpse(gps)
Rows: 685,169
Columns: 4
$ Timestamp <chr> "01/06/2014 06:28:01", "01/06/2014 06:28:01", "01/…
$ id        <dbl> 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35…
$ lat       <dbl> 36.07623, 36.07622, 36.07621, 36.07622, 36.07621, …
$ long      <dbl> 24.87469, 24.87460, 24.87444, 24.87425, 24.87417, …

converting Data-Time Field

gps$Timestamp <- date_time_parse(gps$Timestamp, 
                                 zone = "",
                                 format="%m/%d/%Y %H:%M:%S") #function from clock package
gps$id <- as_factor(gps$id) #from tidyverse package

converting Aspatial Data into a Simple Feature Data Frame

gps_sf <- st_as_sf(gps,
                   coords=c("long","lat"),
                   crs=4326) # wgs84 geographic coordinate system

converting movement path from GPS Points

gps_path <- gps_sf %>%
  group_by(id) %>%
  summarize(m=mean(Timestamp),
            do_union=FALSE)%>%
  st_cast("LINESTRING")

Plotting the gps Paths

gps_path_selected <- gps_path %>%
  filter(id==1) 

tmap_mode("view")
tm_shape(bgmap) +
tm_rgb(bgmap,r=1,g=2,b=3,
        alpha = NA,
        saturation = 1,
        interpolate = TRUE,
         max.value = 255)+
  tm_shape(gps_path_selected) +
  tm_lines()
Leaflet | Tiles © Esri — Esri, DeLorme, NAVTEQ