create_dimming_fn {rearrr}R Documentation

Create dimming_fn function

Description

[Experimental]

Creates a function that takes 2 inputs (`x`, `d`) and performs the operation:

x * (numerator / ((add_to_distance + d) ^ exponent))

Here, `x` is the current value and `d` is its distance to an origin. The greater the distance, the more we will dim the value of `x`.

With the default values, the returned function is:

function(x, d){

⁠ ⁠x * (1 / ((1 + d) ^ 2))

}

Usage

create_dimming_fn(numerator = 1, exponent = 2, add_to_distance = 1)

Arguments

numerator

The numerator. Defaults to 1.

exponent

The exponent. Defaults to 2.

add_to_distance

Constant to add to the distance before exponentiation. Ensures dimming even when the distance (d) is below 1. Defaults to 1.

Value

Function with the arguments x and d, with both expected to be numeric vectors. More specifically:

function(x, d){

⁠ ⁠x * (numerator / ((add_to_distance + d) ^ exponent))

}

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other function creators: create_n_fn(), create_origin_fn()

Examples

# Attach packages
library(rearrr)
has_ggplot <- require(ggplot2)  # Attach if installed

# Set seed
set.seed(1)

# Create two vectors
x <- runif(10)
d <- runif(10, max = 0.5)

# Create dimming_fn with an add_to_distance of 0
# Note: In practice this risks zero-division
non_smoothed_dimming_fn <- create_dimming_fn(add_to_distance = 0)
non_smoothed_dimming_fn
as.list(environment(non_smoothed_dimming_fn))

# Use median_origin_fn
non_smoothed_dimming_fn(x, d)

# Plotting the dimming

# Create data.frame with distance-based dimming
df <- data.frame(
  "x" = 1,
  "d" = 1:10
)
df$x_dimmed <- non_smoothed_dimming_fn(df$x, df$d)

# Plot the dimming
if (has_ggplot){
  ggplot(df, aes(x=d, y=x_dimmed)) +
    geom_point() +
    geom_line() +
    theme_minimal()
}

[Package rearrr version 0.3.4 Index]