Add function to a plot

Description

Add function to a plot

Usage

type_function(fun = dnorm, args = list(), n = 101, ...)

Arguments

fun Function of x to plot. Defaults to dnorm.
args Additional arguments to be passed to fun
n Number of points to interpolate on the x axis.
Additional arguments are passed to the lines() function, ex: type=“p”, col=“pink”.

Details

When using type_function() in a tinyplot() call, the x value indicates the range of values to plot on the x-axis.

Examples

library("tinyplot")

# Plot the normal density
tinyplot(x = -4:4, type = type_function(dnorm))

# Extra arguments for the function to plot
tinyplot(x = -1:10, type = type_function(
  fun = dnorm, args = list(mean = 3)
))

# Additional arguments are passed to the `lines()` function.
tinyplot(x = -4:4, type = type_function(
  fun = dnorm,
  col = "pink", type = "p", pch = 3
))

# Custom function example
# (Here using the `\()` anonymous function syntax introduced in R 4.1.0)
tinyplot(x = -4:4, type = type_function(fun = \(x) 0.5 * exp(-abs(x))))