Introduction to R and RStudioProject management in RStudioIntroduction to R


  • Use RStudio to write and run R programs.
  • Use install.packages() to install packages.
  • Use library() to load packages.

Data Structures


  • The mostly used basic data types in R are numeric, integer, logical, and character
  • Use factors to represent categories in R.

Exploring Data Frames & Data frame Manipulation with dplyrExploring Data framesData frame Manipulation with dplyr


  • We can use the select() and filter() functions to select certain columns in a data frame and to subset it based a specific conditions.
  • With mutate(), we can create new columns in a data frame with values based on existing columns.
  • By combining group_by() and summarize() in a pipe (%>%) chain, we can generate summary statistics for each group in a data frame.

Introduction to visualisationIntroduction to VisualisationWriting data


  • With ggplot2, we use the + operator to combine plot layers and incrementally build a more complex plot.
  • In the aesthetics (aes()), we can assign variables to the x and y axes and use the fill argument for colouring surfaces.
  • With scale_fill_viridis_c() and scale_fill_manual() we can assign new colours to our plot.
  • To open the help documentation for a function, we run the name of the function preceded by the ? sign.

Introduction to Geospatial Concepts


  • Each location on the Earth has its geographical latitude and longitude, which can be transformed on a plane using a map projection.

  • Depending on the research question, we need a global, regional, or local CRS suitable properties.

Open and Plot Vector Layers


  • Metadata for vector layers include geometry type, CRS, and extent.
  • Load spatial objects into R with the st_read() function.
  • Spatial objects can be plotted directly with ggplot2 using the geom_sf() function. No need to convert to a data frame.

Explore and plot by vector layer attributesQuery Vector Feature Metadata


  • Spatial objects in sf are similar to standard data frames and can be manipulated using the same functions.

  • Almost any feature of a plot can be customized using the various functions and options in the ggplot2 package.

Plot multiple shapefiles


  • Use the + operator to add multiple layers to a ggplot.
  • A plot can be a combination of multiple vector layers.
  • Use the scale_color_manual() and scale_fill_manual() functions to set legend colors.

Handling Spatial Projections & CRS


  • ggplot2 automatically converts all objects in a plot to the same CRS.
  • Still be aware of the CRS and extent for each object.
  • You can export an sf object to a shapefile with st_write().

Intro to Raster Data


  • The GeoTIFF file format includes metadata about the raster data.
  • To plot raster data with the ggplot2 package, we need to convert them to data frames.
  • R stores CRS information in the PROJ.4 format.
  • Histograms are useful to identify missing or bad data values.

Plot Raster Data


  • Continuous data ranges can be grouped into categories using mutate() and cut().
  • Use the built-in terrain.colors() or set your preferred colour scheme manually.

Reproject Raster Data


  • In order to plot two raster data sets together, they must be in the same CRS.
  • Use the project() function to convert between CRSs.

Raster Calculations


  • Rasters can be computed on using mathematical functions.
  • The writeRaster() function can be used to write raster data to a file.

Work with Multi-Band Rasters


  • A single raster file can contain multiple bands or layers.
  • Use the rast() function to load all bands in a multi-layer raster file into R.
  • Individual bands within a SpatRaster can be accessed, analysed, and visualized using the same functions no matter how many bands it holds.

Import and Visualise OSM Data


  • Use the Nominatim and Overpass APIs within R
  • Use the osmdata package to retrieve geospatial data
  • Select features and attributes among OSM tags
  • Use the ggplot, sf and leaflet packages to map data

Basic GIS operations with R and sf


  • Use the st_* functions from sf for basic GIS operations
  • Perform unions, joins and intersection operations
  • Compute the area of spatial polygons with st_area()