Learn R Programming

GCubeR (version 0.1.3)

plot_by_class: Summarise and plot standing volume by c130 class and species

Description

This function builds a cross-tabulated volume table by species and c130 classes, adds a TOTAL row per class, optionally exports the table as a CSV, and returns a ggplot object showing the volume distribution by c130 class.

Usage

plot_by_class(
  data,
  volume_col = "dagnelie_vc22_2",
  breaks = seq(30, 230, by = 25),
  small_limit = 60,
  medium_limit = 120,
  output = NULL,
  make_plot = TRUE
)

Value

A list with two components:

  • table: data frame with species as rows and c130 classes as columns, plus a TOTAL row.

  • plot: a ggplot2 object (or NULL if make_plot = FALSE).

Arguments

data

A data frame containing at least:

  • c130: stem circumference at 1.30 m (cm),

  • species_code: species identifier,

  • a volume column (defaults to "dagnelie_vc22_2").

volume_col

Name of the column containing tree volume (string). Defaults to "dagnelie_vc22_2".

breaks

Numeric vector defining c130 class boundaries (cm). Default is seq(30, 230, by = 25).

small_limit

Threshold between small and medium wood (cm of c130). A vertical dashed line is drawn at this value in the plot. Default is 60.

medium_limit

Threshold between medium and large wood (cm of c130). A vertical dashed line is drawn at this value in the plot. Default is 120.

output

Optional file path where the cross-tabulated table should be exported as a CSV. If NULL (default), no file is written. Export is handled by the utility function export_output().

make_plot

Logical; if TRUE (default), a ggplot object is created and returned alongside the table.

Details

The table has:

  • rows = species (plus a "TOTAL" row),

  • columns = c130 classes (e.g. [30,55), [55,80), ...),

  • cells = summed volume per species and c130 class.

The plot shows a volume-weighted histogram (or barchart) by c130 class, stacked by species, with a trend line for total volume per class and dashed vertical lines marking small, medium and large wood thresholds.

The c130 classes are built with cut() using breaks as class boundaries and an open-ended last class (using Inf as the upper bound). The resulting factor labels (e.g. "[30,55)") are used as column names in the cross-tabulated table.

For the plot, volume is used as a weight so that bar heights represent total volume per c130 class. A trend line is computed from total volume per class midpoint using the same binning scheme.

Examples

Run this code
set.seed(123)
n <- 150
c130 <- runif(n, 30, 230)
htot <- 0.25 * c130 + rnorm(n, 0, 3)
htot <- pmax(5, pmin(htot, 45))

species_list <- c(
  "PINUS_SYLVESTRIS", "PICEA_ABIES",
  "QUERCUS_ROBUR", "FAGUS_SYLVATICA", "BETULA_SP"
)
species_code <- sample(species_list, n, replace = TRUE)

df <- data.frame(
  c130 = round(c130, 1),
  htot = round(htot, 1),
  species_code = species_code
)

df <- dagnelie_vc22_2(df)
res <- plot_by_class(df, volume_col = "dagnelie_vc22_2")

res$table

print(res$plot)

Run the code above in your browser using DataLab