library(data.table)
library(sf)
library(terra)
library(ggplot2)
my_species <- "Quercus_ilex" Quercus ilex in Soria
Introduction
This report analyzes the spatial distribution, climate preferences, and productivity of Quercus ilex (32 plots) across the 606 IFN4 plots in Soria by integrating field data with satellite NDVI, climate rasters, and the Spanish Forest Map.
Task 1 — The plot table :
In this section, we load the field plot data, spatial rasters (NDVI, Temperature, and GDD), and vector maps. All data files are correctly read from the Data.R/ directory.
Read the data
# read-data :
# Read the inventory data and convert to a spatial object (sf)
ifn <- fread(file.path("Data.R", "CSV Soria.csv"))
plots <- st_as_sf(as.data.frame(ifn), coords = c("x", "y"), crs = 25830)
# Load the correct NDVI raster provided for the course
ndvi <- rast("Data.R/ndvi_cyl_2025.tif")[[1]]
names(ndvi) <- "ndvi"
# Load climate rasters
gdd <- rast("Data.R/Grados_dia_anuales_acumulados.tif")
names(gdd) <- "gdd"
temp <- rast("Data.R/Temperatura_Media_Anual.tif")
names(temp) <- "temp"
# Load the Spanish Forest Map (MFE) and province boundaries
mfe <- st_read("Data.R/mfe_soria.gpkg", quiet = TRUE)
mfe <- mfe[, c("TIPOBOSQUE", "Especie1")]
prov <- st_read("Data.R/provinces_cyl.gpkg", quiet = TRUE)NDVI, GDD and temperature at each plot
Next, we extract the environmental values for each plot. We must reproject the plots to match the Coordinate Reference System (CRS) of the NDVI raster before extraction to ensure accurate spatial overlay.
# Reproject plots to match the NDVI raster CRS before extracting
plots_sin <- st_transform(plots, crs(ndvi))
vals_ndvi <- extract(ndvi, vect(plots_sin))
# Add the NDVI values to the main table (the .tif values are already scaled 0-1)
ifn[, ndvi := vals_ndvi$ndvi]
# Extract GDD and Temperature (plots and these climate rasters share the same CRS)
vals_gdd <- extract(gdd, vect(plots))
vals_temp <- extract(temp, vect(plots))
ifn[, gdd := vals_gdd$gdd]
ifn[, temp := vals_temp$temp]Forest map attributes at each plot
# Extract forest map attributes using a spatial join
plots_mfe <- st_join(plots, mfe)
pm <- as.data.table(st_drop_geometry(plots_mfe))
# Keep only the first matched polygon per plot to avoid duplicates
pm_unique <- pm[, .(TIPOBOSQUE = TIPOBOSQUE[1], Especie1 = Especie1[1]), by = plot_id]
ifn[, TIPOBOSQUE := pm_unique$TIPOBOSQUE]
ifn[, Especie1 := pm_unique$Especie1]Save the table
# Save the final consolidated table to the working directory
fwrite(ifn, "ifn_soria_covariates.csv")Task 2 — Summary by species:
This table shows the mean and standard deviation of biomass, NDVI, and climate variables across all forest types. It gives us a baseline to compare the performance and habitat of Quercus ilex against the rest of the species in Soria.
# Group data by forest type to calculate species-specific means and standard deviations
summary_species <- ifn[, .(
n_plots = .N,
mean_biomass = mean(biomass_tha, na.rm = TRUE),
sd_biomass = sd(biomass_tha, na.rm = TRUE),
mean_ndvi = mean(ndvi, na.rm = TRUE),
mean_gdd = mean(gdd, na.rm = TRUE),
mean_temp = mean(temp, na.rm = TRUE)
), by = forest_type][order(-mean_biomass)]
summary_species forest_type n_plots mean_biomass sd_biomass mean_ndvi mean_gdd
<char> <int> <num> <num> <num> <num>
1: Fagus_sylvatica 9 206.574623 78.584538 0.8666222 1234.713
2: Pinus_sylvestris 241 145.225738 134.120694 0.7544344 1308.905
3: Pinus_nigra 106 138.987237 119.744635 0.6780245 1458.025
4: Mixed 11 94.550583 109.090563 0.5340182 1662.745
5: Pinus_pinaster 23 61.092005 35.915706 0.6261304 1632.367
6: Quercus_faginea 43 21.717863 21.836127 0.7407116 1503.188
7: Juniperus_thurifera 127 17.079125 18.607493 0.4933740 1590.469
8: Quercus_pyrenaica 14 9.646414 7.634802 0.5959286 1708.769
9: Quercus_ilex 32 5.575712 5.030916 0.5620344 1670.649
mean_temp
<num>
1: 7.795281
2: 8.879714
3: 9.605297
4: 10.186284
5: 9.887016
6: 9.513892
7: 9.644539
8: 10.297588
9: 10.014262
Interpretation : The summary table above highlights the wide variability across forest types in Soria. Conifers (such as Pinus sylvestris) dominate the upper ranks in terms of biomass production, whereas Quercus ilex exhibits the lowest mean biomass (5.58 t/ha), reflecting its slow-growing Mediterranean shrub-to-tree structure and adaptation to harsher climatic constraints.
Task 3 — My species vs. the rest:
Here, we group the data into two categories (“my species” vs. “others”) to directly compare Quercus ilex against the regional averages.
# Create a binary group for direct comparison
ifn[, group := ifelse(forest_type == my_species, "my species", "others")]
summary_group <- ifn[, .(
n_plots = .N,
mean_biomass = mean(biomass_tha, na.rm = TRUE),
sd_biomass = sd(biomass_tha, na.rm = TRUE),
mean_ndvi = mean(ndvi, na.rm = TRUE),
mean_gdd = mean(gdd, na.rm = TRUE),
mean_temp = mean(temp, na.rm = TRUE)
), by = group]
summary_group group n_plots mean_biomass sd_biomass mean_ndvi mean_gdd mean_temp
<char> <int> <num> <num> <num> <num> <num>
1: others 574 99.781116 119.089110 0.6700631 1441.626 9.313416
2: my species 32 5.575712 5.030916 0.5620344 1670.649 10.014262
Interpretation : This direct comparison isolates Quercus ilex (32 plots) from the rest of the regional forest inventory (574 plots). It clearly demonstrates that Quercus ilex thrives in significantly warmer thermal regimes, showing a higher mean annual temperature (10.01 °C vs. 9.31 °C) and accumulated growing degree days (1670.6 vs. 1441.6) compared to the regional baseline.
Task 4 — Figures
Map
This figure maps the geographical location of the field plots over the mean annual temperature raster, highlighting the specific spatial niche of Quercus ilex.
# fig-map
# "Mean Annual Temperature and Quercus ilex Plots in Soria"
# Prepare spatial data for Soria province
prov_utm <- st_transform(prov, 25830)
soria_utm <- prov_utm[prov_utm$NAME_2 == "Soria", ]
soria_v <- vect(soria_utm)
# Crop temperature raster to Soria for plotting
temp_soria <- mask(crop(temp, soria_v), soria_v)
temp_df <- as.data.frame(temp_soria, xy = TRUE, na.rm = TRUE)
# Sort plots so 'my species' is drawn last (on top) for visibility
plots_ordered <- st_as_sf(as.data.frame(ifn[order(group == "my species")]), coords = c("x", "y"), crs = 25830)
ggplot() +
geom_raster(data = temp_df, aes(x = x, y = y, fill = temp)) +
geom_sf(data = soria_utm, fill = NA, colour = "black") +
geom_sf(data = plots_ordered, aes(colour = group, size = group)) +
scale_fill_distiller(palette = "YlOrRd", direction = 1, name = "Mean Temp\n(°C)") +
scale_colour_manual(
values = c("my species" = "blue", "others" = "grey30"),
labels = c("my species" = expression(italic("Quercus ilex")), "others" = "Others"),
name = "Species Group"
) +
scale_size_manual(
values = c("my species" = 2.5, "others" = 0.6),
labels = c("my species" = expression(italic("Quercus ilex")), "others" = "Others"),
name = "Species Group"
) +
labs(
title = "Figure 1: Mean Annual Temperature and Quercus ilex Plots in Soria",
x = "Longitude",
y = "Latitude"
) +
theme_minimal()Description : The spatial distribution map shows that Quercus ilex plots (blue points) are primarily clustered in the warmer southern and southeastern sectors of Soria province, with a smaller secondary cluster located in the north-east, corresponding to local thermal pockets.
Distribution of one covariate
The boxplot visualizes the temperature distribution spread, confirming whether Quercus ilex occupies a broad or narrow climate niche compared to other forest types
# fig-boxplot
# "Distribution of Mean Annual Temperature by species group"
ggplot(ifn, aes(x = forest_type, y = temp, fill = group)) +
geom_boxplot() +
coord_flip() +
scale_fill_manual(
values = c("my species" = "blue", "others" = "grey70"),
labels = c("my species" = expression(italic("Quercus ilex")), "others" = "Others"),
name = "Species Group"
) +
labs(
title = "Figure 2: Distribution of Mean Annual Temperature by species",
x = "Forest Type",
y = "Mean Annual Temperature (°C)"
) +
theme_minimal()Description: The boxplot illustrates that Quercus ilex occupies a notably narrow and high-temperature thermal niche compared to most other forest types in the province, confirming its strict thermophilic requirements.
Biomass vs. NDVI:
This scatter plot examines if satellite greenness (NDVI) can serve as a reliable predictor for field-measured biomass.
# fig-scatter
# "Relationship between biomass and NDVI across IFN plots"
# Calculate correlations to answer Q4 quantitatively
ifn[, cor_val := cor(ndvi, biomass_tha, use = "complete.obs"), by = group]
print(unique(ifn[, .(group, cor_val)])) group cor_val
<char> <num>
1: others 0.3715632
2: my species 0.3446407
# Sort the data frame so 'my species' is drawn last and clearly visible
ifn_plot <- ifn[order(group == "my species")]
ggplot(ifn_plot, aes(x = ndvi, y = biomass_tha, colour = group, size = group)) +
geom_point(alpha = 0.8) +
scale_colour_manual(
values = c("my species" = "red", "others" = "grey70"),
labels = c("my species" = expression(italic("Quercus ilex")), "others" = "Others"),
name = "Species Group"
) +
scale_size_manual(
values = c("my species" = 3, "others" = 1),
labels = c("my species" = expression(italic("Quercus ilex")), "others" = "Others"),
name = "Species Group"
) +
labs(
title = "Figure 3: Relationship between biomass and NDVI across IFN plots",
x = "NDVI (unitless, 0-1)",
y = "Biomass (t/ha)"
) +
theme_minimal()Description: The scatter plot reveals a weak relationship between satellite-derived NDVI and field biomass across the plots. For Quercus ilex (red points), the dispersion highlights that optical saturation and background reflectance limit the ability of NDVI to accurately predict standing timber volume.
Task 5 — Interpretation
1. Where is my species?
Based on the map in Figure 1, Quercus ilex is not spread evenly across the entire IFN sampling grid in Soria. It is primarily concentrated in the southern and southeastern parts of the sampled area, with a distinct, smaller group of plots located in the north-east.
2. In which conditions does it grow?
Based on the Task 3 summary table, Quercus ilex grows under distinctly warmer climatic conditions compared to the regional average, showing higher mean annual temperatures (10.01 °C vs. 9.31 °C for others) and higher GDD (1670.6 vs. 1441.6). As shown in the Figure 2 box plot, its temperature range is relatively narrow and clustered at the warmer end of the spectrum.
3. How productive is it?
In terms of productivity, Quercus ilex ranks last among all 9 forest types listed in the Task 2 table. It exhibits a very low mean biomass of 5.58 t/ha (SD = 5.03 t/ha). This is significantly lower than major coniferous species like Pinus sylvestris (145.23 t/ha), reflecting its typical slow-growing Mediterranean shrub-to-tree nature.
4. Can a satellite see it?
Predicting biomass accurately from NDVI for these plots is very difficult. Judging from the Figure 3 scatter plot, the points do not form a tight line. When calculated, the correlation between NDVI and biomass is weak for Quercus ilex (and other species). Because NDVI often saturates in dense canopies and varies due to understory or soil color, it does not reliably predict the vast differences in biomass directly.
5. Map vs. inventory
# Calculate match percentage excluding NA map values
mfe_match_pct <- ifn[forest_type == my_species & !is.na(Especie1),
mean(gsub(" ", "_", Especie1) == gsub(" ", "_", my_species)) * 100]
mfe_match_pct[1] 90.32258
When comparing the field inventory with the Spanish Forest Map, the spatial agreement for Quercus ilex is 90.3%. This means that out of the 31 valid plots mapped, 28 agreed perfectly (excluding 1 plot that had missing/NA map data).
Use of AI
AI suggested the use of the gsub() function in Task 5 to standardize plant species names (replacing spaces with underscores) between the IFN data and the MFE map, ensuring accurate programmatic matching.