R Workshop

1 Installing R

1.1 Installing R

1.1.1 Installing R

1.1.1.1 Checking that it worked


sessionInfo()

1.1.2 Rstudio

1.1.3 Useful R packages


install.packages(c("tidyverse","sf","terra","leaflet"))

2 R Basics

2.1 Section A

2.1.1 Test

2.1.2 System Content 1

2.1.2.1 Subtest

2.1.3 Content 2

3 Tidyverse

3.1 {dplyr}

3.1.1 Single Table Verbs

  • select()

3.1.2 Multiple Table Verbs

  • Inner_join()

3.1.3 Midwest Dataset

  • Potential Exercise Material

3.2 Other Useful Packages

4 Shapefiles

4.1 The {sf} R package

sf is a package that provides simple features access for R, which are records of spatial data with a geometry list-column.

4.1.1 Loading Shapefiles

library(sf)
## Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 7.2.1; sf_use_s2() is TRUE
fname <- system.file("shape/nc.shp", package="sf")
fname
## [1] "C:/Users/Obrien_K/Documents/R/win-library/4.1/sf/shape/nc.shp"
nc <- st_read(fname)
## Reading layer `nc' from data source 
##   `C:\Users\Obrien_K\Documents\R\win-library\4.1\sf\shape\nc.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 100 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## Geodetic CRS:  NAD27
head(nc)
## Simple feature collection with 6 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -81.74107 ymin: 36.07282 xmax: -75.77316 ymax: 36.58965
## Geodetic CRS:  NAD27
##    AREA PERIMETER CNTY_ CNTY_ID        NAME  FIPS FIPSNO CRESS_ID BIR74 SID74
## 1 0.114     1.442  1825    1825        Ashe 37009  37009        5  1091     1
## 2 0.061     1.231  1827    1827   Alleghany 37005  37005        3   487     0
## 3 0.143     1.630  1828    1828       Surry 37171  37171       86  3188     5
## 4 0.070     2.968  1831    1831   Currituck 37053  37053       27   508     1
## 5 0.153     2.206  1832    1832 Northampton 37131  37131       66  1421     9
## 6 0.097     1.670  1833    1833    Hertford 37091  37091       46  1452     7
##   NWBIR74 BIR79 SID79 NWBIR79                       geometry
## 1      10  1364     0      19 MULTIPOLYGON (((-81.47276 3...
## 2      10   542     3      12 MULTIPOLYGON (((-81.23989 3...
## 3     208  3616     6     260 MULTIPOLYGON (((-80.45634 3...
## 4     123   830     2     145 MULTIPOLYGON (((-76.00897 3...
## 5    1066  1606     3    1197 MULTIPOLYGON (((-77.21767 3...
## 6     954  1838     5    1237 MULTIPOLYGON (((-76.74506 3...

4.2 Irish Transverse Mercator

  • An ITM co-ordinate is generally given as a pair of two six-digit numbers (excluding any digits behind a decimal point which may be used in very precise surveying). The first number is always the easting and the second is the northing. The easting and northing are in metres from the false origin.
The ITM co-ordinate for the Spire of Dublin on O’Connell Street is:

715830, 734697

The first figure is the easting and means that the location is 715,830 metres east from the false origin (along the X axis). The second figure is the northing and puts the location 734,697 metres north of the false origin (along the Y axis)

The equivalent Irish Grid co-ordinate for the same location is:

315904, 234671

4.2.1 Coordinate Reference Systems

5 Making Plots

5.1 {ggplot2} R package

5.2 Making a Map

  • making heatmaps

ggplot(somedata) + geom_sf()

5.3 Adding Points

6 R Packages for GIS

6.1 {ggspatial}

6.1.1 The {ggspatial} R package

This is a Spatial Data Framework for ggplot2 that combines Spatial data with the power of the ggplot2 framework to created easier mapping when input data are already in the form of spatial objects.

6.1.1.1 Longlake Data

library(ggplot2)
load_longlake_data(
  which = c(
    "longlake_osm",
    "longlake_depth_raster"
  ),
  raster_format = "stars"
)
class(longlake_osm)
## [1] "stars"
ggplot() +
  layer_spatial(longlake_osm)

ggplot() +
  layer_spatial(longlake_depth_raster) +
  scale_fill_continuous(
    na.value = NA,
    type = "viridis"
  )

6.3 tidyterra

6.4 {terra}

  • {terra} is an R package for spatial data analysis. There are tutorials at rspatial.org/terra.
library(sf)

https://gnss.osi.ie/new-converter/

library(terra)
## terra 1.7.3
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

7 Appendices

7.0.1 Getting Shapefiles

8 Spare Stuff

8.1 USA Map

library(maps); #library(ggplot2);

library(mapproj)

states <- map_data("state")
usamap <- ggplot(states, aes(x=long, y=lat, group=group)) +
 geom_polygon(fill="white", colour="black")
usamap + coord_map("mercator")

# first 20 quakes
df.20 <- quakes[1:20,]

8.2 Meuse

library(sp)
data(meuse)
coordinates(meuse) = ~x+y
m.sf = st_as_sf(meuse)
opar = par(mar=rep(0,4))
plot(m.sf)