Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Create and parse a speclib object
- ## LR
- library(hsdar)
- data(spectral_data) # load sample data
- # Extract objects from sample data
- # (these should be created/imported by the user, in a standard workflow)
- spectra <- spectra(spectral_data) # spectra (matrix dim n×m)
- wavelength <- wavelength(spectral_data) # wavelenghts (vector dim m)
- ids <- idSpeclib(spectral_data) # IDs (character dim n)
- SI <- SI(spectral_data) # supplemetary info (data.frame dim n×x)
- # Create a speclib object
- spectra_speclib <- speclib(spectra, wavelength)
- idSpeclib(spectra_speclib) <- as.character(ids) # (optional)
- SI(spectra_speclib) <- SI # (optional)
- # Create a "long" data.table (to be used with ggplot)
- library(data.table)
- spectra_long <- data.table(
- "spectra" = as.vector(spectra),
- "wavelength" = rep(wavelength, each = length(ids)),
- "id" = rep(as.character(ids), length(wavelength))
- )
- # Create summary statistics from spectra
- spectra_summary <- spectra_long[
- ,list(
- spectra_avg = mean(spectra),
- spectra_sd = sd(spectra),
- spectra_med = median(spectra),
- spectra_q1 = quantile(spectra, .25),
- spectra_q3 = quantile(spectra, .75)
- ),
- by = "wavelength"]
- ## Examples of ggplot plots (with the equivalent plot.speclib)
- library(ggplot2)
- # Default plot (average and standard deviation)
- plot(spectra_speclib, main = "Default Plot")
- ggplot(spectra_summary, aes(x = wavelength)) +
- geom_ribbon(aes(ymin = spectra_avg-spectra_sd, ymax = spectra_avg+spectra_sd), alpha = .2) +
- geom_line(aes(y = spectra_avg)) +
- xlab("Wavelength (nm)") + ylab("Reflectance (%)") + ggtitle("Default plot") +
- theme_light()
- # Median (and quartiles)
- plot(spectra_speclib, FUN = "median", main = "Median spectrum")
- ggplot(spectra_summary, aes(x = wavelength)) +
- geom_ribbon(aes(ymin = spectra_q1, ymax = spectra_q3), alpha = .2) +
- geom_line(aes(y = spectra_med)) +
- xlab("Wavelength (nm)") + ylab("Reflectance (%)") + ggtitle("Median spectrum") +
- theme_light()
- # Specific spectrum
- plot(spectra_speclib, FUN = 1, main = "First spectrum of Speclib")
- ggplot(spectra_long[id==1,], aes(x = wavelength)) +
- geom_ribbon(data = spectra_summary, aes(ymin = spectra_q1, ymax = spectra_q3), alpha = .2) +
- geom_line(aes(y = spectra), colour = "blue", size = .75) +
- xlab("Wavelength (nm)") + ylab("Reflectance (%)") + ggtitle("First spectrum of Speclib") +
- theme_light()
- # More spectra
- plot(spectra_speclib, FUN = 1, col = "red", main = "Three spectra")
- plot(spectra_speclib, FUN = 2, col = "blue", new = FALSE)
- plot(spectra_speclib, FUN = 3, col = "orange", new = FALSE)
- ggplot(spectra_long[id %in% c(1:3),], aes(x = wavelength)) +
- geom_ribbon(data = spectra_summary, aes(ymin = spectra_q1, ymax = spectra_q3), alpha = .2) +
- geom_line(aes(y = spectra, colour = id)) +
- xlab("Wavelength (nm)") + ylab("Reflectance (%)") + ggtitle("Three spectra") +
- theme_light()
- # More spectra with panels
- ggplot(spectra_long[id %in% c(1:3),], aes(x = wavelength)) +
- geom_ribbon(data = spectra_summary, aes(ymin = spectra_q1, ymax = spectra_q3), alpha = .2) +
- geom_line(data = spectra_summary, aes(y = spectra_med), alpha = .2) +
- geom_line(aes(y = spectra)) +
- facet_wrap(~id) +
- xlab("Wavelength (nm)") + ylab("Reflectance (%)") + ggtitle("Three spectra") +
- theme_light()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement