<- c("sf",
needed_libraries "maps",
"units",
"GGally",
"raster",
"mapproj",
"leaflet",
"rnaturalearth",
"rnaturalearthdata"
)# Determines which libraries you do not have
# already installed.
<- setdiff( needed_libraries,
need_inst rownames( installed.packages()) )
# Instrall the neccessary ones.
if( length( need_inst ) ) {
install.packages( need_inst, dependencies = TRUE )
}
In Class Activity
This is a bit quick but is designed to make sure that:
- To make sure everyone can get their computer up-to-date with the proper set of libraries so that we can work with geospatial data, and
- Give you some practice turning normal numerical data into geospatial coordinates and performing basic operations.
Spatial Libraries
The following code defines the set of necessary libraries for the next few weeks and then figures out which (potential) subset you need to install. It then installs them all in one call. It will not load them into your session, you’ll have to do that later in your code chunks.
The Data - Preprocessing
The data for this is some site-level data from the Sonoran Desert bark beetle. The URL is in the next chunk, and is currently being hosted on my class Github repository. Load it in.
library( tidyverse )
<- "https://raw.githubusercontent.com/dyerlab/ENVS-Lectures/master/data/Araptus_Disperal_Bias.csv"
url read_csv( url ) -> data
Rows: 31 Columns: 9
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): Site
dbl (8): Males, Females, Suitability, MFRatio, GenVarArapat, GenVarEuphli, L...
ℹ 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.
summary( data )
Site Males Females Suitability
Length:31 Min. : 9.00 Min. : 5.00 Min. :0.0563
Class :character 1st Qu.:16.00 1st Qu.:15.50 1st Qu.:0.2732
Mode :character Median :21.00 Median :21.00 Median :0.3975
Mean :25.68 Mean :23.52 Mean :0.4276
3rd Qu.:31.50 3rd Qu.:29.00 3rd Qu.:0.5442
Max. :64.00 Max. :63.00 Max. :0.9019
MFRatio GenVarArapat GenVarEuphli Latitude
Min. :0.5938 Min. :0.0500 Min. :0.0500 Min. :23.29
1st Qu.:0.8778 1st Qu.:0.1392 1st Qu.:0.1777 1st Qu.:24.95
Median :1.1200 Median :0.2002 Median :0.2171 Median :26.64
Mean :1.1598 Mean :0.2006 Mean :0.2203 Mean :26.44
3rd Qu.:1.3618 3rd Qu.:0.2592 3rd Qu.:0.2517 3rd Qu.:27.78
Max. :2.2000 Max. :0.3379 Max. :0.5122 Max. :29.33
Longitude
Min. :-114.3
1st Qu.:-113.1
Median :-112.0
Mean :-112.0
3rd Qu.:-110.8
Max. :-109.3
Interactive Map
Use the leaflet
library and make an interactive map. If you can, add a label to each marker with the ID of the site. You can look at the provider tiles here and use on in the addProviderTiles()
function.
There is a great tutorial here on how to customize the leaflet display. You may want to go look at it and see what you can use to make a better display.
# make a quick leaflet map here to get an idea of
library( leaflet )
|>
data leaflet() |>
addMarkers( ~Longitude, ~Latitude ) |>
addTiles()
Converting to sf
Objects
Convert the spatial data in the beetle data.frame into an sf
object. Make sure you set the Coordinate Reference System to 4326.
# convert raw data to simple features in your data.frame
library( sf )
Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
|>
data st_as_sf(coords = c("Longitude","Latitude"), crs=4326 ) -> data
head( data )
Simple feature collection with 6 features and 7 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -114.2935 ymin: 24.00789 xmax: -109.327 ymax: 29.32541
Geodetic CRS: WGS 84
# A tibble: 6 × 8
Site Males Females Suitability MFRatio GenVarArapat GenVarEuphli
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 32 40 27 0.0563 1.48 0.144 0.219
2 73 11 5 0.146 2.2 0.137 0.253
3 93 25 21 0.163 1.19 0.163 0.133
4 const 18 11 0.174 1.64 0.280 0.235
5 159 22 15 0.188 1.47 0.160 0.0809
6 88 23 18 0.219 1.28 0.266 0.176
# ℹ 1 more variable: geometry <POINT [°]>
Questions
For these questions, you may want to look at the sf
cheat sheet here to find the appropriate geospatial function.
- How far apart (in km) are the sites Constitución (
const
) and San Francisquito (sfran
)?
library( units )
udunits database from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/units/share/udunits/udunits2.xml
|>
data filter( Site %in% c("const", "sfran" ) ) |>
st_distance() |>
set_units( km ) -> pw
<- format( as.numeric( pw[2,1] ), digits=4 ) distConstSFran
My sampling locations were 290.1 km apart.
Is there a correlation between habitat suitability and the ratio of males to female beetles sampled at each locale?
Make a plot using
ggplot
of the data with suitability as the size of the points and the sex ratio as the fill color.
|>
data ggplot( aes( size=Suitability, color = MFRatio ) ) +
geom_sf() +
theme_minimal()
- Reproject the data and plot it again using an EPSG code defined for Mexico UTM zone 12N (see epgs.io for projections).