knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = TRUE, message=FALSE, warning=FALSE
)
library(riverbed)
library(dplyr)
library(ggplot2)

Example datasets

data(s1)
data(s2)

We consider two longitudinal series $s_1=(l_1,z_1)$, and $s_2=(l_2,z_2)$:

result_area <- area_between(s1,s2)

ggplot(filter(result_area$data, !is.na(series)),
       aes(x=l_obs,y=z_obs,color=series)) +
  geom_line()+
  geom_point()

Principle of the area calculation

To calculate the area between the curves we need to interpolate the data and calculate intersects between the two curves:

ggplot(result_area$data,
       aes(x=l_obs,y=z_obs,color=series)) +
  geom_line()+
  geom_point()+
  geom_point(data=filter(result_area$data,
                         p=="interpolated",
                         series=="s1"),
             aes(x=l,y=z1),shape="|",size=3)+
  geom_point(data=filter(result_area$data,
                         p=="interpolated",
                         series=="s2"),
             aes(x=l,y=z2),shape="|",size=3)+
  geom_point(data=filter(result_area$data,
                         p=="intersect"),
             aes(x=l,y=z1), col="dark blue",shape=0)

Use of area_between()

This, as well as the calculation of corresponding trapezia, is realised by the function area_between().

result_area <- area_between(s1,s2)
result_area

The output of the function corresponds to

Note that it is also possible to calculate the area between one longitudinal series $s_1=(l_1,z_1)$ and a constant horizontal value $h$.

result_area_h <- area_between(s1,h=225)

Plot of area

plot_area(result_area)

If the distinction between upper and lower area is irrelevant then it is possible not to distinguish these two types on the plot:

plot_area(result_area,distinguish_type=FALSE)

The plotting function plot_area() can also be used with just one series and constant horizontal level $h$, as calculated earlier:

plot_area(result_area_h)


lvaudor/riverbed documentation built on Feb. 25, 2023, 3:47 p.m.