https://github.com/brownag/ggspc
Custom 'ggplot2' 'Stat', 'Geom', and 'theme' definitions for the 'aqp' 'SoilProfileCollection' object
https://github.com/brownag/ggspc
aqp ggplot2 graphics plot r soilprofilecollection
Last synced: 12 months ago
JSON representation
Custom 'ggplot2' 'Stat', 'Geom', and 'theme' definitions for the 'aqp' 'SoilProfileCollection' object
- Host: GitHub
- URL: https://github.com/brownag/ggspc
- Owner: brownag
- License: gpl-3.0
- Created: 2023-02-02T01:14:50.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-01-29T17:59:05.000Z (over 1 year ago)
- Last Synced: 2025-04-02T18:15:48.444Z (about 1 year ago)
- Topics: aqp, ggplot2, graphics, plot, r, soilprofilecollection
- Language: R
- Homepage:
- Size: 740 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE.md
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
fig.retina = 3
)
```
# {ggspc}
[](https://github.com/brownag/ggspc/actions/workflows/R-CMD-check.yml)
[](https://humus.rocks/ggspc/)
[](https://CRAN.R-project.org/package=ggspc)
[](https://app.codecov.io/gh/brownag/ggspc?branch=main)
The goal of {ggspc} is to provide custom 'Stat', 'Geom' and 'theme' definitions for 'SoilProfileCollection' object compatibility with 'ggplot2'.
## Installation
You can install the development version of {ggspc} like so:
``` r
remotes::install_github("brownag/ggspc")
```
## Example
This a example shows how to solve the common problem of plotting variables contained in a `SoilProfileCollection` with `ggplot2::ggplot()`
```{r example}
library(aqp)
library(ggspc)
library(ggplot2)
data(loafercreek, package = "soilDB")
GHL(loafercreek) <- "dspcomplayerid"
```
### Basics
This is a demonstration of what is possible with a simple `fortify()` method defined. The "fortify" method makes it such that names from horizon and site slots of the SPC can be used in `ggplot()` aesthetics via `aes()`.
```{r basic-spc-ggplot}
# site v.s. site level
ggplot(loafercreek, aes(earthcovkind1, slope_field)) +
geom_boxplot(na.rm = TRUE)
```
```{r basic-spc-ggplot2}
# site v.s. horizon level
ggplot(loafercreek, aes(hillslopeprof, clay)) +
geom_boxplot(na.rm = TRUE)
```
```{r basic-spc-ggplot3}
# horizon v.s. horizon level
ggplot(loafercreek, aes(clay, dspcomplayerid)) +
geom_boxplot(na.rm = TRUE)
```
### Depth Weighted Aggregation (`stat_depth_weighted()`)
The `stat_depth_weighted()` function is a specialized {ggplot2} statistic intended for calculation of depth-weighted values for horizon data in a `SoilProfileCollection`. The default uses a constant interval `from=0` `to=200` (centimeters), but the intervals of interest may alternately be specified as site-level column names (unquoted), and therefore may vary between profiles.
Currently, `stat_depth_weighted()` only supports the "point" geometry type, but in future "boxplot" and others may be supported.
```{r stat-depth-weighted-spc}
# default y aesthetic is the profile_id()
ggplot(loafercreek[1:10], aes(clay)) +
stat_depth_weighted(na.rm = TRUE)
```
```{r stat-depth-weighted-spc2}
# can use alternate y aesthetic, e.g. hillslopeprof
ggplot(loafercreek, aes(clay, hillslopeprof)) +
stat_depth_weighted(na.rm = TRUE)
```
```{r stat-depth-weighted-spc3}
# continuous y axes works too (horizon v.s. horizon)
ggplot(loafercreek, aes(clay, sand)) +
stat_depth_weighted(na.rm = TRUE)
```
```{r stat-depth-weighted-spc4}
# continuous y (horizon v.s. site)
ggplot(loafercreek, aes(clay, slope_field)) +
stat_depth_weighted(na.rm = TRUE)
```
```{r stat-depth-weighted-spc5}
# can combine with typical ggplot geoms (0-200cm mean over boxplots)
ggplot(loafercreek, aes(clay, hillslopeprof)) +
geom_boxplot(na.rm = TRUE) +
stat_depth_weighted(na.rm = TRUE, col = "red", pch = 17, cex = 3)
```
```{r stat-depth-weighted-spc6}
# use site-level columns for profile-specific intervals (e.g. PSCS)
ggplot() +
stat_depth_weighted(
loafercreek,
aes(clay, hillslopeprof),
na.rm = TRUE,
from = psctopdepth,
to = pscbotdepth
)
```