# 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()
}
Run the code above in your browser using DataLab