In this Notebook, we’ll do compute some summary stats for the attribute table of the Yosemite fire ignition points, then use attribute queries to filter them.
Load the packages we’ll need for this notebook (sf, dplyr, and tmap):
library(sf)
library(dplyr)
library(tmap)
Pro-actively resolve any name clashes:
library(conflicted)
# Set conflict preferences
conflict_prefer("filter", "dplyr", quiet = TRUE)
conflict_prefer("count", "dplyr", quiet = TRUE)
conflict_prefer("select", "dplyr", quiet = TRUE)
conflict_prefer("arrange", "dplyr", quiet = TRUE)
Next import fire ignition points layer:
## Define the location of the geodatabase
gdb_fires_fn <- "./data/yose_firehistory.gdb"
file.exists(gdb_fires_fn)
[1] TRUE
## View the layers in this source
st_layers(gdb_fires_fn)
Driver: OpenFileGDB
Available layers:
## Import the historic fires ignition points
yose_fires_pt <- st_read(gdb_fires_fn, layer="YNP_FireHistoryPoints")
Reading layer `YNP_FireHistoryPoints' from data source
`D:\Workshops\R-Spatial\rspatial_mod\outputs\rspatial_bgs23\notebooks\data\yose_firehistory.gdb'
using driver `OpenFileGDB'
Simple feature collection with 4432 features and 21 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 243771.7 ymin: 4152632 xmax: 304217.2 ymax: 4227949
Projected CRS: NAD83 / UTM zone 11N
## Preview
glimpse(yose_fires_pt)
Rows: 4,432
Columns: 22
$ OBJECTID <int> 1, 2, 3, 8, 7, 10, 5, 9, 6, 4, 18, 12, 28, 31, 29, 30, 27, 26, 14, 17, 23, 21, 32, 33, 25, 2…
$ FIRE_ID <chr> "YNP-2", "YNP-3", "YNP-7", "YNP-10", "YNP-11", "YNP-15", "YNP-19", "YNP-20", "YNP-3", "YNP-5…
$ LINK_ID <chr> "1930YNP-2", "1930YNP-3", "1930YNP-7", "1931YNP-10", "1931YNP-11", "1931YNP-15", "1931YNP-19…
$ SACS_ID <chr> "YNP-002", "YNP-003", "YNP-007", "YNP-010", "YNP-011", "YNP-015", "YNP-019", "YNP-020", "YNP…
$ FILE_ID <chr> " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " …
$ NAME <chr> " ", " ", " ", "Dog Lake\n", "Rafferty Creek\n", "Cottonwood Cree", "Merced Lake\n", "Onoin …
$ ACRES <dbl> 0.004, 0.007, 0.870, 0.099, 0.050, 1.989, 0.011, 1.492, 0.994, 0.079, 0.010, 0.010, 5.271, 0…
$ YEAR <int> 1930, 1930, 1930, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1932, 1932, 1932, 1932, 1932, 19…
$ TYPE <chr> "WF", "WF", "WF", "WF", "WF", "WF", "WF", "WF", "WF", "WF", "WF", "WF", "WF", "WF", "WF", "W…
$ CAUSE <chr> "LTG", "LTG", "LTG", "LTG", "LTG", "LTG", "LTG", "LTG", "LTG", "LTG", "LTG", "LTG", "LTG", "…
$ HECTARES <dbl> 0.001780549, 0.002750173, 0.352157100, 0.040217169, 0.020073619, 0.804988998, 0.004530046, 0…
$ AREA <dbl> 17.805, 27.502, 3521.572, 402.172, 200.736, 8049.891, 45.301, 6037.401, 4024.610, 321.289, 3…
$ PERIMETER <dbl> 14.97941, 18.61561, 210.64806, 71.18618, 50.29261, 318.48152, 23.89166, 275.81270, 225.19107…
$ SEQ_NO <dbl> 157, 156, 155, 150, 151, 148, 153, 149, 152, 154, 140, 146, 130, 127, 129, 128, 131, 132, 14…
$ DECADE <int> 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 1930, 19…
$ STARTDATE <dttm> 1899-12-30, 1899-12-30, 1899-12-30, 1899-12-30, 1899-12-30, 1899-12-30, 1899-12-30, 1899-12…
$ OUTDATE <dttm> 1899-12-30, 1899-12-30, 1899-12-30, 1899-12-30, 1899-12-30, 1899-12-30, 1899-12-30, 1899-12…
$ X_COORD <dbl> 262412.7, 267727.9, 266014.8, 287444.7, 295170.0, 256659.1, 285626.0, 256657.1, 261539.1, 26…
$ Y_COORD <dbl> 4163816, 4166016, 4167194, 4197420, 4192392, 4198577, 4178935, 4197587, 4181555, 4171437, 41…
$ ET_ID <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ ORIG_FID <int> 1, 2, 3, 8, 7, 10, 5, 9, 6, 4, 18, 12, 28, 31, 29, 30, 27, 26, 14, 17, 23, 21, 32, 33, 25, 2…
$ Shape <POINT [m]> POINT (262412.7 4163816), POINT (267727.9 4166016), POINT (266014.8 4167194), POINT (2…
Import the park boundary and project it to UTM Zone 11N NAD83 (for plotting):
epsg_utm11n_nad83 <- 26911
yose_bnd_sf <- st_read(dsn="./data", layer="yose_boundary") |>
st_transform(epsg_utm11n_nad83)
Reading layer `yose_boundary' from data source
`D:\Workshops\R-Spatial\rspatial_mod\outputs\rspatial_bgs23\notebooks\data' using driver `ESRI Shapefile'
Simple feature collection with 1 feature and 11 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -119.8864 ymin: 37.4947 xmax: -119.1964 ymax: 38.18515
Geodetic CRS: North_American_Datum_1983
Plot them:
tm_shape(yose_bnd_sf) +
tm_borders() +
tm_shape(yose_fires_pt) +
tm_dots(col = "red", alpha = 0.5, size = 0.05)
If you want to compute descriptive stats on the attribute table,
without regard to the geometry, you can extract the attribute table as a
data frame with st_drop_geometry():
yose_fires_pt |> st_drop_geometry() |> head()
Q01. What is the distribution of acres burned?
TIP: To view the distribution, compute the quantiles or make a histogram plot.
## Because the 'ACRES' column is numeric, you can compute the quantitles with summary()
summary(yose_fires_pt$ACRES)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 0.10 0.10 92.21 0.99 78892.00
## Or:
quantile(yose_fires_pt$ACRES)
0% 25% 50% 75% 100%
0.002 0.099 0.100 0.994 78892.000
## Histogram
hist(yose_fires_pt$ACRES, breaks = 20)
Q02. Compute a frequency table of fire causes.
table(yose_fires_pt$CAUSE)
HC LTG MI
829 3355 248
#Or
yose_fires_pt |> st_drop_geometry() |> group_by(CAUSE) |> tally()
Q03. What was the largest fire?
yose_fires_pt |> st_drop_geometry() |> slice_max(ACRES, n=1)
Or:
yose_fires_pt |>
st_drop_geometry() |>
arrange(desc(ACRES)) |>
slice(1)
Q04. For each decade, compute the number of fires, and the mean fire size.
yose_fires_pt |>
st_drop_geometry() |>
group_by(DECADE) |>
summarise(count = n(), avg_size_acres = mean(ACRES))
Attribute queries can be performed using dplyr
filter().
Example: make a plot of the fire ignition points from the 1930s:
fires30s_sf <- yose_fires_pt |>
filter(DECADE == 1930)
head(fires30s_sf)
Simple feature collection with 6 features and 21 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 256659.1 ymin: 4163816 xmax: 295170 ymax: 4198577
Projected CRS: NAD83 / UTM zone 11N
OBJECTID FIRE_ID LINK_ID SACS_ID FILE_ID NAME ACRES YEAR TYPE CAUSE HECTARES AREA
1 1 YNP-2 1930YNP-2 YNP-002 0.004 1930 WF LTG 0.001780549 17.805
2 2 YNP-3 1930YNP-3 YNP-003 0.007 1930 WF LTG 0.002750173 27.502
3 3 YNP-7 1930YNP-7 YNP-007 0.870 1930 WF LTG 0.352157100 3521.572
4 8 YNP-10 1931YNP-10 YNP-010 Dog Lake\n 0.099 1931 WF LTG 0.040217169 402.172
5 7 YNP-11 1931YNP-11 YNP-011 Rafferty Creek\n 0.050 1931 WF LTG 0.020073619 200.736
6 10 YNP-15 1931YNP-15 YNP-015 Cottonwood Cree 1.989 1931 WF LTG 0.804988998 8049.891
PERIMETER SEQ_NO DECADE STARTDATE OUTDATE X_COORD Y_COORD ET_ID ORIG_FID Shape
1 14.97941 157 1930 1899-12-30 1899-12-30 262412.7 4163816 0 1 POINT (262412.7 4163816)
2 18.61561 156 1930 1899-12-30 1899-12-30 267727.9 4166016 0 2 POINT (267727.9 4166016)
3 210.64806 155 1930 1899-12-30 1899-12-30 266014.8 4167194 0 3 POINT (266014.8 4167194)
4 71.18618 150 1930 1899-12-30 1899-12-30 287444.7 4197420 0 8 POINT (287444.7 4197420)
5 50.29261 151 1930 1899-12-30 1899-12-30 295170.0 4192392 0 7 POINT (295170 4192392)
6 318.48152 148 1930 1899-12-30 1899-12-30 256659.1 4198577 0 10 POINT (256659.1 4198577)
tm_shape(yose_bnd_sf) +
tm_borders() +
tm_shape(fires30s_sf) +
tm_dots(col = "red", size = 0.2)
Q05. Make a plot of all the human caused fire ignition points.
fires_hc_sf <- yose_fires_pt |>
filter(CAUSE == 'HC')
tm_shape(yose_bnd_sf) +
tm_borders() +
tm_shape(fires_hc_sf) +
tm_dots(col = "red", size = 0.2)
Q06. Make a plot of all the human caused fire ignition points in the 1980s (only).
fires_hc80s_sf <- yose_fires_pt |>
filter(CAUSE == 'HC', DECADE == 1980)
tm_shape(yose_bnd_sf) +
tm_borders() +
tm_shape(fires_hc80s_sf) +
tm_dots(col = "red", size = 0.2) +
tm_layout(main.title = "Human Caused Fires During the 1980s",
main.title.size = 1)
Q07. Plot the human caused fires each decade (one plot per decade), for each decade from the 1980s onward:
Tip: See tm_facets()
fires_hc50s20_sf <- yose_fires_pt |>
filter(CAUSE == 'HC', DECADE >= 1980)
tm_shape(yose_bnd_sf) +
tm_borders() +
tm_shape(fires_hc50s20_sf) +
tm_dots(col = "red", size = 0.2) +
tm_facets(by = "DECADE") +
tm_layout(main.title = "Human Caused Fires Per Decade",
main.title.size = 1)