#load into R library
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.3
## Warning: package 'ggplot2' was built under R version 4.5.3
## Warning: package 'tibble' was built under R version 4.5.3
## Warning: package 'tidyr' was built under R version 4.5.3
## Warning: package 'readr' was built under R version 4.5.3
## Warning: package 'purrr' was built under R version 4.5.3
## Warning: package 'dplyr' was built under R version 4.5.3
## Warning: package 'stringr' was built under R version 4.5.3
## Warning: package 'lubridate' was built under R version 4.5.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.4     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf) # simple features
## Warning: package 'sf' was built under R version 4.5.3
## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
library (terra) # for raster
## Warning: package 'terra' was built under R version 4.5.3
## terra 1.9.50
## 
## Attaching package: 'terra'
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
library(tmap) # Thematic maps are geographical maps in which spatial data distributions are visualized
## Warning: package 'tmap' was built under R version 4.5.3
#load the copepod data into R studio
dat <- readr::read_csv(
  here::here("data", "data-for-course", "copepods_raw.csv")
)
## Rows: 5313 Columns: 11
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): sample_time_utc, project, route, vessel, region
## dbl (6): silk_id, segment_no, latitude, longitude, meanlong, richness_raw
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
dat
## # A tibble: 5,313 × 11
##    silk_id segment_no latitude longitude sample_time_utc  project route vessel  
##      <dbl>      <dbl>    <dbl>     <dbl> <chr>            <chr>   <chr> <chr>   
##  1       1          1    -28.3      154. 26/06/2009 22:08 AusCPR  BRSY  ANL Win…
##  2       1          5    -28.7      154. 26/06/2009 23:12 AusCPR  BRSY  ANL Win…
##  3       1          9    -29.0      154. 27/06/2009 0:17  AusCPR  BRSY  ANL Win…
##  4       1         13    -29.3      154. 27/06/2009 1:22  AusCPR  BRSY  ANL Win…
##  5       1         17    -29.7      154. 27/06/2009 2:26  AusCPR  BRSY  ANL Win…
##  6       1         18    -29.8      154. 27/06/2009 2:43  AusCPR  BRSY  ANL Win…
##  7       1         26    -30.4      153. 27/06/2009 4:52  AusCPR  BRSY  ANL Win…
##  8       1         30    -30.7      153. 27/06/2009 5:57  AusCPR  BRSY  ANL Win…
##  9       1         33    -31.0      153. 27/06/2009 6:45  AusCPR  BRSY  ANL Win…
## 10       1         37    -31.3      153. 27/06/2009 7:50  AusCPR  BRSY  ANL Win…
## # ℹ 5,303 more rows
## # ℹ 3 more variables: meanlong <dbl>, region <chr>, richness_raw <dbl>
ggplot(dat) + 
  aes(x = longitude, y = latitude, color = richness_raw) +
  geom_point()

ggplot(dat, aes(x = latitude, y = richness_raw)) + 
  stat_smooth() + 
  geom_point()
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

sdat <- st_as_sf(
  dat,
  coords = c("longitude", "latitude"),
  crs = 4326
)

