create_segments: Create a data frame for plotting piecewise-linear segments

View source: R/create_segments.R

create_segmentsR Documentation

Create a data frame for plotting piecewise-linear segments

Description

Functions can be approximated by a sequence of piecewise-linear segments. This function generates the information needed for plotting those segments, as you might if the piecewise linear approximation were plotted on top of the slice_plot() of the function.

Usage

create_segments(ftilde, domain, h = NULL, nsegs = 20)

Arguments

ftilde

a tilde expression, as in slice_plot()

domain

specification of a domain, as in slice_plot()

h

an alternative to nsegs, giving the length of each segment.

nsegs

how many segments to divide the domain into

Value

a data frame (see examples)

Examples

## Not run: 
fun <- sin(x) ~ x
dom <- bounds(x = c(-5,5))
segments <- create_segments(fun, domain=dom, nsegs=20)
slice_plot(fun, dom) %>%
  # show the slope function as slopes
  gf_segment(y + yend ~ x + xend, data = segments, color=~slope) %>%
  gf_refine(scale_color_viridis_c())

slice_plot(fun, dom) %>%
  # piecewise linear approximations
  gf_segment(yf + yfend ~ x + xend, data = segments, color=~slope,
  size=2, alpha=0.5) %>%
  gf_refine(scale_color_viridis_c())

slice_plot(fun, dom) %>%
  # show the approximate slope function
  gf_line(slope ~ x , data = segments, color=~slope) %>%
  gf_refine(scale_color_viridis_c())


## End(Not run)


dtkaplan/Znotes documentation built on Sept. 4, 2022, 10:21 a.m.