ribbon: Add a polygon to a plot

View source: R/ribbon.R

ribbonR Documentation

Add a polygon to a plot

Description

A slight modification to polygon with quality of life improvements to make it easier to add colored confidence intervals to a plot. Default values are taken for all polygon arguments, except for border, which is set to NA. Additionally, an alpha argument has been added to accommodate partially transparent confidence intervals. Any of the input values can be overwritten within this function's call.

Usage

ribbon(
  x,
  y,
  density = NULL,
  angle = 45,
  border = NA,
  col = NA,
  lty = par("lty"),
  ...,
  fillOddEven = FALSE,
  alpha = NULL
)

Arguments

x

vector containing the x-axis coordinates of the polygon vertices. See details for more information..

y

Either a vector containing the y-axis coordinates of the polygon vertices, or a two column data.frame / matrix with the vertices. See details for more information.

density

Set to NULL. See polygon.

angle

Set to 45. See polygon.

border

Set to NA. See polygon.

col

Set to NA. See polygon.

lty

Set to par("lty"). See polygon.

...

Additional arguments such as xpd, lend, ljoin, and lmitre can be given as arugments.

fillOddEven

Set to FALSE. See polygon.

alpha

new alpha level in [0,1]. If col is a HEX color that already includes an alpha channel, the alpha argument will be ignored.

Details

If y is a two column data.frame or matrix, ribbon will convert y to a vector such that y = c(y[,1], rev(y[,2])) in order to create the lower and upper bounds of the polygon. Additionally, when y is a two column data.frame or matrix, x can have the same length as the number of rows in y, and ribbon will concatenate the reverse of the vector x to ensure it has equal length.

Examples

## Not run: 
   # Load data
   data(cars)
   # fit model
   m1 <- lm(
     dist ~ speed,
     data = cars
   )
   # make predictions
   preds <- predict(
     m1, 
     newdata = data.frame(speed = 10:25),
     interval = "confidence"
   )
   # base plot
   blank(
     xlim = c(10,25),
     ylim = c(15,120),
     xlab = "Speed",
     ylab = "Stopping distance",
     xaxt = "s",
     yaxt = "s",
     bty = "l",
     las = 1
   )
   # add 95% confidence interval
   ribbon(
     x=10:25,
     y=preds[,c("lwr","upr")],
     col = "purple",
     alpha = 0.5
   )
   # add mean prediction
   lines(
     x=10:25,
     y = preds[,"fit"],
     lwd =2,
     col = "purple"
   )
   # add data
   points(
     x = cars$speed,
     y = cars$dist,
     pch = 16
   )

## End(Not run)


dapperstats/bbplot documentation built on June 11, 2022, 8:38 p.m.