crs4326 <- st_crs(4326)
crs4326 # look at the whole CRS
## Coordinate Reference System:
##   User input: EPSG:4326 
##   wkt:
## GEOGCRS["WGS 84",
##     ENSEMBLE["World Geodetic System 1984 ensemble",
##         MEMBER["World Geodetic System 1984 (Transit)"],
##         MEMBER["World Geodetic System 1984 (G730)"],
##         MEMBER["World Geodetic System 1984 (G873)"],
##         MEMBER["World Geodetic System 1984 (G1150)"],
##         MEMBER["World Geodetic System 1984 (G1674)"],
##         MEMBER["World Geodetic System 1984 (G1762)"],
##         MEMBER["World Geodetic System 1984 (G2139)"],
##         MEMBER["World Geodetic System 1984 (G2296)"],
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]],
##         ENSEMBLEACCURACY[2.0]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     USAGE[
##         SCOPE["Horizontal component of 3D system."],
##         AREA["World."],
##         BBOX[-90,-180,90,180]],
##     ID["EPSG",4326]]
crs4326$Name # pull out just the name of the crs
## [1] "WGS 84"
crs4326$wkt
## [1] "GEOGCRS[\"WGS 84\",\n    ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n        MEMBER[\"World Geodetic System 1984 (Transit)\"],\n        MEMBER[\"World Geodetic System 1984 (G730)\"],\n        MEMBER[\"World Geodetic System 1984 (G873)\"],\n        MEMBER[\"World Geodetic System 1984 (G1150)\"],\n        MEMBER[\"World Geodetic System 1984 (G1674)\"],\n        MEMBER[\"World Geodetic System 1984 (G1762)\"],\n        MEMBER[\"World Geodetic System 1984 (G2139)\"],\n        MEMBER[\"World Geodetic System 1984 (G2296)\"],\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]],\n        ENSEMBLEACCURACY[2.0]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"
# Check the corrected label.
sf::st_crs(sdat)
## Coordinate Reference System:
##   User input: EPSG:4326 
##   wkt:
## GEOGCRS["WGS 84",
##     ENSEMBLE["World Geodetic System 1984 ensemble",
##         MEMBER["World Geodetic System 1984 (Transit)"],
##         MEMBER["World Geodetic System 1984 (G730)"],
##         MEMBER["World Geodetic System 1984 (G873)"],
##         MEMBER["World Geodetic System 1984 (G1150)"],
##         MEMBER["World Geodetic System 1984 (G1674)"],
##         MEMBER["World Geodetic System 1984 (G1762)"],
##         MEMBER["World Geodetic System 1984 (G2139)"],
##         MEMBER["World Geodetic System 1984 (G2296)"],
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]],
##         ENSEMBLEACCURACY[2.0]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     USAGE[
##         SCOPE["Horizontal component of 3D system."],
##         AREA["World."],
##         BBOX[-90,-180,90,180]],
##     ID["EPSG",4326]]
sdat
## Simple feature collection with 5313 features and 9 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 89.6107 ymin: -65.2428 xmax: 174.335 ymax: -16.80253
## Geodetic CRS:  WGS 84
## # A tibble: 5,313 × 10
##    silk_id segment_no sample_time_utc  project route vessel      meanlong region
##  *   <dbl>      <dbl> <chr>            <chr>   <chr> <chr>          <dbl> <chr> 
##  1       1          1 26/06/2009 22:08 AusCPR  BRSY  ANL Windar…     153. East  
##  2       1          5 26/06/2009 23:12 AusCPR  BRSY  ANL Windar…     153. East  
##  3       1          9 27/06/2009 0:17  AusCPR  BRSY  ANL Windar…     153. East  
##  4       1         13 27/06/2009 1:22  AusCPR  BRSY  ANL Windar…     153. East  
##  5       1         17 27/06/2009 2:26  AusCPR  BRSY  ANL Windar…     153. East  
##  6       1         18 27/06/2009 2:43  AusCPR  BRSY  ANL Windar…     153. East  
##  7       1         26 27/06/2009 4:52  AusCPR  BRSY  ANL Windar…     153. East  
##  8       1         30 27/06/2009 5:57  AusCPR  BRSY  ANL Windar…     153. East  
##  9       1         33 27/06/2009 6:45  AusCPR  BRSY  ANL Windar…     153. East  
## 10       1         37 27/06/2009 7:50  AusCPR  BRSY  ANL Windar…     153. East  
## # ℹ 5,303 more rows
## # ℹ 2 more variables: richness_raw <dbl>, geometry <POINT [°]>
plot(sdat["richness_raw"])

plot(sdat)

# using tmap
tm_shape(sdat) +
  tm_dots(col = "richness_raw")

aus <- sf::st_read(
  here::here(
    "data", "data-for-course",
    "spatial-data", "Aussie", "Aussie.shp"
  )
)
## Reading layer `Aussie' from data source 
##   `C:\Users\Imannina\Documents\TIMS1\MB5370_Module_2_Coding\data\data-for-course\spatial-data\Aussie\Aussie.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 8 features and 1 field
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 112.9211 ymin: -43.63192 xmax: 153.6389 ymax: -9.229614
## Geodetic CRS:  WGS 84
shelf <- sf::st_read(
  here::here(
    "data", "data-for-course",
    "spatial-data", "aus_shelf", "aus_shelf.shp"
  )
)
## Reading layer `aus_shelf' from data source 
##   `C:\Users\Imannina\Documents\TIMS1\MB5370_Module_2_Coding\data\data-for-course\spatial-data\aus_shelf\aus_shelf.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 1 field
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 112.2242 ymin: -44.1284 xmax: 153.8942 ymax: -8.8798
## Geodetic CRS:  GRS 1980(IUGG, 1980)
aus
## Simple feature collection with 8 features and 1 field
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 112.9211 ymin: -43.63192 xmax: 153.6389 ymax: -9.229614
## Geodetic CRS:  WGS 84
##   adm                       geometry
## 1 NSW MULTIPOLYGON (((141.0029 -3...
## 2 Vic MULTIPOLYGON (((140.9659 -3...
## 3 QLD MULTIPOLYGON (((153.5522 -2...
## 4  SA MULTIPOLYGON (((129.0013 -2...
## 5  WA MULTIPOLYGON (((129.0012 -1...
## 6 Tas MULTIPOLYGON (((148.3096 -4...
## 7  NT MULTIPOLYGON (((137.9962 -1...
## 8 ACT MULTIPOLYGON (((149.1253 -3...
tm_shape(shelf) + 
  tm_polygons()

tm1 <- tm_shape(shelf, bbox = sdat) +
  tm_polygons() +
  tm_shape(aus) +
  tm_polygons() +
  tm_shape(sdat) +
  tm_dots()

tm1

tmap_save(tm1, filename = "Richness-map.png", 
          width = 600, height = 600)
## Map saved to C:\Users\Imannina\Documents\TIMS1\MB5370_Module_2_Coding\docs\Richness-map.png
## Resolution: 600 by 600 pixels
## Size: 2 by 2 inches (300 dpi)