Advertisement
ggranga

tutorial_plot_speclib

Mar 18th, 2020
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.31 KB | None | 0 0
  1.  
  2. ## Create and parse a speclib object
  3. ## LR
  4.  
  5. library(hsdar)
  6.  
  7. data(spectral_data) # load sample data
  8.  
  9. # Extract objects from sample data
  10. # (these should be created/imported by the user, in a standard workflow)
  11. spectra <- spectra(spectral_data)       # spectra (matrix dim n×m)
  12. wavelength <- wavelength(spectral_data) # wavelenghts (vector dim m)
  13. ids <- idSpeclib(spectral_data)         # IDs (character dim n)
  14. SI <- SI(spectral_data)                 # supplemetary info (data.frame dim n×x)
  15.  
  16. # Create a speclib object  
  17. spectra_speclib <- speclib(spectra, wavelength)
  18. idSpeclib(spectra_speclib) <- as.character(ids) # (optional)
  19. SI(spectra_speclib) <- SI # (optional)
  20.  
  21. # Create a "long" data.table (to be used with ggplot)
  22. library(data.table)
  23. spectra_long <- data.table(
  24.   "spectra" = as.vector(spectra),
  25.   "wavelength" = rep(wavelength, each = length(ids)),
  26.   "id" = rep(as.character(ids), length(wavelength))
  27. )
  28. # Create summary statistics from spectra
  29. spectra_summary <- spectra_long[
  30.   ,list(
  31.     spectra_avg = mean(spectra),
  32.     spectra_sd = sd(spectra),
  33.     spectra_med = median(spectra),
  34.     spectra_q1 = quantile(spectra, .25),
  35.     spectra_q3 = quantile(spectra, .75)
  36.   ),
  37.   by = "wavelength"]
  38.  
  39.  
  40. ## Examples of ggplot plots (with the equivalent plot.speclib)
  41. library(ggplot2)
  42.  
  43. # Default plot (average and standard deviation)
  44. plot(spectra_speclib, main = "Default Plot")
  45. ggplot(spectra_summary, aes(x = wavelength)) +
  46.   geom_ribbon(aes(ymin = spectra_avg-spectra_sd, ymax = spectra_avg+spectra_sd), alpha = .2) +
  47.   geom_line(aes(y = spectra_avg)) +
  48.   xlab("Wavelength (nm)") + ylab("Reflectance (%)") + ggtitle("Default plot") +
  49.   theme_light()
  50.  
  51. # Median (and quartiles)
  52. plot(spectra_speclib, FUN = "median", main = "Median spectrum")
  53. ggplot(spectra_summary, aes(x = wavelength)) +
  54.   geom_ribbon(aes(ymin = spectra_q1, ymax = spectra_q3), alpha = .2) +
  55.   geom_line(aes(y = spectra_med)) +
  56.   xlab("Wavelength (nm)") + ylab("Reflectance (%)") + ggtitle("Median spectrum") +
  57.   theme_light()
  58.  
  59. # Specific spectrum
  60. plot(spectra_speclib, FUN = 1, main = "First spectrum of Speclib")
  61. ggplot(spectra_long[id==1,], aes(x = wavelength)) +
  62.   geom_ribbon(data = spectra_summary, aes(ymin = spectra_q1, ymax = spectra_q3), alpha = .2) +
  63.   geom_line(aes(y = spectra), colour = "blue", size = .75) +
  64.   xlab("Wavelength (nm)") + ylab("Reflectance (%)") + ggtitle("First spectrum of Speclib") +
  65.   theme_light()
  66.  
  67. # More spectra
  68. plot(spectra_speclib, FUN = 1, col = "red", main = "Three spectra")
  69. plot(spectra_speclib, FUN = 2, col = "blue", new = FALSE)
  70. plot(spectra_speclib, FUN = 3, col = "orange", new = FALSE)
  71. ggplot(spectra_long[id %in% c(1:3),], aes(x = wavelength)) +
  72.   geom_ribbon(data = spectra_summary, aes(ymin = spectra_q1, ymax = spectra_q3), alpha = .2) +
  73.   geom_line(aes(y = spectra, colour = id)) +
  74.   xlab("Wavelength (nm)") + ylab("Reflectance (%)") + ggtitle("Three spectra") +
  75.   theme_light()
  76.  
  77. # More spectra with panels
  78. ggplot(spectra_long[id %in% c(1:3),], aes(x = wavelength)) +
  79.   geom_ribbon(data = spectra_summary, aes(ymin = spectra_q1, ymax = spectra_q3), alpha = .2) +
  80.   geom_line(data = spectra_summary, aes(y = spectra_med), alpha = .2) +
  81.   geom_line(aes(y = spectra)) +
  82.   facet_wrap(~id) +
  83.   xlab("Wavelength (nm)") + ylab("Reflectance (%)") + ggtitle("Three spectra") +
  84.   theme_light()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement