/
data-documentation.R
1522 lines (1474 loc) · 53.3 KB
/
data-documentation.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##
## document data here
## don't forget: @usage data(XXX)
#' @title Lookup tables for sand, silt, clay, texture class, and textural modifiers.
#'
#' @description A list that contains a snapshot of the values generated using the logic from
#' the particle size estimator calculation in NASIS, the average values per
#' texture class, and average rock fragment values by textural modifier.
#'
#' @usage data(soiltexture)
#' @format A list with 3 data frames. The first named values which contains
#' values for sand, silt and clay by texture class. The second with average
#' values for sand, silt and clay per texture class. The third has fragvoltot
#' low, rv and high values for texmod. \describe{ \item{list("clay")}{clay
#' percentage of the fine earth fraction, a integer vector}
#' \item{list("sand")}{sand percentage of the fine earth fraction, a integer
#' vector} \item{list("silt")}{silt percentage of the fine earth fraction, a
#' integer vector} \item{list("texcl")}{texture class, a character vector}
#' \item{list("texmod")}{textural modifiers, a character vector} }
#' @keywords datasets
"soiltexture"
#' @title Example Output from soilDB::fetchOSD()
#' @description An example `SoilProfileCollection` object created by `soilDB::fetchOSD()`, derived from the Cecil, Appling, and Bonneau Official Series Descriptions.
#' @keywords datasets
#' @usage data(osd)
#' @format A `SoilProfileCollection`
"osd"
#' @title Example SoilProfileCollection with Overlapping Horizons
#' @description A `SoilProfileCollection` with overlapping horizons, derived from a Dynamic Soil Properties project.
#' @keywords datasets
#' @usage data(SPC.with.overlap)
#' @format A `SoilProfileCollection`
"SPC.with.overlap"
#' @title Example Data from Wilson et al. 2022
#' @description An example `SoilProfileCollection`, derived from Wilson et al., 2022. Select data extracted from Appendix tables.
#' @keywords datasets
#' @usage data(wilson2022)
#' @format A `SoilProfileCollection` with the following elements. Total elemental analysis by lithium borate fusion.
#'
#' Horizon level attributes:
#' * name: horizon designation
#' * Al2O3: total Al (wt %)
#' * Fe2O3: total Fe (wt %)
#' * K2O: total K (wt %)
#' * MgO: total Mg (wt %)
#' * Na2O: total Na (wt %)
#' * P2O5: total P (wt %)
#' * SiO2: total Si (wt %)
#' * CaO: total Ca(wt %)
#' * Alo: Oxalate Extractable Al (g/kg)
#' * Feo: Oxalate Extractable Fe (g/kg)
#' * Fed: Dithionite extractable Fe (g/kg)
#' * Fed_minus_Feo: Crystalline Fe (hydr)oxides (g/kg)
#' * CIA: Chemical Index of Alteration, see original paper (ratio, unitless)
#' * Fed_div_Fet: (ratio, unitless)
#' * Fet: Total Fe from lithium borate fusion (g/kg)
#' * resin_Pi: Hedley phosphorus fractions (mg/kg)
#' * NaHCO3_Pi: Hedley phosphorus fractions (mg/kg)
#' * labile_Pi: Sum of resin Pi and NaHCO3 Pi (mg/kg)
#' * NaCO3_Po: Hedley phosphorus fractions (mg/kg)
#' * NaOH_Pi: Hedley phosphorus fractions (mg/kg)
#' * NaOH_Po: Hedley phosphorus fractions (mg/kg)
#' * Ca_Pi: Hedley phosphorus fractions (mg/kg)
#' * organic_P: Sum of NaHCO3 and NaOH Po fractions (mg/kg)
#' * total_P: Total P from lithium borate fusion (mg/kg)
#' * occluded_P: Difference between total P and sum of Hedley P fractions (mg/kg)
#' * top: horizon top depth (cm)
#' * bottom: horizon bottom depth (cm)
#' * pedonID: pedon ID (serial number)
#'
#' Site level attributes:
#' * pm: parent material group
#' * biome: biome
#'
#' @references
#' Stewart G. Wilson, Randy A. Dahlgren, Andrew J. Margenot, Craig Rasmussen, Anthony T. O'Geen. 2022. Expanding the Paradigm: The influence of climate and lithology on soil phosphorus, Geoderma: 421. \doi{10.1016/j.geoderma.2022.115809}
#'
#'
#'
#' @examples
#'
#' data(wilson2022)
#'
#' groupedProfilePlot(wilson2022, groups = 'pm',
#' group.name.offset = -15, label = 'biome',
#' name.style = 'center-center', color = 'CIA',
#' cex.names = 0.66, cex.id = 0.66, width = 0.3,
#' depth.axis = FALSE, hz.depths = TRUE)
#'
#' groupedProfilePlot(wilson2022, groups = 'biome',
#' group.name.offset = -15, label = 'pm',
#' name.style = 'center-center', color = 'Fet',
#' cex.names = 0.66, cex.id = 0.66, width = 0.3,
#' depth.axis = FALSE, hz.depths = TRUE)
#'
"wilson2022"
#' Soil Profile Data Example 1
#'
#' Soil profile data from Pinnacles National Monument, CA.
#'
#'
#' @name sp1
#' @docType data
#' @format A data frame with 60 observations on the following 21 variables.
#' \describe{ \item{group}{a numeric vector} \item{id}{a
#' character vector} \item{top}{a numeric vector}
#' \item{bottom}{a numeric vector} \item{bound_distinct}{a
#' character vector} \item{bound_topography}{a character vector}
#' \item{name}{a character vector} \item{texture}{a character
#' vector} \item{prop}{a numeric vector}
#' \item{structure_grade}{a character vector}
#' \item{structure_size}{a character vector}
#' \item{structure_type}{a character vector}
#' \item{stickiness}{a character vector} \item{plasticity}{a
#' character vector} \item{field_ph}{a numeric vector}
#' \item{hue}{a character vector} \item{value}{a numeric
#' vector} \item{chroma}{a numeric vector} }
#' @references \url{https://casoilresource.lawr.ucdavis.edu/}
#' @keywords datasets
#' @examples
#'
#' data(sp1)
#' # convert colors from Munsell to hex-encoded RGB
#' sp1$soil_color <- with(sp1, munsell2rgb(hue, value, chroma))
#'
#' # promote to SoilProfileCollection
#' depths(sp1) <- id ~ top + bottom
#' site(sp1) <- ~ group
#'
#' # re-sample each profile into 1 cm (thick) depth slices
#' # for the variables 'prop', 'name', 'soil_color'
#' # result is a SoilProfileCollection object
#' s <- dice(sp1, 0:25 ~ prop + name + soil_color)
#'
#' # plot, note slices
#' plot(s)
#'
#'
#' # aggregate all profiles along 1 cm depth slices,
#' # using data from column 'prop'
#' s1 <- slab(sp1, fm= ~ prop)
#'
#' # check median & IQR
#' library(lattice)
#' xyplot(top ~ p.q50 + p.q25 + p.q75,
#' data=s1, type='S', horizontal=TRUE, col=1, lty=c(1,2,2),
#' panel=panel.superpose, ylim=c(110,-5), asp=2)
#'
#'
NULL
#' Honcut Creek Soil Profile Data
#'
#' A collection of 18 soil profiles, consisting of select soil morphologic
#' attributes, associated with a stratigraphic study conducted near Honcut
#' Creek, California.
#'
#'
#' @name sp2
#' @docType data
#' @format A data frame with 154 observations on the following 21 variables.
#' \describe{ \item{id}{profile id} \item{surface}{dated
#' surface} \item{top}{horizon top in cm} \item{bottom}{horizon
#' bottom in cm} \item{bound_distinct}{horizon lower boundary
#' distinctness class} \item{bound_topography}{horizon lower boundary
#' topography class} \item{name}{horizon name}
#' \item{texture}{USDA soil texture class}
#' \item{prop}{field-estimated clay content}
#' \item{structure_grade}{soil structure grade}
#' \item{structure_size}{soil structure size}
#' \item{structure_type}{soil structure type}
#' \item{stickiness}{stickiness} \item{plasticity}{plasticity}
#' \item{field_ph}{field-measured pH} \item{hue}{Munsell hue}
#' \item{value}{Munsell value} \item{chroma}{Munsell chroma}
#' \item{r}{RGB red component} \item{g}{RGB green component}
#' \item{b}{RGB blue component} \item{soil_color}{R-friendly
#' encoding of soil color} }
#' @author Dylan E. Beaudette
#' @references \url{https://casoilresource.lawr.ucdavis.edu/}
#' @source Busacca, Alan J.; Singer, Michael J.; Verosub, Kenneth L. 1989. Late
#' Cenozoic stratigraphy of the Feather and Yuba rivers area, California, with
#' a section on soil development in mixed alluvium at Honcut Creek. USGS
#' Bulletin 1590-G.
#' @keywords datasets
#' @examples
#'
#' # keep examples from using more than 2 cores
#' data.table::setDTthreads(Sys.getenv("OMP_THREAD_LIMIT", unset = 2))
#'
#' data(sp2)
#'
#' # convert into SoilProfileCollection object
#' depths(sp2) <- id ~ top + bottom
#'
#' # transfer site-level data
#' site(sp2) <- ~ surface
#'
#' # generate a new plotting order, based on the dated surface each soil was described on
#' p.order <- order(sp2$surface)
#'
#' # plot
#' par(mar=c(1,0,3,0))
#' plot(sp2, plot.order=p.order)
#'
#' # setup multi-figure output
#' par(mfrow=c(2,1), mar=c(0,0,1,0))
#'
#' # truncate plot to 200 cm depth
#' plot(sp2, plot.order=p.order, max.depth=200)
#' abline(h=200, lty=2, lwd=2)
#'
#' # compute numerical distances between profiles
#' # based on select horizon-level properties, to a depth of 200 cm
#' d <- NCSP(sp2, vars=c('prop','field_ph','hue'), maxDepth = 100, k = 0)
#'
#' # plot dendrogram with ape package:
#' if(require(ape) & require(cluster)) {
#' h <- diana(d)
#' p <- as.phylo(as.hclust(h))
#' plot(p, cex=0.75, label.offset=0.01, font=1, direct='down', srt=90, adj=0.5, y.lim=c(-0.125, 0.5))
#'
#' # add in the dated surface type via color
#' tiplabels(col=as.numeric(sp2$surface), pch=15)
#'
#' # based on distance matrix values, YMMV
#' legend('topleft', legend=levels(sp2$surface), col=1:6, pch=15, bty='n', bg='white', cex=0.75)
#' }
#'
#'
NULL
#' Soil Profile Data Example 3
#'
#' Soil samples from 10 soil profiles, taken from the Sierra Foothill Region of
#' California.
#'
#' These data were collected to support research funded by the Kearney
#' Foundation of Soil Science.
#'
#' @name sp3
#' @docType data
#' @format A data frame with 46 observations on the following 15 variables.
#' \describe{ \item{id}{soil id} \item{top}{horizon upper
#' boundary (cm)} \item{bottom}{horizon lower boundary (cm)}
#' \item{clay}{clay content} \item{cec}{CEC by amonium acetate
#' at pH 7} \item{ph}{pH in 1:1 water-soil mixture}
#' \item{tc}{total carbon percent} \item{hue}{Munsell hue
#' (dry)} \item{value}{Munsell value (dry)}
#' \item{chroma}{Munsell chroma (dry)} \item{mid}{horizon
#' midpoint (cm)} \item{ln_tc}{natural log of total carbon percent}
#' \item{L}{color: l-coordinate, CIE-LAB colorspace (dry)}
#' \item{A}{color: a-coordinate, CIE-LAB colorspace (dry)}
#' \item{B}{color: b-coordinate, CIE-LAB colorspace (dry)}
#' \item{name}{horizon name} \item{soil_color}{horizon color} }
#' @keywords datasets
#' @references \url{https://casoilresource.lawr.ucdavis.edu/}
#' @examples
#'
#' ## this example investigates the concept of a "median profile"
#'
#' # required packages
#' if (require(ape) & require(cluster)) {
#' data(sp3)
#'
#' # generate a RGB version of soil colors
#' # and convert to HSV for aggregation
#' sp3$h <- NA
#' sp3$s <- NA
#' sp3$v <- NA
#' sp3.rgb <- with(sp3, munsell2rgb(hue, value, chroma, return_triplets = TRUE))
#'
#' sp3[, c('h', 's', 'v')] <- t(with(sp3.rgb, rgb2hsv(r, g, b, maxColorValue = 1)))
#'
#' # promote to SoilProfileCollection
#' depths(sp3) <- id ~ top + bottom
#'
#' # aggregate across entire collection
#' a <- slab(sp3, fm = ~ clay + cec + ph + h + s + v, slab.structure = 10)
#'
#' # check
#' str(a)
#'
#' # convert back to wide format
#' library(data.table)
#'
#' a.wide.q25 <- dcast(as.data.table(a), top + bottom ~ variable, value.var = c('p.q25'))
#' a.wide.q50 <- dcast(as.data.table(a), top + bottom ~ variable, value.var = c('p.q50'))
#' a.wide.q75 <- dcast(as.data.table(a), top + bottom ~ variable, value.var = c('p.q75'))
#'
#' # add a new id for the 25th, 50th, and 75th percentile pedons
#' a.wide.q25$id <- 'Q25'
#' a.wide.q50$id <- 'Q50'
#' a.wide.q75$id <- 'Q75'
#'
#' # combine original data with "mean profile"
#' vars <- c('top', 'bottom', 'id', 'clay', 'cec', 'ph', 'h', 's', 'v')
#' # make data.frame version of sp3
#' sp3.df <- as(sp3, 'data.frame')
#'
#' sp3.grouped <- as.data.frame(rbind(as.data.table(horizons(sp3))[, .SD, .SDcol = vars],
#' a.wide.q25[, .SD, .SDcol = vars],
#' a.wide.q50[, .SD, .SDcol = vars],
#' a.wide.q75[, .SD, .SDcol = vars]))
#'
#' # re-constitute the soil color from HSV triplets
#' # convert HSV back to standard R colors
#' sp3.grouped$soil_color <- with(sp3.grouped, hsv(h, s, v))
#'
#' # give each horizon a name
#' sp3.grouped$name <- paste(
#' round(sp3.grouped$clay),
#' '/' ,
#' round(sp3.grouped$cec),
#' '/',
#' round(sp3.grouped$ph, 1)
#' )
#'
#'
#' # first promote to SoilProfileCollection
#' depths(sp3.grouped) <- id ~ top + bottom
#'
#' plot(sp3.grouped)
#'
#' ## perform comparison, and convert to phylo class object
#' ## D is rescaled to [0,]
#' d <- NCSP(
#' sp3.grouped,
#' vars = c('clay', 'cec', 'ph'),
#' maxDepth = 100,
#' k = 0.01
#' )
#'
#' h <- agnes(d, method = 'ward')
#' p <- ladderize(as.phylo(as.hclust(h)))
#'
#' # look at distance plot-- just the median profile
#' plot_distance_graph(d, 12)
#'
#' # similarity relative to median profile (profile #12)
#' round(1 - (as.matrix(d)[12, ] / max(as.matrix(d)[12, ])), 2)
#'
#' ## make dendrogram + soil profiles
#'
#' # setup plot: note that D has a scale of [0,1]
#' par(mar = c(1, 1, 1, 1))
#' p.plot <- plot(p,
#' cex = 0.8,
#' label.offset = 3,
#' direction = 'up',
#' y.lim = c(200, 0),
#' x.lim = c(1.25, length(sp3.grouped) + 1),
#' show.tip.label = FALSE)
#'
#' # get the last plot geometry
#' lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
#'
#' # the original labels, and new (indexed) order of pedons in dendrogram
#' d.labels <- attr(d, 'Labels')
#'
#' new_order <- sapply(1:lastPP$Ntip,
#' function(i)
#' which(as.integer(lastPP$xx[1:lastPP$Ntip]) == i))
#'
#' # plot the profiles, in the ordering defined by the dendrogram
#' # with a couple fudge factors to make them fit
#' plotSPC(
#' sp3.grouped,
#' color = "soil_color",
#' plot.order = new_order,
#' y.offset = max(lastPP$yy) + 10,
#' width = 0.1,
#' cex.names = 0.5,
#' add = TRUE
#' )
#' }
NULL
#' Soil Chemical Data from Serpentinitic Soils of California
#'
#' Soil Chemical Data from Serpentinitic Soils of California
#'
#' Selected soil physical and chemical data from (McGahan et al., 2009).
#'
#' @name sp4
#' @docType data
#' @format A data frame with 30 observations on the following 13 variables.
#' \describe{ \item{id}{site name} \item{name}{horizon
#' designation} \item{top}{horizon top boundary in cm}
#' \item{bottom}{horizon bottom boundary in cm}
#' \item{K}{exchangeable K in c mol/kg} \item{Mg}{exchangeable
#' Mg in cmol/kg} \item{Ca}{exchangeable Ca in cmol/kg}
#' \item{CEC_7}{cation exchange capacity (NH4OAc at pH 7)}
#' \item{ex_Ca_to_Mg}{extractable Ca:Mg ratio} \item{sand}{sand
#' content by weight percentage} \item{silt}{silt content by weight
#' percentage} \item{clay}{clay content by weight percentage}
#' \item{CF}{>2mm fraction by volume percentage} }
#' @references McGahan, D.G., Southard, R.J, Claassen, V.P. 2009.
#' Plant-Available Calcium Varies Widely in Soils on Serpentinite Landscapes.
#' Soil Sci. Soc. Am. J. 73: 2087-2095.
#' @source https://www.soils.org/publications/sssaj/articles/73/6/2087
#' @keywords datasets
#' @examples
#'
#' # load sample data set, a simple data.frame object with horizon-level data from 10 profiles
#' library(aqp)
#' data(sp4)
#' str(sp4)
#' sp4$idbak <- sp4$id
#'
#' # upgrade to SoilProfileCollection
#' # 'id' is the name of the column containing the profile ID
#' # 'top' is the name of the column containing horizon upper boundaries
#' # 'bottom' is the name of the column containing horizon lower boundaries
#' depths(sp4) <- id ~ top + bottom
#'
#' # check it out
#' class(sp4) # class name
#' str(sp4) # internal structure
#'
#' # check integrity of site:horizon linkage
#' spc_in_sync(sp4)
#'
#' # check horizon depth logic
#' checkHzDepthLogic(sp4)
#'
#' # inspect object properties
#' idname(sp4) # self-explanitory
#' horizonDepths(sp4) # self-explanitory
#'
#' # you can change these:
#' depth_units(sp4) # defaults to 'cm'
#' metadata(sp4) # not much to start with
#'
#' # alter the depth unit metadata
#' depth_units(sp4) <- 'inches' # units are really 'cm'
#'
#' # more generic interface for adjusting metadata
#'
#' # add attributes to metadata list
#' metadata(sp4)$describer <- 'DGM'
#' metadata(sp4)$date <- as.Date('2009-01-01')
#' metadata(sp4)$citation <- 'McGahan, D.G., Southard, R.J, Claassen, V.P.
#' 2009. Plant-Available Calcium Varies Widely in Soils
#' on Serpentinite Landscapes. Soil Sci. Soc. Am. J. 73: 2087-2095.'
#'
#' depth_units(sp4) <- 'cm' # fix depth units, back to 'cm'
#'
#' # further inspection with common function overloads
#' length(sp4) # number of profiles in the collection
#' nrow(sp4) # number of horizons in the collection
#' names(sp4) # column names
#' min(sp4) # shallowest profile depth in collection
#' max(sp4) # deepest profile depth in collection
#'
#' # extraction of soil profile components
#' profile_id(sp4) # vector of profile IDs
#' horizons(sp4) # horizon data
#'
#' # extraction of specific horizon attributes
#' sp4$clay # vector of clay content
#'
#' # subsetting SoilProfileCollection objects
#' sp4[1, ] # first profile in the collection
#' sp4[, 1] # first horizon from each profile
#'
#' # basic plot method, highly customizable: see manual page ?plotSPC
#' plot(sp4)
#' # inspect plotting area, very simple to overlay graphical elements
#' abline(v=1:length(sp4), lty=3, col='blue')
#' # profiles are centered at integers, from 1 to length(obj)
#' axis(1, line=-1.5, at=1:10, cex.axis=0.75, font=4, col='blue', lwd=2)
#' # y-axis is based on profile depths
#' axis(2, line=-1, at=pretty(1:max(sp4)), cex.axis=0.75, font=4, las=1, col='blue', lwd=2)
#'
#'
#' # symbolize soil properties via color
#' par(mar=c(0,0,4,0))
#' plot(sp4, color='clay')
#' plot(sp4, color='CF')
#'
#' # apply a function to each profile, returning a single value per profile,
#' # in the same order as profile_id(sp4)
#' soil.depths <- profileApply(sp4, max) # recall that max() gives the depth of a soil profile
#'
#' # check that the order is correct
#' all.equal(names(soil.depths), profile_id(sp4))
#'
#' # a vector of values that is the same length as the number of profiles
#' # can be stored into site-level data
#' sp4$depth <- soil.depths
#' # check: looks good
#' max(sp4[1, ]) == sp4$depth[1]
#'
#' # extract site-level data
#' site(sp4) # as a data.frame
#' sp4$depth # specific columns as a vector
#'
#' # use site-level data to alter plotting order
#' new.order <- order(sp4$depth) # the result is an index of rank
#' par(mar=c(0,0,0,0))
#' plot(sp4, plot.order=new.order)
#'
#' # deconstruct SoilProfileCollection into a data.frame, with horizon+site data
#' as(sp4, 'data.frame')
#'
NULL
#' Sample Soil Database #5
#'
#' 296 Soil Profiles from the La Rochelle region of France (F. Carre and
#' Girard, 2002)
#'
#' These data are c/o F. Carre (Florence.CARRE@ineris.fr).
#'
#' @name sp5
#' @docType data
#' @format `SoilProfileCollection` object
#' @references F. Carre, M.C. Girard. 2002. Quantitative mapping of soil types
#' based on regression kriging of taxonomic distances with landform and land
#' cover attributes. Geoderma. 110: 241--263.
#' @source 296 Soil Profiles from the La Rochelle region of France (F. Carre
#' and Girard, 2002). These data can be found on the OSACA project page
#' (\url{http://eusoils.jrc.ec.europa.eu/projects/OSACA/}).
#' @keywords datasets
#' @examples
#'
#' \dontrun{
#' library(scales)
#' data(sp5)
#' par(mar=c(1,1,1,1))
#' # plot a random sampling of profiles
#' s <- sample(1:length(sp5), size=25)
#' plot(sp5[s, ], divide.hz=FALSE)
#'
#' # plot the first 100 profiles, as 4 rows of 25, hard-coding the max depth
#' layout(matrix(c(1,2,3,4), ncol=1), height=c(0.25,0.25,0.25,0.25))
#' plot(sp5[1:25, ], max.depth=300)
#' plot(sp5[26:50, ], max.depth=300)
#' plot(sp5[51:75, ], max.depth=300)
#' plot(sp5[76:100, ], max.depth=300)
#'
#'
#' # 4x1 matrix of plotting areas
#' layout(matrix(c(1,2,3,4), ncol=1), height=c(0.25,0.25,0.25,0.25))
#'
#' # plot profiles, with points added to the mid-points of randomly selected horizons
#' sub <- sp5[1:25, ]
#' plot(sub, max.depth=300) ; mtext('Set 1', 2, line=-0.5, font=2)
#' y.p <- profileApply(sub, function(x) {
#' s <- sample(1:nrow(x), 1)
#' h <- horizons(x); with(h[s,], (top+bottom)/2)
#' })
#' points(1:25, y.p, bg='white', pch=21)
#'
#' # plot profiles, with arrows pointing to profile bottoms
#' sub <- sp5[26:50, ]
#' plot(sub, max.depth=300); mtext('Set 2', 2, line=-0.5, font=2)
#' y.a <- profileApply(sub, function(x) max(x))
#' arrows(1:25, y.a-50, 1:25, y.a, len=0.1, col='white')
#'
#' # plot profiles, with points connected by lines: ideally reflecting some kind of measured data
#' sub <- sp5[51:75, ]
#' plot(sub, max.depth=300); mtext('Set 3', 2, line=-0.5, font=2)
#' y.p <- 20*(sin(1:25) + 2*cos(1:25) + 5)
#' points(1:25, y.p, bg='white', pch=21)
#' lines(1:25, y.p, lty=2)
#'
#' # plot profiles, with polygons connecting horizons with max clay content (+/-) 10 cm
#' sub <- sp5[76:100, ]
#' y.clay.max <- profileApply(sub, function(x) {
#' i <- which.max(x$clay)
#' h <- horizons(x)
#' with(h[i, ], (top+bottom)/2)
#' } )
#'
#' plot(sub, max.depth=300); mtext('Set 4', 2, line=-0.5, font=2)
#' polygon(c(1:25, 25:1), c(y.clay.max-10, rev(y.clay.max+10)),
#' border='black', col=rgb(0,0,0.8, alpha=0.25))
#' points(1:25, y.clay.max, pch=21, bg='white')
#'
#' # close plot
#' dev.off()
#'
#'
#' # plotting parameters
#' yo <- 100 # y-offset
#' sf <- 0.65 # scaling factor
#' # plot profile sketches
#' par(mar=c(0,0,0,0))
#' plot(sp5[1:25, ], max.depth=300, y.offset=yo, scaling.factor=sf)
#' # optionally add describe plotting area above profiles with lines
#' # abline(h=c(0,90,100, (300*sf)+yo), lty=2)
#' # simulate an environmental variable associated with profiles (elevation, etc.)
#' r <- vector(mode='numeric', length=25)
#' r[1] <- -50 ; for(i in 2:25) {r[i] <- r[i-1] + rnorm(mean=-1, sd=25, n=1)}
#' # rescale
#' r <- rescale(r, to=c(80, 0))
#' # illustrate gradient with points/lines/arrows
#' lines(1:25, r)
#' points(1:25, r, pch=16)
#' arrows(1:25, r, 1:25, 95, len=0.1)
#' # add scale for simulated gradient
#' axis(2, at=pretty(0:80), labels=rev(pretty(0:80)), line=-1, cex.axis=0.75, las=2)
#' # depict a secondary environmental gradient with polygons (water table depth, etc.)
#' polygon(c(1:25, 25:1), c((100-r)+150, rep((300*sf)+yo, times=25)),
#' border='black', col=rgb(0,0,0.8, alpha=0.25))
#'
#' }
#'
#'
NULL
#' Soil Physical and Chemical Data from Manganiferous Soils
#'
#' Soil Physical and Chemical Data from Manganiferous Soils (Bourgault and
#' Rabenhorst, 2011)
#'
#' Selected soil physical and chemical data from (Bourgault and Rabenhorst,
#' 2011).
#'
#' @name sp6
#' @docType data
#' @format A data frame with 30 observations on the following 13 variables.
#' \describe{ \item{id}{pedon name} \item{name}{horizon
#' designation} \item{top}{horizon top boundary in cm}
#' \item{bottom}{horizon bottom boundary in cm}
#' \item{color}{moist soil color in Munsell notation}
#' \item{texture}{USDA soil texture class} \item{sand}{sand
#' content by weight percentage} \item{silt}{silt content by weight
#' percentage} \item{clay}{clay content by weight percentage}
#' \item{Fe}{DCB-extracted Fe in g/kg (see citation)}
#' \item{Mn}{DCB-extracted Mn in g/kg (see citation)}
#' \item{C}{total organic carbon as g/kg} \item{pH}{measured in
#' 1:1 H20 slurry} \item{Db}{bulk density (g/cc), clod method} }
#' @references Rebecca R. Bourgault, Martin C. Rabenhorst. 2011. Genesis and
#' characterization of manganiferous soils in the Eastern Piedmont, USA.
#' Geoderma. 165:84-94.
#' @source http://www.sciencedirect.com/science/article/pii/S0016706111001972
#' @keywords datasets
#' @examples
#'
#' # setup environment
#' library(aqp)
#' data(sp6)
#'
#' # init SPC
#' depths(sp6) <- id ~ top + bottom
#' # convert non-standard Munsell colors
#' sp6$soil_color <- getClosestMunsellChip(sp6$color)
#'
#' # profile sketches
#' par(mar=c(0,0,3,0))
#' plot(sp6, color='soil_color')
#' plot(sp6, color='Mn')
#' plot(sp6, color='Fe')
#' plot(sp6, color='pH')
#' plot(sp6, color='texture')
#'
#'
NULL
#' Soil Physical and Chemical Data Related to Studies in the Sierra Nevada
#' Mountains, CA, USA.
#'
#' Soil physical and chemical data associated with two bio-climatic sequences
#' (granitic and andesitic parent material) from the western flank of the
#' Sierra Nevada mountains.
#'
#' These data were assembled from Dahlgren et al. (1997) and Rasmussen et al.
#' (2007), with permission granted by lead authors, by D.E. Beaudette.
#'
#' @name sierraTransect
#' @docType data
#' @usage data(sierraTransect)
#'
#' @references R.A. Dahlgren, J.L. Boettinger, G.L. Huntington, R.G. Amundson.
#' Soil development along an elevational transect in the western Sierra Nevada,
#' California, Geoderma, Volume 78, Issues 3–4, 1997, Pages 207-236.
#'
#' Rasmussen, C., Matsuyama, N., Dahlgren, R.A., Southard, R.J. and Brauer, N.
#' (2007), Soil Genesis and Mineral Transformation Across an Environmental
#' Gradient on Andesitic Lahar. Soil Sci. Soc. Am. J., 71: 225-237.
#' @source Original manuscripts and personal communication with authors.
#' @keywords datasets
#' @examples
#'
#' data(sierraTransect)
#'
#' # tighter margins
#' op <- par(mar=c(0,0,0,0))
#'
#' # quick sketch
#' plotSPC(sierraTransect, name.style = 'center-center', width=0.3)
#'
#' # split by transect
#' par(mar=c(0,0,1,1))
#' groupedProfilePlot(
#' sierraTransect, groups='transect',
#' group.name.offset = -15, width=0.3,
#' name.style='center-center'
#' )
#'
#' # thematic
#' groupedProfilePlot(
#' sierraTransect, groups='transect',
#' group.name.offset = -15, width=0.3,
#' name.style='center-center', color='Fe_o_to_Fe_d'
#' )
#'
#' # horizon boundary viz
#' sierraTransect$hzd <- hzDistinctnessCodeToOffset(substr(sierraTransect$hz_boundary, 0, 1))
#'
#' groupedProfilePlot(
#' sierraTransect, groups='transect', group.name.offset = -15,
#' width=0.3, name.style='center-center', color='Fe_o_to_Fe_d',
#' hz.distinctness.offset='hzd')
#'
#'
#' # split transects
#' g <- subset(sierraTransect, transect == 'Granite')
#' a <- subset(sierraTransect, transect == 'Andesite')
#'
#' g.order <- order(g$elev)
#' a.order <- order(a$elev)
#'
#' # order (left -> right) by elevation
#' par(mar=c(2,0,0,2), mfrow=c(2,1))
#' plot(g, width=0.3, name.style='center-center', cex.names=0.75, plot.order=g.order)
#' axis(1, at=1:length(g), labels=g$elev[g.order], line=-1.5)
#'
#' plot(a, width=0.3, name.style='center-center', cex.names=0.75, plot.order=a.order)
#' axis(1, at=1:length(a), labels=a$elev[a.order], line=-1.5)
#'
#'
#' par(op)
#'
"sierraTransect"
#' Soil Data from the Central Sierra Nevada Region of California
#'
#' Site and laboratory data from soils sampled in the central Sierra Nevada
#' Region of California.
#'
#' These data were extracted from the NSSL database. `ca630` is a list composed
#' of site and lab data, each stored as `data.frame` objects. These data are modeled by a
#' 1:many (site:lab) relation, with the `pedon_id` acting as the primary key in
#' the `site` table and as the foreign key in the `lab` table.
#'
#' @name ca630
#' @docType data
#' @usage data(ca630)
#' @format List containing:
#'
#' $site : A data frame containing site information. \describe{
#' \item{user_site_id}{national user site id} \item{mlra}{the
#' MLRA} \item{county}{the county} \item{ssa}{soil survey area}
#' \item{lon}{longitude, WGS84} \item{lat}{latitude, WGS84}
#' \item{pedon_key}{national soil profile id}
#' \item{user_pedon_id}{local soil profile id}
#' \item{cntrl_depth_to_top}{control section top depth (cm)}
#' \item{cntrl_depth_to_bot}{control section bottom depth (cm)}
#' \item{sampled_taxon_name}{soil series name} }
#'
#' $lab : A data frame containing horizon information. \describe{
#' \item{pedon_key}{national soil profile id}
#' \item{layer_key}{national horizon id}
#' \item{layer_sequence}{horizon sequence number}
#' \item{hzn_top}{horizon top (cm)} \item{hzn_bot}{horizon
#' bottom (cm)} \item{hzn_desgn}{horizon name}
#' \item{texture_description}{USDA soil texture}
#' \item{nh4_sum_bases}{sum of bases extracted by ammonium acetate (pH
#' 7)} \item{ex_acid}{exchangeable acidity \[method ?]}
#' \item{CEC8.2}{cation exchange capacity by sum of cations method (pH
#' 8.2)} \item{CEC7}{cation exchange capacity by ammonium acetate (pH
#' 7)} \item{bs_8.2}{base saturation by sum of cations method (pH 8.2)}
#' \item{bs_7}{base saturation by ammonium acetate (pH 7)} }
#' @note These data are out of date. Pending some new data + documentation. Use
#' with caution
#' @source \url{https://ncsslabdatamart.sc.egov.usda.gov/}
#' @keywords datasets
#' @examples
#'
#' \dontrun{
#' library(tactile)
#' library(lattice)
#' library(Hmisc)
#' library(sp)
#'
#' # check the data out:
#' data(ca630)
#' str(ca630)
#'
#' # note that pedon_key is the link between the two tables
#'
#' # make a copy of the horizon data
#' ca <- ca630$lab
#'
#' # promote to a SoilProfileCollection class object
#' depths(ca) <- pedon_key ~ hzn_top + hzn_bot
#'
#' # add site data, based on pedon_key
#' site(ca) <- ca630$site
#'
#' # ID data missing coordinates: '|' is a logical OR
#' (missing.coords.idx <- which(is.na(ca$lat) | is.na(ca$lon)))
#'
#' # remove missing coordinates by safely subsetting
#' if(length(missing.coords.idx) > 0)
#' ca <- ca[-missing.coords.idx, ]
#'
#' # register spatial data
#' initSpatial(ca) <- ~ lon + lat
#'
#' # assign a coordinate reference system
#' prj(ca) <- 'EPSG:4269'
#'
#' # check the result
#' print(ca)
#'
#' # aggregate %BS 7 for all profiles into 1 cm slices
#' a <- slab(ca, fm= ~ bs_7)
#'
#' # plot median & IQR by 1 cm slice
#' xyplot(
#' top ~ p.q50,
#' data = a,
#' lower=a$p.q25,
#' upper=a$p.q75,
#' alpha=0.5,
#' ylim=c(160,-5),
#' scales = list(alternating = 1, y = list(tick.num = 7)),
#' panel = panel.depth_function,
#' prepanel = prepanel.depth_function,
#' ylab='Depth (cm)', xlab='Base Saturation at pH 7',
#' par.settings = tactile.theme(superpose.line = list(col = 'black', lwd = 2))
#' )
#'
#' # aggregate %BS at pH 8.2 for all profiles by MLRA, along 1 cm slices
#' # note that mlra is stored in @site
#' a <- slab(ca, mlra ~ bs_8.2)
#'
#' # keep only MLRA 18 and 22
#' a <- subset(a, subset=mlra %in% c('18', '22'))
#'
#' # plot median & IQR by 1 cm slice, using different colors for each MLRA
#' xyplot(
#' top ~ p.q50,
#' groups = factor(mlra),
#' data = a,
#' lower=a$p.q25,
#' upper=a$p.q75,
#' alpha=0.25,
#' sync.colors = TRUE,
#' ylim=c(160,-5),
#' scales = list(alternating = 1, y = list(tick.num = 7)),
#' panel = panel.depth_function,
#' prepanel = prepanel.depth_function,
#' ylab='Depth (cm)', xlab='Base Saturation at pH 7',
#' par.settings = tactile.theme(superpose.line = list(lwd = 2)),
#' auto.key = list(lines = TRUE, points = FALSE, columns = 2)
#' )
#'
#'
#'
#' # Extract the 2nd horizon from all profiles as SPDF
#' ca.2 <- ca[, 2]
#'
#' # subset profiles 1 through 10
#' ca.1.to.10 <- ca[1:10, ]
#'
#' # basic plot method: profile plot
#' par(mar = c(0, 0, 3, 1))
#' plotSPC(ca.1.to.10, name='hzn_desgn', color = 'CEC7')
#' }
#'
"ca630"
#' Soil Morphologic, Geochemical, and Mineralogy Data from Rowley et al. 2019.
#'
#' Data from Table 1 and Supplementary Tables 1 and 2 from "A cascading
#' influence of calcium carbonate on the biogeochemistry and pedogenic
#' trajectories of subalpine soils, Switzerland".
#'
#'
#' @name rowley2019
#' @docType data
#' @usage data(rowley2019)
#' @format A \code{SoilProfileCollection} object:
#'
#' site-level attributes \describe{ \item{id}{profile ID} \item{group}{profile
#' group} }
#'
#' horizon-level attributes \describe{ \item{sample_id}{sample ID}
#' \item{name}{horizon name} \item{pH}{pH} \item{Al_exch}{cmol(+) / kg,
#' exchangeable Al} \item{Ca_exch}{cmol(+) / kg, exchangeable Ca}
#' \item{CEC_sum}{cmol(+) / kg, cation exchange capacity calculated as the sum
#' of exchangeable cations, not including H+}
#' \item{Ca_exch_saturation}{percent} \item{Al_exch_saturation}{percent}
#' \item{TON}{percent, total nitrogen} \item{SOC}{percent, soil organic carbon}
#' \item{C_to_N}{carbon to nitrogen ratio} \item{Alo}{g/kg, oxalate-extractable
#' Al} \item{Feo}{g/kg, oxalate-extractable Fe} \item{Ald}{g/kg,
#' dithionite-extractable Al} \item{Fed}{g/kg, dithionite-extractable Fe}
#' \item{Feo_Fed}{Fe_o to Fe_d ratio} \item{id}{profile ID} \item{top}{horizon
#' top (cm)} \item{bottom}{horizon bottom (cm)} \item{Al}{g/kg by x-ray
#' fluorescence} \item{Ca}{g/kg by x-ray fluorescence} \item{Cr}{g/kg by x-ray
#' fluorescence} \item{Fe}{g/kg by x-ray fluorescence} \item{K}{g/kg by x-ray
#' fluorescence} \item{Mg}{g/kg by x-ray fluorescence} \item{Mn}{g/kg by x-ray
#' fluorescence} \item{Na}{g/kg by x-ray fluorescence} \item{Ni}{g/kg by x-ray
#' fluorescence} \item{P}{g/kg by x-ray fluorescence} \item{Si}{g/kg by x-ray
#' fluorescence} \item{Ti}{g/kg by x-ray fluorescence}
#' \item{Phyllosilicates}{percent by x-ray diffraction spectra}
#' \item{Quartz}{percent by x-ray diffraction spectra}
#' \item{K_Feldspar}{percent by x-ray diffraction spectra}
#' \item{Na_Plagioclase}{percent by x-ray diffraction spectra}
#' \item{Goethite}{percent by x-ray diffraction spectra}
#' \item{Unidentified}{percent by x-ray diffraction spectra}
#' \item{CCE_Total}{percent} \item{CCE_Reactive}{percent}
#' \item{Reactive_carbonate}{percent} \item{Sand}{percent <2um}
#' \item{Silt}{percent 2-50um} \item{Clay}{percent 50-2000um}
#'
#' \item{CaH2O}{Milliq ex: grams of Ca per kilogram of dry soil (g kg-1)}
#' \item{Ca2MKCl}{2M KCl: grams of Ca per kilogram of dry soil (g kg-1)}
#' \item{CaNa2EDTA}{0.05 M Na2EDTA: grams of Ca per kilogram of dry soil (g
#' kg-1)} \item{CaCuCl2}{0.5 M CuCl2: grams of Ca per kilogram of dry soil (g
#' kg-1)}
#'
#' \item{hzID}{horizon ID} }
#' @references Mike C. Rowley, Stephanie Grand, Thierry Adatte, Eric P.
#' Verrecchia, Cascading influence of calcium carbonate on the biogeochemistry and
#' pedogenic trajectories of subalpine soils), Switzerland, Geoderma, 2019,
#' 114065, ISSN 0016-7061, \doi{10.1016/j.geoderma.2019.114065}.
#' @keywords datasets
#' @examples
#'
#' library(lattice)
#'
#' # load data
#' data('rowley2019')
#'
#' # check first 5 rows and 10 columns of horizon data
#' horizons(rowley2019)[1:5, 1:10]
#'
#' # check site data
#' site(rowley2019)
#'
#' # graphical summary
#' par(mar=c(1,1,3,1))
#' plotSPC(rowley2019, color='Feo_Fed', name='name', cex.names=0.85)
#'
#' plotSPC(rowley2019, color='Ca_exch', name='name', cex.names=0.85)
#'
#' # grouped plot
#' groupedProfilePlot(rowley2019, groups = 'group', color='Ca_exch',
#' name='name', cex.names=0.85, group.name.offset = -10)
#'
#' # aggregate over 1cm slices, for select properties
#' a <- slab(rowley2019, group ~ Reactive_carbonate + Ca_exch + pH + K_Feldspar + Na_Plagioclase + Al)
#'
#' # plot styling
#' tps <- list(superpose.line=list(lwd=2, col=c('royalblue', 'firebrick')))
#'
#' # make the figure
#' xyplot(top ~ p.q50 | variable, data=a, ylab='Depth', groups=group,
#' main='', as.table=TRUE,
#' xlab='median bounded by 25th and 75th percentiles',
#' lower=a$p.q25, upper=a$p.q75, ylim=c(55,-5),
#' panel=panel.depth_function,
#' prepanel=prepanel.depth_function,
#' cf=a$contributing_fraction,
#' alpha=0.33, sync.colors=TRUE,
#' scales=list(x=list(relation='free', alternating=1)),
#' par.settings=tps,
#' auto.key=list(columns=2, lines=TRUE, points=FALSE),
#' strip=strip.custom(bg=grey(0.9))
#' )
#'
#'
"rowley2019"
#' Soil Morphologic Data from Jacobs et al. 2002.
#'
#' Select soil morphologic data from "Redoximorphic Features as Indicators of
#' Seasonal Saturation, Lowndes County, Georgia". This is a useful sample
#' dataset for testing the analysis and visualization of redoximorphic
#' features.